/
component.go
348 lines (325 loc) · 8.94 KB
/
component.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package api
import (
"encoding/json"
"fmt"
"io"
"log"
"net/url"
"os"
"strings"
"github.com/agilestacks/hub/cmd/hub/config"
"github.com/agilestacks/hub/cmd/hub/util"
)
const componentsResource = "hub/api/v1/components"
func Components(selector string, onlyCustomComponents, jsonFormat bool) {
components, err := componentsBy(selector, onlyCustomComponents)
if err != nil {
log.Fatalf("Unable to query for Component(s): %v", err)
}
if len(components) == 0 {
if jsonFormat {
log.Print("No Components")
} else {
fmt.Print("No Components\n")
}
} else {
if jsonFormat {
var toMarshal interface{}
if len(components) == 1 {
toMarshal = &components[0]
} else {
toMarshal = components
}
out, err := json.MarshalIndent(toMarshal, "", " ")
if err != nil {
log.Fatalf("Error marshalling JSON response for output: %v", err)
}
os.Stdout.Write(out)
os.Stdout.Write([]byte("\n"))
} else {
errors := make([]error, 0)
for _, component := range components {
errors = formatComponentEntity(&component, errors)
}
if len(errors) > 0 {
fmt.Print("Errors encountered:\n")
for _, err := range errors {
fmt.Printf("\t%v\n", err)
}
}
}
}
}
func formatComponentEntity(component *Component, errors []error) []error {
fmt.Printf("\n\t%s\n", formatComponentTitle(component))
if component.Description != "" {
fmt.Printf("\t\tDescription: %s\n", strings.Trim(component.Description, "\r\n "))
}
if len(component.Tags) > 0 {
fmt.Printf("\t\tTags: %s\n", strings.Join(component.Tags, ", "))
}
if component.Category != "" {
fmt.Printf("\t\tCategory: %s\n", component.Category)
}
if component.License != "" {
fmt.Printf("\t\tLicense: %s\n", component.License)
}
if component.Icon != "" {
fmt.Printf("\t\tIcon: %s\n", util.Wrap(component.Icon))
}
if component.Template != nil {
fmt.Printf("\t\tTemplate: %s\n", formatTemplateRef(component.Template))
}
if component.Git != nil {
fmt.Printf("\t\tGit: %s\n", formatComponentGitRef(component.Git))
}
if component.Version != "" {
fmt.Printf("\t\tVersion: %s\n", component.Version)
}
if component.Maturity != "" {
fmt.Printf("\t\tMaturity: %s\n", component.Maturity)
}
if len(component.Requires) > 0 {
fmt.Printf("\t\tRequires: %s\n", strings.Join(component.Requires, ", "))
}
if len(component.Provides) > 0 {
fmt.Printf("\t\tProvides: %s\n", strings.Join(component.Provides, ", "))
}
if len(component.TeamsPermissions) > 0 {
formatted := formatTeams(component.TeamsPermissions)
fmt.Printf("\t\tTeams: %s\n", formatted)
}
return errors
}
func formatComponentTitle(component *Component) string {
brief := ""
if component.Brief != "" {
brief = " - " + component.Brief
}
id := ""
if component.Id != "" {
brief = fmt.Sprintf(" [%s]", component.Id)
}
return fmt.Sprintf("%s%s%s", component.QName, id, brief)
}
func formatComponentGitRef(ref *ComponentGitRef) string {
subDir := ""
if ref.SubDir != "" {
subDir = "/" + ref.SubDir
}
return fmt.Sprintf("%s%s", ref.Remote, subDir)
}
func formatComponent(component *Component) {
errors := formatComponentEntity(component, make([]error, 0))
if len(errors) > 0 {
fmt.Print("Errors encountered formatting response:\n")
for _, err := range errors {
fmt.Printf("\t%v\n", err)
}
}
}
func componentBy(selector string) (*Component, error) {
if !util.IsUint(selector) {
return componentByName(selector)
}
return componentById(selector)
}
func componentsBy(selector string, onlyCustomComponents bool) ([]Component, error) {
if !util.IsUint(selector) {
return componentsByName(selector, onlyCustomComponents)
}
component, err := componentById(selector)
if err != nil {
return nil, err
}
if component != nil {
return []Component{*component}, nil
}
return nil, nil
}
func componentById(id string) (*Component, error) {
path := fmt.Sprintf("%s/%s", componentsResource, url.PathEscape(id))
var jsResp Component
code, err := get(hubApi(), path, &jsResp)
if code == 404 {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("Error querying SuperHub Components: %v", err)
}
if code != 200 {
return nil, fmt.Errorf("Got %d HTTP querying SuperHub Components, expected 200 HTTP", code)
}
return &jsResp, nil
}
func componentByName(name string) (*Component, error) {
components, err := componentsByName(name, false)
if err != nil {
return nil, fmt.Errorf("Unable to query for Component `%s`: %v", name, err)
}
if len(components) == 0 {
return nil, fmt.Errorf("No Component `%s` found", name)
}
if len(components) > 1 {
return nil, fmt.Errorf("More than one Component returned by name `%s`", name)
}
component := components[0]
return &component, nil
}
func componentsByName(name string, onlyCustomComponents bool) ([]Component, error) {
path := componentsResource
var filters []string
if name != "" {
filters = append(filters, "qname="+url.QueryEscape(name))
}
if onlyCustomComponents {
filters = append(filters, "kind=custom")
}
if len(filters) > 0 {
path += "?" + strings.Join(filters, "&")
}
var jsResp []Component
code, err := get(hubApi(), path, &jsResp)
if code == 404 {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("Error querying SuperHub Components: %v", err)
}
if code != 200 {
return nil, fmt.Errorf("Got %d HTTP querying SuperHub Components, expected 200 HTTP", code)
}
return jsResp, nil
}
func CreateComponent(req ComponentRequest) {
component, err := createComponent(req)
if err != nil {
log.Fatalf("Unable to create SuperHub Component: %v", err)
}
formatComponent(component)
}
func createComponent(req ComponentRequest) (*Component, error) {
if req.Template != "" && !util.IsUint(req.Template) {
template, err := templateByName(req.Template)
if err != nil {
return nil, err
}
req.Template = template.Id
}
var jsResp Component
code, err := post(hubApi(), componentsResource, &req, &jsResp)
if err != nil {
return nil, err
}
if code != 200 && code != 201 {
return nil, fmt.Errorf("Got %d HTTP creating SuperHub Component, expected [200, 201] HTTP", code)
}
return &jsResp, nil
}
func RawCreateComponent(body io.Reader) {
component, err := rawCreateComponent(body)
if err != nil {
log.Fatalf("Unable to create SuperHub Component: %v", err)
}
formatComponent(component)
}
func rawCreateComponent(body io.Reader) (*Component, error) {
var jsResp Component
code, err := post2(hubApi(), componentsResource, body, &jsResp)
if err != nil {
return nil, err
}
if code != 200 && code != 201 {
return nil, fmt.Errorf("Got %d HTTP creating SuperHub Component, expected [200, 201] HTTP", code)
}
return &jsResp, nil
}
func DeleteComponent(selector string) {
err := deleteComponent(selector)
if err != nil {
log.Fatalf("Unable to delete SuperHub Component: %v", err)
}
}
func deleteComponent(selector string) error {
component, err := componentBy(selector)
id := ""
if err != nil {
str := err.Error()
if util.IsUint(selector) &&
(strings.Contains(str, "json: cannot unmarshal") || strings.Contains(str, "cannot parse") || config.Force) {
util.Warn("%v", err)
id = selector
} else {
return err
}
} else if component == nil {
return error404
} else {
id = component.Id
}
force := ""
if config.Force {
force = "?force=true"
}
path := fmt.Sprintf("%s/%s%s", componentsResource, url.PathEscape(id), force)
code, err := delete(hubApi(), path)
if err != nil {
return err
}
if code != 202 && code != 204 {
return fmt.Errorf("Got %d HTTP deleting SuperHub Component, expected [202, 204] HTTP", code)
}
return nil
}
func PatchComponent(selector string, change ComponentPatch) {
component, err := patchComponent(selector, change)
if err != nil {
log.Fatalf("Unable to patch SuperHub Component: %v", err)
}
formatComponent(component)
}
func patchComponent(selector string, change ComponentPatch) (*Component, error) {
component, err := componentBy(selector)
if err != nil {
return nil, err
}
if component == nil {
return nil, error404
}
path := fmt.Sprintf("%s/%s", componentsResource, url.PathEscape(component.Id))
var jsResp Component
code, err := patch(hubApi(), path, &change, &jsResp)
if err != nil {
return nil, err
}
if code != 200 {
return nil, fmt.Errorf("Got %d HTTP patching SuperHub Component, expected 200 HTTP", code)
}
return &jsResp, nil
}
func RawPatchComponent(selector string, body io.Reader) {
component, err := rawPatchComponent(selector, body)
if err != nil {
log.Fatalf("Unable to patch SuperHub Component: %v", err)
}
formatComponent(component)
}
func rawPatchComponent(selector string, body io.Reader) (*Component, error) {
component, err := componentBy(selector)
if err != nil {
return nil, err
}
if component == nil {
return nil, error404
}
path := fmt.Sprintf("%s/%s", componentsResource, url.PathEscape(component.Id))
var jsResp Component
code, err := patch2(hubApi(), path, body, &jsResp)
if err != nil {
return nil, err
}
if code != 200 {
return nil, fmt.Errorf("Got %d HTTP patching SuperHub Component, expected 200 HTTP", code)
}
return &jsResp, nil
}