Added path checks and updated config
- Cleaned up func main() - Separated config into two sections server and factoryman - Updated backup.go to reflect changes to config file - Updated config.go to reflect changes to config.yml as well as added a check to paths in config.yml - Updated launch.go to reflect changes to config move cli handler from main to cli.go
This commit is contained in:
@@ -13,8 +13,8 @@ func backUp(cmd string, c GoConfig) {
|
||||
fmt.Println("Starting full server backup")
|
||||
t := time.Now()
|
||||
timeStamp := t.Format(time.RFC3339)
|
||||
fullBackupName := fmt.Sprintf("%s/ServerBackup.%s.tgz", c.Config.BackupDir, timeStamp)
|
||||
fullBackup := exec.Command("tar", "-czvf", fullBackupName, c.Config.ServDir)
|
||||
fullBackupName := fmt.Sprintf("%s/ServerBackup.%s.tgz", c.Factoryman.BackupDir, timeStamp)
|
||||
fullBackup := exec.Command("tar", "-czvf", fullBackupName, c.Server.ServDir)
|
||||
fullBackup.Stdout = os.Stdout
|
||||
err := fullBackup.Run()
|
||||
if err != nil {
|
||||
@@ -24,8 +24,8 @@ func backUp(cmd string, c GoConfig) {
|
||||
fmt.Println("Backing up saves")
|
||||
t := time.Now()
|
||||
timeStamp := t.Format(time.RFC3339)
|
||||
savesBackupName := fmt.Sprintf("%s/SaveBackup.%s.tgz", c.Config.BackupDir, timeStamp)
|
||||
saveDir := fmt.Sprintf("%s/saves", c.Config.ServDir)
|
||||
savesBackupName := fmt.Sprintf("%s/SaveBackup.%s.tgz", c.Factoryman.BackupDir, timeStamp)
|
||||
saveDir := fmt.Sprintf("%s/saves", c.Server.ServDir)
|
||||
savesBackup := exec.Command("tar", "-czvf", savesBackupName, saveDir)
|
||||
savesBackup.Stdout = os.Stdout
|
||||
err := savesBackup.Run()
|
||||
|
||||
41
cli.go
Normal file
41
cli.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func cliToolMode() {
|
||||
var c = readCfg(fmConfig)
|
||||
if verifyConfig(c) {
|
||||
if len(os.Args) > 1 {
|
||||
switch os.Args[1] {
|
||||
case "start":
|
||||
startStopServer("start", c)
|
||||
case "stop":
|
||||
startStopServer("stop", c)
|
||||
case "help", "h", "--help", "-h":
|
||||
fmt.Printf("Start Server: %s start\nStop Server: %s stop\n", os.Args[0], os.Args[0])
|
||||
fmt.Printf("Run backup\n\tFull backup: %s backup full", os.Args[0])
|
||||
fmt.Printf("\n\tBackup saves: %s backup saves\n", os.Args[0])
|
||||
case "backup":
|
||||
if len(os.Args) > 2 {
|
||||
switch os.Args[2] {
|
||||
case "full":
|
||||
backUp("full", c)
|
||||
case "saves":
|
||||
backUp("saves", c)
|
||||
default:
|
||||
fmt.Println("Invalid backup type: use 'full' or 'saves'")
|
||||
}
|
||||
} else {
|
||||
fmt.Println("Missing backup type: use 'full' or 'saves'")
|
||||
}
|
||||
default:
|
||||
fmt.Printf("Unknown command: %s. Use 'start', 'stop', or 'backup'.\n", os.Args[1])
|
||||
}
|
||||
} else {
|
||||
fmt.Println("Use 'start', 'stop', or 'backup' command\nex. factoryman start\nTo configure edit 'conifg.yml'")
|
||||
}
|
||||
}
|
||||
}
|
||||
44
config.go
44
config.go
@@ -1,6 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
@@ -10,16 +12,19 @@ import (
|
||||
const fmConfig = "config.yml"
|
||||
|
||||
type GoConfig struct {
|
||||
Config struct {
|
||||
Server struct {
|
||||
ServDir string `yaml:"serverFolder"`
|
||||
ServPort int `yaml:"port"`
|
||||
WorldFile string `yaml:"worldFile"`
|
||||
ServCfg string `yaml:"serverSettings"`
|
||||
ServExec string `yaml:"serverExec"`
|
||||
} `yaml:"server"`
|
||||
|
||||
Factoryman struct {
|
||||
ServPort int `yaml:"port"`
|
||||
BackupDir string `yaml:"backupDir"`
|
||||
UseScreen bool `yaml:"screen"`
|
||||
ScreenName string `yaml:"screenName"`
|
||||
} `yaml:"server"`
|
||||
} `yaml:"factoryman"`
|
||||
}
|
||||
|
||||
func readCfg(factCfg string) GoConfig {
|
||||
@@ -36,3 +41,36 @@ func readCfg(factCfg string) GoConfig {
|
||||
}
|
||||
return config
|
||||
}
|
||||
func isItReal(path string) bool {
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return true
|
||||
} else if errors.Is(err, os.ErrNotExist) {
|
||||
return false
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
func verifyConfig(config GoConfig) bool {
|
||||
if !isItReal(config.Server.ServDir) {
|
||||
fmt.Printf("PATH NOT FOUND: %s", config.Server.ServDir)
|
||||
return false
|
||||
}
|
||||
if !isItReal(config.Server.WorldFile) {
|
||||
fmt.Printf("PATH NOT FOUND: %s", config.Server.WorldFile)
|
||||
return false
|
||||
}
|
||||
if !isItReal(config.Server.ServCfg) {
|
||||
fmt.Printf("PATH NOT FOUND: %s", config.Server.ServCfg)
|
||||
return false
|
||||
}
|
||||
if !isItReal(config.Server.ServExec) {
|
||||
fmt.Printf("PATH NOT FOUND: %s", config.Server.ServExec)
|
||||
return false
|
||||
}
|
||||
if !isItReal(config.Factoryman.BackupDir) {
|
||||
fmt.Printf("PATH NOT FOUND: %s", config.Factoryman.BackupDir)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
||||
}
|
||||
|
||||
13
config.yml
13
config.yml
@@ -1,9 +1,12 @@
|
||||
server:
|
||||
serverFolder: "factorio-headless_linux_2.0.72/factorio"
|
||||
serverFolder: "factorio"
|
||||
worldFile: "factorio/data/saves/newworld.zip"
|
||||
serverSettings: "factorio/data/server-settings.json"
|
||||
serverExec: "factorio/bin/x64/factorio"
|
||||
|
||||
|
||||
factoryman:
|
||||
port: 34197
|
||||
worldFile: "factorio-headless_linux_2.0.72/factorio/Newworlds.zip"
|
||||
serverSettings: "factorio-headless_linux_2.0.72/factorio/data/server-settings.json"
|
||||
backupDir: "factorio-headless_linux_2.0.72/backups"
|
||||
serverExec: "factorio-headless_linux_2.0.72/factorio/bin/x64/factorio"
|
||||
screen: True
|
||||
screenName: "Factorio"
|
||||
backupDir: "factorio/backups"
|
||||
BIN
factoryman
BIN
factoryman
Binary file not shown.
@@ -10,10 +10,10 @@ import (
|
||||
func startStopServer(cmd string, con GoConfig) {
|
||||
switch cmd {
|
||||
case "start":
|
||||
x := fmt.Sprintf("%s --port %d --server-settings %s --start-server %s", con.Config.ServExec, con.Config.ServPort, con.Config.ServCfg, con.Config.WorldFile)
|
||||
if con.Config.UseScreen { // if screen enabled in confing.yml
|
||||
x := fmt.Sprintf("%s --port %d --server-settings %s --start-server %s", con.Server.ServExec, con.Factoryman.ServPort, con.Server.ServCfg, con.Server.WorldFile)
|
||||
if con.Factoryman.UseScreen { // if screen enabled in confing.yml
|
||||
fmt.Println("Starting factorio server in screen session")
|
||||
startScreenCmd := exec.Command("screen", "-dmS", con.Config.ScreenName, "bash", "-c", x, "; exec sh")
|
||||
startScreenCmd := exec.Command("screen", "-dmS", con.Factoryman.ScreenName, "bash", "-c", x, "; exec sh")
|
||||
startScreenCmd.Stdout = os.Stdout
|
||||
startScreenCmd.Stderr = os.Stderr
|
||||
err := startScreenCmd.Run()
|
||||
@@ -31,7 +31,7 @@ func startStopServer(cmd string, con GoConfig) {
|
||||
}
|
||||
}
|
||||
case "stop":
|
||||
quitServerCmd := exec.Command("screen", "-S", con.Config.ScreenName, "-p", "0", "-X", "stuff", "/quit\n")
|
||||
quitServerCmd := exec.Command("screen", "-S", con.Factoryman.ScreenName, "-p", "0", "-X", "stuff", "/quit\n")
|
||||
err := quitServerCmd.Run()
|
||||
if err != nil {
|
||||
log.Fatalf("Command failed: %s, Error: %v", quitServerCmd.Args, err)
|
||||
|
||||
36
main.go
36
main.go
@@ -5,42 +5,10 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
var c = readCfg(fmConfig)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) == 1 {
|
||||
fmt.Println("Use 'start', 'stop', or 'backup' command")
|
||||
fmt.Println("ex. factoryman start")
|
||||
fmt.Println("To configure edit 'conifg.yml'")
|
||||
} else if len(os.Args) > 1 {
|
||||
switch os.Args[1] {
|
||||
case "start":
|
||||
startStopServer("start", c)
|
||||
case "stop":
|
||||
startStopServer("stop", c)
|
||||
case "help", "h", "--help", "-h":
|
||||
fmt.Printf("Start Server: %s start\nStop Server: %s stop\n", os.Args[0], os.Args[0])
|
||||
fmt.Printf("Run backup\n\tFull backup: %s backup full", os.Args[0])
|
||||
fmt.Printf("\n\tBackup saves: %s backup saves\n", os.Args[0])
|
||||
case "backup":
|
||||
if len(os.Args) > 2 {
|
||||
switch os.Args[2] {
|
||||
case "full":
|
||||
backUp("full", c)
|
||||
case "saves":
|
||||
backUp("saves", c)
|
||||
default:
|
||||
fmt.Println("Invalid backup type: use 'full' or 'saves'")
|
||||
}
|
||||
fmt.Println("Use 'start', 'stop', or 'backup' command\nex. factoryman start\nTo configure edit 'conifg.yml'")
|
||||
} else {
|
||||
fmt.Println("Missing backup type: use 'full' or 'saves'")
|
||||
}
|
||||
default:
|
||||
fmt.Printf("Unknown command: %s. Use 'start', 'stop', or 'backup'.\n", os.Args[1])
|
||||
}
|
||||
} else {
|
||||
fmt.Println("Use 'start', 'stop', or 'backup' command")
|
||||
fmt.Println("ex. factoryman start")
|
||||
fmt.Println("To configure edit 'conifg.yml'")
|
||||
cliToolMode()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user