ensure we return closer from run()

This commit is contained in:
onyx-and-iris 2026-02-16 00:11:50 +00:00
parent c22b07808f
commit 80a3bdcd90
2 changed files with 35 additions and 27 deletions

View File

@ -86,46 +86,54 @@ func run() (func(), error) {
) )
} }
rcon, err := connectRcon(host, port, rconpass) client, closer, err := connectRcon(host, port, rconpass)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("failed to connect to rcon: %w", err)
}
defer rcon.Close()
if !interactive {
runCommands(flag.Args(), rcon)
return nil, nil
} }
if interactive {
fmt.Printf("Enter 'Q' to exit.\n>> ") fmt.Printf("Enter 'Q' to exit.\n>> ")
err = interactiveMode(rcon, os.Stdin) err := interactiveMode(client, os.Stdin)
if err != nil { if err != nil {
return nil, err return closer, fmt.Errorf("interactive mode error: %w", err)
} }
return nil, nil return closer, nil
} }
func connectRcon(host string, port int, password string) (*q3rcon.Rcon, error) { commands := flag.Args()
rcon, err := q3rcon.New(host, port, password, q3rcon.WithTimeouts(map[string]time.Duration{ if len(commands) == 0 {
log.Debug("no commands provided, defaulting to 'status'")
commands = append(commands, "status")
}
runCommands(client, commands)
return closer, nil
}
func connectRcon(host string, port int, password string) (*q3rcon.Rcon, func(), error) {
client, err := q3rcon.New(host, port, password, q3rcon.WithTimeouts(map[string]time.Duration{
"map": time.Second, "map": time.Second,
"map_rotate": time.Second, "map_rotate": time.Second,
"map_restart": time.Second, "map_restart": time.Second,
})) }))
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
return rcon, nil
closer := func() {
if err := client.Close(); err != nil {
log.Error(err)
}
}
return client, closer, nil
} }
// runCommands runs the commands given in the flag.Args slice. // runCommands runs the commands given in the flag.Args slice.
// If no commands are given, it defaults to running the "status" command. // If no commands are given, it defaults to running the "status" command.
func runCommands(commands []string, rcon *q3rcon.Rcon) { func runCommands(client *q3rcon.Rcon, commands []string) {
if len(commands) == 0 {
commands = append(commands, "status")
}
for _, cmd := range commands { for _, cmd := range commands {
resp, err := rcon.Send(cmd) resp, err := client.Send(cmd)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
continue continue
@ -135,7 +143,7 @@ func runCommands(commands []string, rcon *q3rcon.Rcon) {
} }
// interactiveMode continuously reads from input until a quit signal is given. // interactiveMode continuously reads from input until a quit signal is given.
func interactiveMode(rcon *q3rcon.Rcon, input io.Reader) error { func interactiveMode(client *q3rcon.Rcon, input io.Reader) error {
scanner := bufio.NewScanner(input) scanner := bufio.NewScanner(input)
for scanner.Scan() { for scanner.Scan() {
cmd := scanner.Text() cmd := scanner.Text()
@ -143,7 +151,7 @@ func interactiveMode(rcon *q3rcon.Rcon, input io.Reader) error {
return nil return nil
} }
resp, err := rcon.Send(cmd) resp, err := client.Send(cmd)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
continue continue

View File

@ -140,6 +140,6 @@ func (r Rcon) listen(timeout time.Duration, respChan chan<- string, errChan chan
} }
} }
func (r Rcon) Close() { func (r Rcon) Close() error {
r.conn.Close() return r.conn.Close()
} }