-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
196 lines (178 loc) · 4.31 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
package main
import (
"bufio"
"encoding/json"
"fmt"
"net"
"os"
"strings"
"syscall"
"github.com/TylerBrock/colorjson"
prompt "github.com/c-bata/go-prompt"
"github.com/jobatator/cli/pkg/connexion"
"golang.org/x/crypto/ssh/terminal"
)
// LivePrefixState -
var LivePrefixState struct {
LivePrefix string
IsEnable bool
}
var conn net.Conn
var reader *bufio.Reader
var suggests []prompt.Suggest
var enableRaw bool = true
var jsonFormatter *colorjson.Formatter
func read() string {
out, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
return out
}
func handleOutput() {
output := read()
jsonStr := []byte("")
if enableRaw {
// pretty print json objects
if output[0:2] == "{\"" && output[len(output)-2:len(output)-1] == "}" {
// case of a json object
var obj map[string]interface{}
json.Unmarshal([]byte(output), &obj)
jsonStr, _ = jsonFormatter.Marshal(obj)
}
if output[0:1] == "[" && output[len(output)-2:len(output)-1] == "]" {
// case of a json array
var obj []interface{}
json.Unmarshal([]byte(output), &obj)
jsonStr, _ = jsonFormatter.Marshal(obj)
}
}
if len(jsonStr) == 0 {
fmt.Print(output)
} else {
fmt.Println(string(jsonStr))
}
}
func executor(in string) {
if strings.ToUpper(in) == "QUIT" || strings.ToUpper(in) == "EXIT" {
conn.Close()
os.Exit(0)
}
fmt.Fprintf(conn, in+"\n")
handleOutput()
}
func completer(in prompt.Document) []prompt.Suggest {
if len(strings.Split(in.Text, " ")) == 1 && len(in.Text) != 0 {
return prompt.FilterHasPrefix(suggests, in.GetWordBeforeCursor(), true)
}
return []prompt.Suggest{}
}
func changeLivePrefix() (string, bool) {
return LivePrefixState.LivePrefix + "> ", true
}
func main() {
var commandsToRun []string
fi, err := os.Stdin.Stat()
if err != nil {
panic(err)
}
if !(fi.Mode()&os.ModeNamedPipe == 0) {
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
commandsToRun = strings.Split(string(input), ";")
}
var url string = ""
for key, arg := range os.Args {
if key == 0 {
break
}
if arg == "-r" || arg == "--raw" {
enableRaw = false
} else if len(arg) > 1 {
url = arg
}
}
options := connexion.ParseURL(url)
if enableRaw {
jsonFormatter = colorjson.NewFormatter()
jsonFormatter.Indent = 4
}
// connect to socket
tmpConn, err := net.Dial("tcp", options.Host+":"+options.Port)
if err != nil {
panic(err)
}
conn = tmpConn
reader = bufio.NewReader(conn)
if len(options.Username) > 0 && len(options.Password) == 0 {
// ask for the password
fmt.Print("Password for " + options.Username + "@" + options.Host + ":" + options.Port + " : ")
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
panic(err)
}
fmt.Println()
options.Password = string(bytePassword)
}
isAuthenticated := false
if len(options.Password) > 0 {
conn.Write([]byte("AUTH " + options.Username + " " + options.Password + " "))
out := read()
out = out[:len(out)-1]
if out != "Welcome!" {
fmt.Println(out)
conn.Close()
os.Exit(0)
} else {
isAuthenticated = true
LivePrefixState.LivePrefix = options.Username + "@"
}
}
LivePrefixState.LivePrefix += options.Host + ":" + options.Port
if len(options.Group) > 0 && isAuthenticated {
// set group
conn.Write([]byte("USE_GROUP " + options.Group + " "))
out := read()
out = out[:len(out)-1]
if out != "OK" {
fmt.Println(out)
conn.Close()
os.Exit(0)
}
LivePrefixState.LivePrefix += "/" + options.Group
}
// AUTOMATIC MODE
if len(commandsToRun) > 0 {
for key, cmd := range commandsToRun {
if len(commandsToRun)-1 == key {
cmd = cmd[0 : len(cmd)-1]
}
cmd = strings.Trim(cmd, " ") + " "
conn.Write([]byte(cmd))
handleOutput()
}
os.Exit(0)
}
// MANUAL MOOE
// fetch all commands
conn.Write([]byte("HELP "))
out := read()
out = out[:len(out)-1]
var commands []string
json.Unmarshal([]byte(out), &commands)
for _, cmd := range commands {
entry := prompt.Suggest{Text: cmd, Description: ""}
suggests = append(suggests, entry)
}
fmt.Println("Use Ctrl+D to exit")
p := prompt.New(
executor,
completer,
prompt.OptionPrefix(">>> "),
prompt.OptionLivePrefix(changeLivePrefix),
prompt.OptionTitle("jobatator "+conn.RemoteAddr().String()),
prompt.OptionInputTextColor(prompt.Cyan),
prompt.OptionPrefixTextColor(prompt.DarkGray),
)
p.Run()
}