Skip to content

Commit 3fb944c

Browse files
committed
Allow for one-shot command executions
1 parent cb9bcc5 commit 3fb944c

File tree

3 files changed

+72
-10
lines changed

3 files changed

+72
-10
lines changed

README.md

+22-7
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,28 @@ Done.
1010

1111
## Usage
1212

13-
```bash
14-
./rcon-cli_* --help
15-
```
13+
```text
14+
rcon-cli is a CLI for attaching to an RCON enabled game server, such as Minecraft.
15+
Without any additional arguments, the CLI will start an interactive session with
16+
the RCON server.
1617
17-
For example:
18+
If arguments are passed into the CLI, then the arguments are sent
19+
as a single command (joined by spaces), the response is displayed,
20+
and the CLI will exit.
1821
19-
```bash
20-
./rcon-cli_darwin_amd64 --host localhost --port 25575 --password test
21-
```
22+
Usage:
23+
rcon-cli [flags] [RCON command ...]
24+
25+
Examples:
2226
27+
rcon-cli --host mc1 --port 25575
28+
rcon-cli --port 25575 stop
29+
RCON_PORT=25575 rcon-cli stop
30+
31+
32+
Flags:
33+
--config string config file (default is $HOME/.rcon-cli.yaml)
34+
--host string RCON server's hostname (default "localhost")
35+
--password string RCON server's password
36+
--port int Server's RCON port (default 27015)
37+
```

cli/entry.go

+27
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"bufio"
2525
"io"
2626
"fmt"
27+
"strings"
2728
)
2829

2930
func Start(hostPort string, password string, in io.Reader, out io.Writer) {
@@ -64,3 +65,29 @@ func Start(hostPort string, password string, in io.Reader, out io.Writer) {
6465
fmt.Fprintln(os.Stderr, "reading standard input:", err)
6566
}
6667
}
68+
69+
func Execute(hostPort string, password string, out io.Writer, command ...string) {
70+
remoteConsole, err := rcon.Dial(hostPort, password)
71+
if err != nil {
72+
log.Fatal("Failed to connect to RCON server", err)
73+
}
74+
defer remoteConsole.Close()
75+
76+
preparedCmd := strings.Join(command, " ")
77+
reqId, err := remoteConsole.Write(preparedCmd)
78+
79+
resp, respReqId, err := remoteConsole.Read()
80+
if err != nil {
81+
if err == io.EOF {
82+
return
83+
}
84+
fmt.Fprintln(os.Stderr, "Failed to read command:", err.Error())
85+
return
86+
}
87+
88+
if reqId != respReqId {
89+
fmt.Fprintln(out, "Weird. This response is for another request.")
90+
}
91+
92+
fmt.Fprintln(out, resp)
93+
}

cmd/root.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,32 @@ var (
3131

3232
// RootCmd represents the base command when called without any subcommands
3333
var RootCmd = &cobra.Command{
34-
Use: "rcon-cli",
34+
Use: "rcon-cli [flags] [RCON command ...]",
3535
Short: "A CLI for attaching to an RCON enabled game server",
36-
Long: ``,
36+
Example: `
37+
rcon-cli --host mc1 --port 25575
38+
rcon-cli --port 25575 stop
39+
RCON_PORT=25575 rcon-cli stop
40+
`,
41+
Long: `
42+
rcon-cli is a CLI for attaching to an RCON enabled game server, such as Minecraft.
43+
Without any additional arguments, the CLI will start an interactive session with
44+
the RCON server.
45+
46+
If arguments are passed into the CLI, then the arguments are sent
47+
as a single command (joined by spaces), the response is displayed,
48+
and the CLI will exit.
49+
`,
3750
Run: func(cmd *cobra.Command, args []string) {
51+
3852
hostPort := net.JoinHostPort(viper.GetString("host"), strconv.Itoa(viper.GetInt("port")))
39-
cli.Start(hostPort, viper.GetString("password"), os.Stdin, os.Stdout)
53+
password := viper.GetString("password")
54+
55+
if len(args) == 0 {
56+
cli.Start(hostPort, password, os.Stdin, os.Stdout)
57+
} else {
58+
cli.Execute(hostPort, password, os.Stdout, args...)
59+
}
4060
},
4161
}
4262

0 commit comments

Comments
 (0)