Fixes Changes and new additions

Fixed an issue where launching server w/o screen failed
New: added an output showing screen sessions after launching
New: added stdout and stdin to screenless launch allowing interacting with server cli
Change: Broke up source code to make it easier to read and make changes
Change: Updated .gitignore
This commit is contained in:
2026-01-13 10:51:18 -07:00
parent 3406ab4743
commit b2c534f29b
7 changed files with 134 additions and 111 deletions

55
launchserver.go Normal file
View File

@@ -0,0 +1,55 @@
package main
import (
"fmt"
"log"
"os"
"os/exec"
"time"
)
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
fmt.Println("Starting factorio server in screen session")
startScreenCmd := exec.Command("screen", "-dmS", con.Config.ScreenName, "bash", "-c", x, "; exec sh")
startScreenCmd.Stdout = os.Stdout
startScreenCmd.Stderr = os.Stderr
err := startScreenCmd.Run()
if err != nil {
log.Fatalf("Failed to start server: %s", err)
} else {
fmt.Printf("Started server on port %d, in screen named %s\n", con.Config.ServPort, con.Config.ScreenName)
y := exec.Command("screen", "-ls")
y.Stdout = os.Stdout
y.Stderr = os.Stderr
err = y.Run()
}
} else {
startSrvCmd := exec.Command("bash", "-c", x)
startSrvCmd.Stdout = os.Stdout
startSrvCmd.Stderr = os.Stdout
startSrvCmd.Stdin = os.Stdin
err := startSrvCmd.Run()
if err != nil {
log.Fatalf("Failed to start server: %s", err)
}
}
case "stop":
quitServerCmd := exec.Command("screen", "-S", con.Config.ScreenName, "-p", "0", "-X", "stuff", "/quit\n")
err := quitServerCmd.Run()
if err != nil {
log.Fatalf("Command failed: %s, Error: %v", quitServerCmd.Args, err)
} else {
fmt.Printf("Server in screen %s stopped\n", con.Config.ScreenName)
}
fmt.Println("Waiting for server to shutdown\r")
time.Sleep(10 * time.Second)
y := exec.Command("screen", "-ls")
y.Stdout = os.Stdout
y.Stderr = os.Stderr
err = y.Run()
}
}