mirror of
https://github.com/onyx-and-iris/q3rcon.git
synced 2026-02-16 02:07:53 +00:00
ensure we return closer from run()
This commit is contained in:
parent
c22b07808f
commit
3be7ddb36b
@ -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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Enter 'Q' to exit.\n>> ")
|
if interactive {
|
||||||
err = interactiveMode(rcon, os.Stdin)
|
fmt.Printf("Enter 'Q' to exit.\n>> ")
|
||||||
if err != nil {
|
err := interactiveMode(client, os.Stdin)
|
||||||
return nil, err
|
if err != nil {
|
||||||
|
return closer, fmt.Errorf("interactive mode error: %w", err)
|
||||||
|
}
|
||||||
|
return closer, nil
|
||||||
}
|
}
|
||||||
return nil, nil
|
|
||||||
|
commands := flag.Args()
|
||||||
|
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, error) {
|
func connectRcon(host string, port int, password string) (*q3rcon.Rcon, func(), error) {
|
||||||
rcon, err := q3rcon.New(host, port, password, q3rcon.WithTimeouts(map[string]time.Duration{
|
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
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user