Skip to content

Commit 8d7a547

Browse files
authored
Add liner and enable interactive cli to have a history and the user to iterate through history (#69)
1 parent cc9cfff commit 8d7a547

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

cli/entry.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
package cli
1818

1919
import (
20-
"bufio"
20+
"errors"
2121
"fmt"
2222
"github.com/james4k/rcon"
23+
"github.com/peterh/liner"
2324
"io"
2425
"log"
2526
"os"
@@ -55,17 +56,35 @@ var colors = map[string]string{
5556
"r": Reset, // reset
5657
}
5758

58-
func Start(hostPort string, password string, in io.Reader, out io.Writer) {
59+
func Start(hostPort string, password string, out io.Writer) {
5960
remoteConsole, err := rcon.Dial(hostPort, password)
6061
if err != nil {
6162
log.Fatal("Failed to connect to RCON server", err)
6263
}
6364
defer remoteConsole.Close()
6465

65-
scanner := bufio.NewScanner(in)
66-
_, _ = out.Write([]byte("> "))
67-
for scanner.Scan() {
68-
cmd := scanner.Text()
66+
lineEditor := liner.NewLiner()
67+
defer lineEditor.Close()
68+
69+
for {
70+
cmd, err := lineEditor.Prompt("> ")
71+
72+
if err != nil {
73+
if errors.Is(err, liner.ErrPromptAborted) {
74+
return
75+
}
76+
77+
if errors.Is(err, io.EOF) {
78+
return
79+
}
80+
81+
_, _ = fmt.Fprintln(os.Stderr, "Error reading input:", err)
82+
}
83+
84+
if cmd == "exit" {
85+
return
86+
}
87+
6988
reqId, err := remoteConsole.Write(cmd)
7089
if err != nil {
7190
_, _ = fmt.Fprintln(os.Stderr, "Failed to send command:", err.Error())
@@ -87,11 +106,8 @@ func Start(hostPort string, password string, in io.Reader, out io.Writer) {
87106

88107
resp = colorize(resp)
89108
_, _ = fmt.Fprintln(out, resp)
90-
_, _ = out.Write([]byte("> "))
91-
}
92109

93-
if err := scanner.Err(); err != nil {
94-
_, _ = fmt.Fprintln(os.Stderr, "reading standard input:", err)
110+
lineEditor.AppendHistory(cmd)
95111
}
96112
}
97113

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ and the CLI will exit.
5454
password := viper.GetString("password")
5555

5656
if len(args) == 0 {
57-
cli.Start(hostPort, password, os.Stdin, os.Stdout)
57+
cli.Start(hostPort, password, os.Stdout)
5858
} else {
5959
cli.Execute(hostPort, password, os.Stdout, args...)
6060
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ require (
1111
github.com/hashicorp/hcl v1.0.0 // indirect
1212
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1313
github.com/magiconair/properties v1.8.7 // indirect
14+
github.com/mattn/go-runewidth v0.0.3 // indirect
1415
github.com/mitchellh/mapstructure v1.5.0 // indirect
1516
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
17+
github.com/peterh/liner v1.2.2 // indirect
1618
github.com/sagikazarmark/locafero v0.4.0 // indirect
1719
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
1820
github.com/sourcegraph/conc v0.3.0 // indirect

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
2121
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2222
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
2323
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
24+
github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8BzLR4=
25+
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
2426
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
2527
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
2628
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
2729
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
30+
github.com/peterh/liner v1.2.2 h1:aJ4AOodmL+JxOZZEL2u9iJf8omNRpqHc/EbrK+3mAXw=
31+
github.com/peterh/liner v1.2.2/go.mod h1:xFwJyiKIXJZUKItq5dGHZSTBRAuG/CpeNpWLyiNRNwI=
2832
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2933
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
3034
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -65,6 +69,7 @@ go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
6569
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
6670
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
6771
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
72+
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6873
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
6974
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
7075
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=

0 commit comments

Comments
 (0)