-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
208 lines (189 loc) · 4.33 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
"time"
"github.com/chzyer/readline"
rcon "github.com/forewing/csgo-rcon"
)
// Flags of the command line
type Flags struct {
Address *string `json:",omitempty"`
Password *string `json:",omitempty"`
Timeout *float64 `json:",omitempty"`
Config *string `json:",omitempty"`
From *string `json:",omitempty"`
Completion *string `json:",omitempty"`
Interactive *bool `json:",omitempty"`
}
var (
flags Flags = Flags{
Address: flag.String("a", rcon.DefaultAddress, "`address` of the server RCON, in the format of HOST:PORT."),
Password: flag.String("p", rcon.DefaultPassword, "`password` of the RCON."),
Timeout: flag.Float64("t", rcon.DefaultTimeout.Seconds(), "`timeout` of the connection (seconds)."),
Config: flag.String("c", "", "load configs from `file` instead of flags."),
From: flag.String("f", "", "read commands from `file`, \"-\" for stdin. From arguments if not set."),
Completion: flag.String("m", "", "read completions from `file`"),
Interactive: flag.Bool("i", false, "interact with the console."),
}
client *rcon.Client
)
func init() {
flag.Parse()
if len(*flags.Config) == 0 {
return
}
data, err := ioutil.ReadFile(*flags.Config)
if err != nil {
fatal(err.Error())
}
err = json.Unmarshal(data, &flags)
if err != nil {
fatal(err.Error())
}
}
func fatal(message string) {
fmt.Fprintln(os.Stderr, message)
os.Exit(1)
}
func main() {
client = rcon.New(*flags.Address, *flags.Password, time.Duration(*flags.Timeout*float64(time.Second)))
if *flags.Interactive {
runInteractive()
return
}
if len(*flags.From) > 0 {
runFile(*flags.From)
return
}
runArgs()
}
func runArgs() {
cmd := strings.TrimSpace(strings.Join(flag.Args(), " "))
if len(cmd) == 0 {
fatal("empty commands")
}
message, err := client.Execute(cmd)
fmt.Println(strings.TrimSpace(message))
if err != nil {
fatal(err.Error())
}
}
func runFile(filename string) {
file := os.Stdin
if filename != "-" {
var err error
file, err = os.Open(filename)
if err != nil {
fatal(err.Error())
}
}
data, err := ioutil.ReadAll(file)
if err != nil {
fatal(err.Error())
}
message, err := client.Execute(string(data))
fmt.Println(strings.TrimSpace(message))
if err != nil {
fatal(err.Error())
}
}
func getCommandCompletion() func(string) []string {
return func(s string) []string {
commands := make([]string, 0)
if len(*flags.Completion) != 0 {
file, err := os.Open(*flags.Completion)
if err != nil {
fatal(err.Error())
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
commands = append(commands, strings.Split(scanner.Text(), " ")[0])
}
if err := scanner.Err(); err != nil {
fatal(err.Error())
}
}
return commands
}
}
var completer = readline.NewPrefixCompleter(
readline.PcItem("mode",
readline.PcItem("vi"),
readline.PcItem("emacs"),
),
readline.PcItemDynamic(getCommandCompletion()),
readline.PcItem("bye"),
)
func filterInput(r rune) (rune, bool) {
switch r {
// block CtrlZ feature
case readline.CharCtrlZ:
return r, false
}
return r, true
}
func runInteractive() {
l, err := readline.NewEx(&readline.Config{
Prompt: "\033[34m>>> \033[0m",
HistoryFile: "/tmp/csgo-rcon.tmp",
AutoComplete: completer,
InterruptPrompt: "^C",
EOFPrompt: "exit",
HistorySearchFold: true,
FuncFilterInputRune: filterInput,
})
if err != nil {
panic(err)
}
defer l.Close()
log.SetOutput(l.Stderr())
for {
line, err := l.Readline()
if err == readline.ErrInterrupt {
if len(line) == 0 {
break
} else {
continue
}
} else if err == io.EOF {
break
}
line = strings.TrimSpace(line)
switch {
case strings.HasPrefix(line, "mode "):
switch line[5:] {
case "vi":
l.SetVimMode(true)
case "emacs":
l.SetVimMode(false)
default:
println("invalid mode:", line[5:])
}
case line == "mode":
if l.IsVimMode() {
println("current mode: vim")
} else {
println("current mode: emacs")
}
case line == "bye":
goto exit
case line == "":
default:
message, err := client.Execute(string(line))
fmt.Println(strings.TrimSpace(message))
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}
exit:
}