/
schema.go
213 lines (186 loc) · 5.66 KB
/
schema.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
package client
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/asteris-llc/gestalt/web/app"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io/ioutil"
"log"
"path"
"time"
)
var (
// SchemaCmd is the wrapper command for schemas
SchemaCmd = &cobra.Command{
Use: "schema",
Short: "work with schemas",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
setupClient()
},
}
// SchemaListCmd lists schemas
SchemaListCmd = &cobra.Command{
Use: "list",
Short: "list schemas",
Long: "List schemas, optionally pretty-printed. In successful cases, this command prints JSON. This command corresponds to calling `GET /v1/schemas`.",
Run: func(cmd *cobra.Command, args []string) {
resp, err := client.Do(
"GET",
"/v1/schemas",
map[string]interface{}{},
nil,
)
if err != nil {
log.Fatal(err)
}
client.HandleResponse(resp)
},
}
// SchemaSubmitCmd submits a schema
SchemaSubmitCmd = &cobra.Command{
Use: "submit {schema.json}",
Short: "submit a schema",
Long: "Create or update a schema in the remote store. This is specified as `schema.json`, a path to a JSON file on disk. If the `--set-defaults` flag is set, the API will immediately set any defaults specified in the file. In successful cases, this command prints JSON equivalent to the schema which was persisted. This command corresponds to calling `POST /v1/schemas` with the given flag. The `--set-defaults` flag corresponds to the `setDefaults` query string option.",
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("expected exactly one JSON file as an argument")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
// read JSON
body, err := ioutil.ReadFile(args[0])
if err != nil {
log.Fatal(err)
}
var dest interface{}
err = json.Unmarshal(body, &dest)
if err != nil {
log.Fatal(err)
}
schema, err := app.LoadSchema(dest)
if err != nil {
log.Fatal(err)
}
err = schema.Validate()
if err != nil {
log.Fatal(err)
}
resp, err := client.Do(
"POST",
"/v1/schemas",
map[string]interface{}{
"setDefaults": viper.Get("set-defaults"),
},
bytes.NewBuffer(body),
)
if err != nil {
log.Fatal(err)
}
client.HandleResponse(resp)
},
}
// SchemaShowCmd shows a schema
SchemaShowCmd = &cobra.Command{
Use: "show {name}",
Short: "show a schema",
Long: "Show a schema, specified by `name`. In successful cases, this command prints JSON. This command corresponds to calling `GET /v1/schemas/{name}`.",
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("expected exactly one name as an argument")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
resp, err := client.Do(
"GET",
path.Join("/v1/schemas", args[0]),
map[string]interface{}{},
nil,
)
if err != nil {
log.Fatal(err)
}
client.HandleResponse(resp)
},
}
// SchemaDeleteCmd deletes a schema
SchemaDeleteCmd = &cobra.Command{
Use: "delete {name}",
Short: "delete a schema",
Long: "Delete an existing schema, specified by `name`. If the `--delete-keys` flag is set, the API will also delete any keys that are specified in the schema being deleted. In successful cases, this command does not print anything. This command corresponds to calling `DELETE /v1/schemas/{name}`. The `--delete-keys` flag corresponds to the `deleteKeys` query string option.",
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("expected exactly one schema name as an argument")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
resp, err := client.Do(
"DELETE",
path.Join("/v1/schemas", args[0]),
map[string]interface{}{
"deleteKeys": viper.Get("delete-keys"),
},
nil,
)
if err != nil {
log.Fatal(err)
}
client.HandleResponse(resp)
},
}
// SchemaValidateCmd submits a schema
SchemaValidateCmd = &cobra.Command{
Use: "validate {schema.json}",
Short: "validate a schema",
Long: "Validate the schema specified as `schema.json`, a path to a JSON file on disk. If it is valid, the command prints \"OK\". This command has no corresponding API call, but the creation/update logic on the server performs the same validation.",
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("expected exactly one JSON file as an argument")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
// read JSON
body, err := ioutil.ReadFile(args[0])
if err != nil {
log.Fatal(err)
}
var dest interface{}
err = json.Unmarshal(body, &dest)
if err != nil {
log.Fatal(err)
}
schema, err := app.LoadSchema(dest)
if err != nil {
log.Fatal(err)
}
err = schema.Validate()
if err != nil {
log.Fatal(err)
}
fmt.Println("OK")
},
}
)
// SchemaFlags sets up flags for the client commands
func SchemaFlags() {
SchemaCmd.PersistentFlags().String("scheme", "http", "set the request scheme")
SchemaCmd.PersistentFlags().String("host", "localhost:3000", "API hostname")
SchemaCmd.PersistentFlags().Duration("timeout", 20*time.Second, "set the request timeout")
SchemaCmd.PersistentFlags().Bool("pretty", true, "pretty-print responses")
SchemaSubmitCmd.Flags().Bool("set-defaults", false, "set defaults when submitting")
SchemaDeleteCmd.Flags().Bool("delete-keys", false, "also delete configuration keys")
// set up command hierarchy
SchemaCmd.AddCommand(
SchemaListCmd,
SchemaShowCmd,
SchemaSubmitCmd,
SchemaDeleteCmd,
SchemaValidateCmd,
)
}