-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathset.go
161 lines (138 loc) · 3.23 KB
/
set.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
package jsonconsul
import (
"encoding/json"
"flag"
"fmt"
"os"
)
const (
Undefined int = iota
Bool
Number
String
Array
Object
)
type JsonSet struct {
// The Consul Key to set.
Key string
// The value that was passed by the command line.
Value string
// What we expect the value type to be.
ExpectedType int
// The actual Json Value that is going to be saved to Consul.
JsonValue []byte
// Is the value going to be json.
IsJsonValue bool
}
func (js *JsonSet) setExpectedType(t string) {
switch t {
case "bool":
js.ExpectedType = Bool
case "number":
js.ExpectedType = Number
case "int":
js.ExpectedType = Number
case "float":
js.ExpectedType = Number
case "string":
js.ExpectedType = String
case "array":
js.ExpectedType = Array
case "object":
js.ExpectedType = Object
default:
js.ExpectedType = Undefined
}
}
func (js *JsonSet) expectedType() string {
switch js.ExpectedType {
case Bool:
return "bool"
case Number:
return "number"
case String:
return "string"
case Array:
return "array"
case Object:
return "object"
}
return "undefined"
}
func (js *JsonSet) checkExpectedType(unmarshalled interface{}) error {
if js.ExpectedType == Undefined {
return nil
}
switch unmarshalled.(type) {
case bool:
if js.ExpectedType != Bool {
return fmt.Errorf("Invalid type. Value is a bool. Expected %s", js.expectedType())
}
case float64:
if js.ExpectedType != Number {
return fmt.Errorf("Invalid type. Value is a number. Expected %s", js.expectedType())
}
case string:
if js.ExpectedType != String {
return fmt.Errorf("Invalid type. Value is a string. Expected %s", js.expectedType())
}
case []interface{}:
if js.ExpectedType != Array {
return fmt.Errorf("Invalid type. Value is an array. Expected %s", js.expectedType())
}
case map[string]interface{}:
if js.ExpectedType != Object {
return fmt.Errorf("Invalid type. Value is an object. Expected %s", js.expectedType())
}
}
return nil
}
func (js *JsonSet) ParseFlags(args []string) {
flags := flag.NewFlagSet(Name, flag.ContinueOnError)
expectedType := flags.String("expected-type", "undefined", "What is the expected type for the value? bool, int, float, string, object, array")
flags.BoolVar(&js.IsJsonValue, "json-value", true, "Is the value going to be set as json")
flags.Parse(args)
js.setExpectedType(*expectedType)
leftovers := flags.Args()
if len(leftovers) < 2 {
fmt.Println("jsonconsul set full/key/path \"true\"")
os.Exit(-1)
}
js.Key = leftovers[0]
js.Value = leftovers[1]
}
func (js *JsonSet) lintedJson() ([]byte, error) {
var (
unmarshalled interface{}
)
err := json.Unmarshal([]byte(js.Value), &unmarshalled)
if err != nil {
return nil, fmt.Errorf("Can't set the key %s invalid value: %s\nIf it is a string please wrap the value with quotes. Ex. \\\"value\\\"", js.Key, js.Value)
}
err = js.checkExpectedType(unmarshalled)
if err != nil {
return nil, err
}
return []byte(js.Value), nil
}
func (js *JsonSet) Run() error {
var (
err error
val []byte
)
if js.IsJsonValue {
js.JsonValue, err = js.lintedJson()
if err != nil {
return err
}
val = js.JsonValue
} else {
val = []byte(js.Value)
}
err = setConsulKV(js.Key, val)
if err != nil {
return err
}
return nil
}