-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpvc.go
103 lines (84 loc) · 2.44 KB
/
pvc.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"github.com/pipeviz/pvc/Godeps/_workspace/src/github.com/pipeviz/pipeviz/types/semantic"
"github.com/pipeviz/pvc/Godeps/_workspace/src/github.com/spf13/cobra"
"github.com/pipeviz/pvc/Godeps/_workspace/src/github.com/xeipuuv/gojsonschema"
)
type menuLevel interface {
Info() ([]byte, error)
Prompt() ([]byte, error)
Accept(string) error
Next(*cliRunner) *cliRunner
}
// cliRunner coordinates control over and interaction with a level
// of interaction in the UI
type cliRunner struct {
parent *cliRunner
obj menuLevel
w io.Writer
}
func main() {
root := &cobra.Command{Use: "pvc"}
root.AddCommand(envCommand())
root.AddCommand(lsCommand())
var target string
root.PersistentFlags().StringVarP(&target, "target", "t", "http://localhost:2309", "Address of the target pipeviz daemon.")
root.Execute()
}
// wrapForJSON converts data into a map that will serialize
// appropriate pipeviz message JSON.
func wrapForJSON(v interface{}) map[string]interface{} {
m := make(map[string]interface{})
switch obj := v.(type) {
case semantic.Environment:
m["environments"] = []semantic.Environment{obj}
case semantic.LogicState:
m["logic-states"] = []semantic.LogicState{obj}
}
return m
}
func toJSONBytes(v interface{}) ([]byte, error) {
// Convert the data to a map that will write out the correct JSON
m := wrapForJSON(v)
msg, err := json.Marshal(m)
if err != nil {
return nil, fmt.Errorf("\nError while marshaling data to JSON for validation: %s\n", err.Error())
}
return msg, nil
}
func validateAndPrint(w io.Writer, v interface{}) {
msg, err := toJSONBytes(v)
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
// Validate the current state of the message
result, err := schemaMaster.Validate(gojsonschema.NewStringLoader(string(msg)))
if err != nil {
fmt.Fprintf(w, "\nError while attempting to validate data: %s\n", err.Error())
return
}
if !result.Valid() {
fmt.Fprintln(w, "\nAs it stands now, the data will fail validation if sent to a pipeviz server. Errors:")
for _, desc := range result.Errors() {
fmt.Fprintf(w, "\t%s\n", desc)
}
}
}
func runCreate(cmd *cobra.Command, args []string) {
// Create the root runner
//cr := &cliRunner{
//w: os.Stdout,
//}
}
type mainMenu struct {
}
func (m *mainMenu) Info() ([]byte, error) {
var b bytes.Buffer
b.WriteString("Which type of state would you like to describe to pipeviz: ")
return b.Bytes(), nil
}