send 'status' if not in interactive and no commands passed

remove interactive global var.
This commit is contained in:
2025-02-03 17:26:42 +00:00
parent c51dba5ead
commit 485978956c
3 changed files with 47 additions and 52 deletions

View File

@@ -2,7 +2,6 @@ package main
import (
"bufio"
"errors"
"flag"
"fmt"
"io"
@@ -15,28 +14,26 @@ import (
log "github.com/sirupsen/logrus"
)
var interactive bool
func exit(err error) {
func exitOnError(err error) {
_, _ = fmt.Fprintf(os.Stderr, "Error: %s\n", err)
flag.Usage()
os.Exit(1)
}
func main() {
var (
host string
port int
rconpass string
loglevel int
host string
port int
rconpass string
interactive bool
loglevel int
)
flag.StringVar(&host, "host", "localhost", "hostname of the server")
flag.StringVar(&host, "h", "localhost", "hostname of the server (shorthand)")
flag.IntVar(&port, "port", 28960, "port of the server")
flag.IntVar(&port, "p", 28960, "port of the server (shorthand)")
flag.StringVar(&rconpass, "rconpass", "", "rcon password")
flag.StringVar(&rconpass, "r", "", "rcon password (shorthand)")
flag.StringVar(&host, "host", "localhost", "hostname of the gameserver")
flag.StringVar(&host, "h", "localhost", "hostname of the gameserver (shorthand)")
flag.IntVar(&port, "port", 28960, "port on which the gameserver resides, default is 28960")
flag.IntVar(&port, "p", 28960, "port on which the gameserver resides, default is 28960 (shorthand)")
flag.StringVar(&rconpass, "rconpass", os.Getenv("RCON_PASS"), "rcon password of the gameserver")
flag.StringVar(&rconpass, "r", os.Getenv("RCON_PASS"), "rcon password of the gameserver (shorthand)")
flag.BoolVar(&interactive, "interactive", false, "run in interactive mode")
flag.BoolVar(&interactive, "i", false, "run in interactive mode")
@@ -49,33 +46,29 @@ func main() {
log.SetLevel(log.Level(loglevel))
}
if port < 1024 || port > 65535 {
exitOnError(fmt.Errorf("invalid port value, got: (%d) expected: in range 1024-65535", port))
}
if len(rconpass) < 8 {
exitOnError(fmt.Errorf("invalid rcon password, got: (%s) expected: at least 8 characters", rconpass))
}
rcon, err := connectRcon(host, port, rconpass)
if err != nil {
log.Fatal(err)
exitOnError(err)
}
defer rcon.Close()
if interactive {
fmt.Printf("Enter 'Q' to exit.\n>> ")
err := interactiveMode(rcon, os.Stdin)
if err != nil {
log.Fatal(err)
}
if !interactive {
runCommands(flag.Args(), rcon)
return
}
if len(flag.Args()) == 0 {
err = errors.New("no rcon commands passed")
exit(err)
}
for _, arg := range flag.Args() {
resp, err := rcon.Send(arg)
if err != nil {
log.Error(err)
continue
}
fmt.Print(resp)
fmt.Printf("Enter 'Q' to exit.\n>> ")
err = interactiveMode(rcon, os.Stdin)
if err != nil {
exitOnError(err)
}
}
@@ -87,6 +80,23 @@ func connectRcon(host string, port int, password string) (*q3rcon.Rcon, error) {
return rcon, nil
}
// runCommands runs the commands given in the flag.Args slice.
// If no commands are given, it defaults to running the "status" command.
func runCommands(commands []string, rcon *q3rcon.Rcon) {
if len(commands) == 0 {
commands = append(commands, "status")
}
for _, cmd := range commands {
resp, err := rcon.Send(cmd)
if err != nil {
log.Error(err)
continue
}
fmt.Print(resp)
}
}
// interactiveMode continuously reads from input until a quit signal is given.
func interactiveMode(rcon *q3rcon.Rcon, input io.Reader) error {
scanner := bufio.NewScanner(input)
@@ -103,6 +113,7 @@ func interactiveMode(rcon *q3rcon.Rcon, input io.Reader) error {
}
fmt.Printf("%s>> ", resp)
}
if scanner.Err() != nil {
return scanner.Err()
}