-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathls.go
314 lines (261 loc) · 8.02 KB
/
ls.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package main
import (
"bufio"
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/pipeviz/pvc/Godeps/_workspace/src/github.com/pipeviz/pipeviz/schema"
"github.com/pipeviz/pvc/Godeps/_workspace/src/github.com/pipeviz/pipeviz/types/semantic"
"github.com/pipeviz/pvc/Godeps/_workspace/src/github.com/spf13/cobra"
gjs "github.com/pipeviz/pvc/Godeps/_workspace/src/github.com/xeipuuv/gojsonschema"
)
// TODO we just use this as a way to namespace common names
type lsCmd struct{}
func lsCommand() *cobra.Command {
lsc := lsCmd{}
cmd := &cobra.Command{
Use: "ls [-n|--no-detect]",
Short: "Generates a pipeviz message describing a logic state.",
Long: "Generates a valid message describing a logic state from user input, then sends the message to a target pipeviz server.",
Run: lsc.runGenLS,
}
var nodetect bool
cmd.Flags().BoolVarP(&nodetect, "no-detect", "n", false, "Skip automated detection of suggested values.")
return cmd
}
// runGenLS is the main entry point for running the logic state-generating
// ls subcommand.
func (lsc lsCmd) runGenLS(cmd *cobra.Command, args []string) {
ls := &semantic.LogicState{}
if !cmd.Flags().Lookup("no-detect").Changed {
*ls = lsc.detectDefaults()
}
// Write directly to stdout, at least for now
w := os.Stdout
// Prep schema to validate the messages as we go
raw, err := schema.Master()
if err != nil {
fmt.Fprintln(w, "WARNING: Failed to open master schema file; pvc cannot validate outgoing messages.")
}
schemaMaster, err = gjs.NewSchema(gjs.NewStringLoader(string(raw)))
if err != nil {
panic("bad schema...?")
}
client := http.Client{Timeout: 5 * time.Second}
fmt.Fprintln(w, "Generating a logic state message...")
reader := bufio.NewReader(os.Stdin)
MenuLoop:
for {
fmt.Fprintf(w, "\n")
lsc.printCurrentState(w, *ls)
var input string
for {
fmt.Fprintf(w, "\nSelect a value to edit by number, (p)rint current JSON message, (s)end, or (q)uit: ")
l, err := fmt.Fscanln(reader, &input)
if l > 1 || err != nil {
continue
}
switch input {
case "q", "quit":
fmt.Fprintf(w, "\nQuitting; message was not sent\n")
os.Exit(1)
case "s", "send":
msg, err := toJSONBytes(*ls)
if err != nil {
log.Fatalf("\nFailed to marshal JSON of logic state object, no message sent\n")
}
resp, err := client.Post(cmd.Flags().Lookup("target").Value.String(), "application/json", bytes.NewReader(msg))
if err != nil {
log.Fatalf(err.Error())
}
bod, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Fatalf(err.Error())
}
if resp.StatusCode >= 200 && resp.StatusCode <= 300 {
fmt.Printf("Message accepted (HTTP code %v), msgid %v\n", resp.StatusCode, string(bod))
} else {
fmt.Printf("Message was rejected with HTTP code %v and message %v\n", resp.StatusCode, string(bod))
}
break MenuLoop
case "p", "print":
byts, err := toJSONBytes(*ls)
if err != nil {
fmt.Fprintf(w, "Error while generating JSON for printing: %q", err.Error())
continue MenuLoop
}
var prettied bytes.Buffer
err = json.Indent(&prettied, byts, "", " ")
if err != nil {
fmt.Fprintf(w, "Error while generating JSON for printing: %q", err.Error())
continue MenuLoop
}
fmt.Fprintf(w, "\nMessage that will be sent to %s:\n", cmd.Flags().Lookup("target").Value)
prettied.WriteTo(w)
w.WriteString("\n")
default:
num, interr := strconv.Atoi(input)
if interr != nil {
continue
} else if 0 < num && num < 10 {
switch num {
case 1:
lsc.collectPath(w, reader, ls)
case 2:
lsc.collectHostFQDN(w, reader, ls)
case 3:
lsc.collectHostNick(w, reader, ls)
case 4:
lsc.collectCommit(w, reader, ls)
case 5:
lsc.collectVersion(w, reader, ls)
case 6:
lsc.collectSemver(w, reader, ls)
case 7:
lsc.collectLgroup(w, reader, ls)
case 8:
lsc.collectNick(w, reader, ls)
case 9:
lsc.collectType(w, reader, ls)
}
continue MenuLoop
} else {
continue
}
}
}
}
}
func (lsc lsCmd) collectPath(w io.Writer, r io.Reader, ls *semantic.LogicState) {
fmt.Fprintf(w, "\n\nEditing Path\nCurrent Value: %q\n", ls.Path)
fmt.Fprint(w, "\nNew value: ")
scn := bufio.NewScanner(r)
for {
scn.Scan()
path := scn.Text()
f, err := os.Open(filepath.Clean(path))
if err == nil {
ls.Path = f.Name()
stat, _ := f.Stat()
if stat.IsDir() {
ls.Type = "code"
} else if strings.HasSuffix(f.Name(), ".so") {
ls.Type = "library"
} else {
ls.Type = "binary"
}
break
} else {
fmt.Fprintf(w, "\n%s does not exist.\n\nNew value: ", path)
}
}
}
func (lsc lsCmd) collectHostFQDN(w io.Writer, r io.Reader, ls *semantic.LogicState) {
fmt.Fprintf(w, "\n\nEditing Host FQDN\nCurrent Value: %q\n", ls.Environment.Address.Hostname)
fmt.Fprint(w, "\nNew value: ")
scn := bufio.NewScanner(r)
scn.Scan()
ls.Environment.Address.Hostname = scn.Text()
}
func (lsc lsCmd) collectHostNick(w io.Writer, r io.Reader, ls *semantic.LogicState) {
fmt.Fprintf(w, "\n\nEditing Host Nick\nCurrent Value: %q\n", ls.Environment.Nick)
fmt.Fprint(w, "\nNew value: ")
scn := bufio.NewScanner(r)
scn.Scan()
ls.Environment.Nick = scn.Text()
}
func (lsc lsCmd) collectCommit(w io.Writer, r io.Reader, ls *semantic.LogicState) {
fmt.Fprintf(w, "\n\nEditing Git commit\nCurrent Value: %q\n", ls.ID.CommitStr)
fmt.Fprint(w, "\nNew value: ")
for {
scn := bufio.NewScanner(r)
scn.Scan()
commit := scn.Text()
byts, err := hex.DecodeString(commit)
if err != nil || len(byts) != 20 {
fmt.Fprintf(w, "\n%s is not a valid Git commit.\n\nNew value: ", commit)
} else {
ls.ID.CommitStr = commit
break
}
}
}
func (lsc lsCmd) collectVersion(w io.Writer, r io.Reader, ls *semantic.LogicState) {
fmt.Fprintf(w, "\n\nEditing Version\nCurrent Value: %q\n", ls.ID.Version)
fmt.Fprint(w, "\nNew value: ")
scn := bufio.NewScanner(r)
scn.Scan()
ls.ID.Version = scn.Text()
}
func (lsc lsCmd) collectSemver(w io.Writer, r io.Reader, ls *semantic.LogicState) {
fmt.Fprintf(w, "\n\nEditing Semver\nCurrent Value: %q\n", ls.ID.Semver)
fmt.Fprint(w, "\nNew value: ")
scn := bufio.NewScanner(r)
scn.Scan()
ls.ID.Semver = scn.Text()
}
func (lsc lsCmd) collectLgroup(w io.Writer, r io.Reader, ls *semantic.LogicState) {
fmt.Fprintf(w, "\n\nEditing Logical group\nCurrent Value: %q\n", ls.Lgroup)
fmt.Fprint(w, "\nNew value: ")
scn := bufio.NewScanner(r)
scn.Scan()
ls.Lgroup = scn.Text()
}
func (lsc lsCmd) collectNick(w io.Writer, r io.Reader, ls *semantic.LogicState) {
fmt.Fprintf(w, "\n\nEditing Logical group\nCurrent Value: %q\n", ls.Nick)
fmt.Fprint(w, "\nNew value: ")
scn := bufio.NewScanner(r)
scn.Scan()
ls.Nick = scn.Text()
}
func (lsc lsCmd) collectType(w io.Writer, r io.Reader, ls *semantic.LogicState) {
fmt.Fprintf(w, "\n\nEditing Logical group\nCurrent Value: %q\n", ls.Type)
fmt.Fprint(w, "\nNew value: ")
scn := bufio.NewScanner(r)
scn.Scan()
ls.Type = scn.Text()
}
func (lsc lsCmd) detectDefaults() (ls semantic.LogicState) {
var err error
ls.Environment.Address.Hostname, err = os.Hostname()
if err != nil {
ls.Environment.Address.Hostname = ""
}
return ls
}
// printMenu prints to stdout a menu showing the current data in the
// message to be generated.
func (lsc lsCmd) printCurrentState(w io.Writer, e semantic.LogicState) {
fmt.Fprintln(w, "Logic state data:")
var n int
n++
fmt.Fprintf(w, " %v. Path: %q\n", n, e.Path)
n++
fmt.Fprintf(w, " %v. Host FQDN: %q\n", n, e.Environment.Address.Hostname)
n++
fmt.Fprintf(w, " %v. Host Nick: %q\n", n, e.Environment.Nick)
n++
fmt.Fprintf(w, " %v. Git commit: %q\n", n, e.ID.CommitStr)
n++
fmt.Fprintf(w, " %v. Version: %q\n", n, e.ID.Version)
n++
fmt.Fprintf(w, " %v. Semver: %q\n", n, e.ID.Semver)
n++
fmt.Fprintf(w, " %v. Logical group: %q\n", n, e.Lgroup)
n++
fmt.Fprintf(w, " %v. Nick: %q\n", n, e.Nick)
n++
fmt.Fprintf(w, " %v. Type: %q\n", n, e.Type)
validateAndPrint(w, e)
}