add parsing for running command, fix bug where server status doesnt update after stopped server

This commit is contained in:
majormjr 2016-12-15 15:08:02 -05:00 committed by Mitch Roote
parent 5b40702685
commit dbd40e6d6c

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"bufio"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
@ -94,7 +95,7 @@ func (f *FactorioServer) Run() error {
args := []string{ args := []string{
"--start-server", filepath.Join(config.FactorioSavesDir, f.Savefile), "--start-server", filepath.Join(config.FactorioSavesDir, f.Savefile),
"--port", strconv.Itoa(f.Port), "--port", strconv.Itoa(f.Port),
"--server-settings", filepath.Join(config.FactorioDir, "server-settings.json"), "--server-settings", filepath.Join(config.FactorioConfigDir, "server-settings.json"),
} }
log.Println("Starting server with command: ", config.FactorioBinary, args) log.Println("Starting server with command: ", config.FactorioBinary, args)
@ -119,8 +120,8 @@ func (f *FactorioServer) Run() error {
return err return err
} }
go io.Copy(os.Stdout, f.StdOut) go parseRunningCommand(f.StdOut)
go io.Copy(os.Stderr, f.StdErr) go parseRunningCommand(f.StdErr)
err = f.Cmd.Start() err = f.Cmd.Start()
if err != nil { if err != nil {
@ -139,11 +140,27 @@ func (f *FactorioServer) Run() error {
return nil return nil
} }
func parseRunningCommand(std io.ReadCloser) (err error) {
stdScanner := bufio.NewScanner(std)
for stdScanner.Scan() {
fmt.Println(stdScanner.Text())
}
if err := stdScanner.Err(); err != nil {
log.Printf("Error reading stdout buffer: %s", err)
return err
}
return nil
}
func (f *FactorioServer) Stop() error { func (f *FactorioServer) Stop() error {
// TODO: Find an alternative to os.Kill on Windows. os.Interupt // TODO: Find an alternative to os.Kill on Windows. os.Interupt
// is not implemented. Maps will not be saved. // is not implemented. Maps will not be saved.
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
err := f.Cmd.Process.Signal(os.Kill) err := f.Cmd.Process.Signal(os.Kill)
if err.Error() == "os: process already finished" {
f.Running = false
return err
}
if err != nil { if err != nil {
log.Printf("Error sending SIGKILLL to Factorio process: %s", err) log.Printf("Error sending SIGKILLL to Factorio process: %s", err)
return err return err
@ -153,6 +170,10 @@ func (f *FactorioServer) Stop() error {
} }
} else { } else {
err := f.Cmd.Process.Signal(os.Interrupt) err := f.Cmd.Process.Signal(os.Interrupt)
if err.Error() == "os: process already finished" {
f.Running = false
return err
}
if err != nil { if err != nil {
log.Printf("Error sending SIGINT to Factorio process: %s", err) log.Printf("Error sending SIGINT to Factorio process: %s", err)
return err return err