-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
260 lines (223 loc) · 6.14 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/DENICeG/go-rriclient/internal/env"
"github.com/DENICeG/go-rriclient/pkg/rri"
"github.com/sbreitf1/go-console"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
gitCommit string
buildTime string
)
var (
app = kingpin.New("rri-client", "Client application for RRI")
argAddress = app.Arg("address", "Address and port like host:1234 of the RRI host").String()
argUser = app.Flag("user", "RRI user to use for login").Short('u').String()
argPassword = app.Flag("pass", "RRI password to use for login. Will be asked for if only user is set").Short('p').String()
argFile = app.Flag("file", "Input file containing RRI requests separated by a '=-=' line").Short('f').String()
argEnvironment = app.Flag("env", "Named environment to use or create").Short('e').String()
argDeleteEnv = app.Flag("delete-env", "Delete an existing environment").String()
argVerbose = app.Flag("verbose", "Print all sent and received requests").Short('v').Bool()
argInsecure = app.Flag("insecure", "Disable SSL Certificate checks").Bool()
argVersion = app.Flag("version", "Display application version and exit").Bool()
)
type environment struct {
Address string `json:"address"`
User string `json:"user"`
Password string `json:"pass" jcrypt:"aes"`
}
func (e environment) HasCredentials() bool {
return len(e.User) > 0 && len(e.Password) > 0
}
func main() {
kingpin.MustParse(app.Parse(os.Args[1:]))
if *argVersion {
console.Println("Standalone RRI Client")
if len(buildTime) == 0 || len(gitCommit) == 0 {
console.Println(" no build information available")
} else {
console.Printlnf(" built at %s from commit %s", buildTime, gitCommit)
}
return
}
if err := func() error {
envReader, err := env.NewReader(".rri-client")
if err != nil {
return err
}
envReader.EnterEnvHandler = enterEnvironment
envReader.GetEnvFileTitle = getEnvTitle
if len(*argDeleteEnv) > 0 {
if err := envReader.DeleteEnvironment(*argDeleteEnv); err != nil {
return err
}
console.Printlnf("environment %q has been deleted", *argDeleteEnv)
return nil
}
env, err := retrieveEnvironment(envReader)
if err != nil {
return err
}
if len(env.Address) == 0 {
return fmt.Errorf("missing RRI server address")
}
client, err := rri.NewClient(env.Address, &rri.ClientConfig{Insecure: *argInsecure})
if err != nil {
if !*argInsecure && strings.Contains(err.Error(), "x509") {
console.Println("HINT: try the '--insecure' flag if you have trouble with self signed certificates")
}
return err
}
defer client.Close()
if *argVerbose {
client.RawQueryPrinter = rawQueryPrinter
}
if env.HasCredentials() {
if err := client.Login(env.User, env.Password); err != nil {
return err
}
}
if len(*argFile) > 0 {
content, err := ioutil.ReadFile(*argFile)
if err != nil {
return err
}
queries, err := parseQueries(string(content))
if err != nil {
return err
}
for _, query := range queries {
console.Println("Exec query", query)
response, err := client.SendQuery(query)
if err != nil {
return err
}
if response != nil && !response.IsSuccessful() {
console.Printlnf("Query failed: %s", response.ErrorMsg())
break
}
}
} else {
return runCLE(client)
}
return nil
}(); err != nil {
console.Printlnf("FATAL: %s", err.Error())
os.Exit(1)
}
}
func retrieveEnvironment(envReader *env.Reader) (environment, error) {
var err error
var env environment
if len(*argEnvironment) > 0 {
err = envReader.CreateOrReadEnvironment(*argEnvironment, &env)
} else if len(*argAddress) == 0 {
err = envReader.SelectEnvironment(&env)
}
if err != nil {
return environment{}, err
}
if len(*argAddress) > 0 {
env.Address = *argAddress
}
if len(*argUser) > 0 {
env.User = *argUser
}
if len(*argPassword) > 0 {
env.Password = *argPassword
}
if len(env.User) > 0 && len(env.Password) == 0 {
var err error
console.Printlnf("Please enter RRI password for user %q", env.User)
console.Print("> ")
env.Password, err = console.ReadPassword()
if err != nil {
return environment{}, err
}
}
return env, nil
}
func enterEnvironment(envName string, env interface{}) error {
e, ok := env.(*environment)
if !ok {
panic(fmt.Sprintf("environment has unexpected type %T", env))
}
var err error
console.Print("Address (Host:Port)> ")
e.Address, err = console.ReadLine()
if err != nil {
return err
}
console.Print("User> ")
e.User, err = console.ReadLine()
if err != nil {
return err
}
console.Print("Password> ")
e.Password, err = console.ReadPassword()
if err != nil {
return err
}
return nil
}
func getEnvTitle(envName, envFile string) string {
data, err := ioutil.ReadFile(envFile)
if err != nil {
return envName
}
type envPreview struct {
Address string `json:"address"`
User string `json:"user"`
}
var env envPreview
if err := json.Unmarshal(data, &env); err != nil {
return envName
}
var suffix string
if len(env.User) > 0 {
suffix = fmt.Sprintf(" (%s@%s)", env.User, env.Address)
} else {
suffix = fmt.Sprintf(" (%s)", env.Address)
}
return fmt.Sprintf("%s%s", envName, suffix)
}
// parseQueries parses multiple queries separated by a =-= line from a string.
func parseQueries(str string) ([]*rri.Query, error) {
lines := strings.Split(str, "\n")
// each string in queryStrings contains a single, unparsed query
queryStrings := make([]string, 0)
appendQueryString := func(str string) {
str = strings.TrimSpace(str)
if len(str) > 0 {
queryStrings = append(queryStrings, str)
}
}
// separate at lines beginning with =-=
var sb strings.Builder
for _, line := range lines {
if strings.HasPrefix(line, "=-=") {
appendQueryString(sb.String())
sb.Reset()
} else {
sb.WriteString(line)
sb.WriteString("\n")
}
}
if sb.Len() > 0 {
appendQueryString(sb.String())
}
queries := make([]*rri.Query, len(queryStrings))
for i, queryString := range queryStrings {
query, err := rri.ParseQueryKV(strings.TrimSpace(queryString))
if err != nil {
return nil, err
}
queries[i] = query
}
return queries, nil
}