forked from fnproject/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapps.go
396 lines (335 loc) · 8.61 KB
/
apps.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"context"
"strings"
client "github.com/fnproject/cli/client"
fnclient "github.com/fnproject/fn_go/client"
apiapps "github.com/fnproject/fn_go/client/apps"
"github.com/fnproject/fn_go/models"
"github.com/jmoiron/jsonq"
"github.com/urfave/cli"
)
type appsCmd struct {
client *fnclient.Fn
}
func apps() cli.Command {
a := appsCmd{client: client.APIClient()}
return cli.Command{
Name: "apps",
Usage: "manage applications",
Subcommands: []cli.Command{
{
Name: "create",
Aliases: []string{"c"},
Usage: "create a new app",
ArgsUsage: "<app>",
Action: a.create,
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "config",
Usage: "application configuration",
},
},
},
{
Name: "inspect",
Aliases: []string{"i"},
Usage: "retrieve one or all apps properties",
ArgsUsage: "<app> [property.[key]]",
Action: a.inspect,
},
{
Name: "update",
Aliases: []string{"u"},
Usage: "update an `app`",
ArgsUsage: "<app>",
Action: a.update,
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "config,c",
Usage: "route configuration",
},
},
},
{
Name: "config",
Usage: "manage your apps's function configs",
Subcommands: []cli.Command{
{
Name: "set",
Aliases: []string{"s"},
Usage: "store a configuration key for this application",
ArgsUsage: "<app> <key> <value>",
Action: a.configSet,
},
{
Name: "get",
Aliases: []string{"g"},
Usage: "inspect configuration key for this application",
ArgsUsage: "<app> <key>",
Action: a.configGet,
},
{
Name: "list",
Aliases: []string{"l"},
Usage: "list configuration key/value pairs for this application",
ArgsUsage: "<app>",
Action: a.configList,
},
{
Name: "unset",
Aliases: []string{"u"},
Usage: "remove a configuration key for this application",
ArgsUsage: "<app> <key>",
Action: a.configUnset,
},
},
},
{
Name: "list",
Aliases: []string{"l"},
Usage: "list all apps",
Action: a.list,
Flags: []cli.Flag{
cli.StringFlag{
Name: "cursor",
Usage: "pagination cursor",
},
cli.Int64Flag{
Name: "n",
Usage: "number of apps to return",
Value: int64(100),
},
},
},
{
Name: "delete",
Aliases: []string{"d"},
Usage: "delete an app",
Action: a.delete,
},
},
}
}
func (a *appsCmd) list(c *cli.Context) error {
params := &apiapps.GetAppsParams{Context: context.Background()}
var resApps []*models.App
for {
resp, err := a.client.Apps.GetApps(params)
if err != nil {
switch e := err.(type) {
case *apiapps.GetAppsAppNotFound:
return fmt.Errorf("%v", e.Payload.Error.Message)
case *apiapps.GetAppsAppDefault:
return fmt.Errorf("%v", e.Payload.Error.Message)
case *apiapps.GetAppsDefault:
// this is the one getting called, not sure what the one above is?
return fmt.Errorf("%v", e.Payload.Error.Message)
default:
return fmt.Errorf("%v", err)
}
}
resApps = append(resApps, resp.Payload.Apps...)
n := c.Int64("n")
if n < 0 {
return errors.New("number of calls: negative value not allowed")
}
howManyMore := n - int64(len(resApps)+len(resp.Payload.Apps))
if howManyMore <= 0 || resp.Payload.NextCursor == "" {
break
}
params.Cursor = &resp.Payload.NextCursor
}
if len(resApps) == 0 {
fmt.Println("no apps found")
return nil
}
for _, app := range resApps {
fmt.Println(app.Name)
}
return nil
}
func (a *appsCmd) create(c *cli.Context) error {
body := &models.AppWrapper{App: &models.App{
Name: c.Args().Get(0),
Config: extractEnvConfig(c.StringSlice("config")),
}}
resp, err := a.client.Apps.PostApps(&apiapps.PostAppsParams{
Context: context.Background(),
Body: body,
})
if err != nil {
switch e := err.(type) {
case *apiapps.PostAppsBadRequest:
return fmt.Errorf("%v", e.Payload.Error.Message)
case *apiapps.PostAppsConflict:
return fmt.Errorf("%v", e.Payload.Error.Message)
case *apiapps.PostAppsDefault:
return fmt.Errorf("%v", e.Payload.Error.Message)
default:
return fmt.Errorf("%v", err)
}
}
fmt.Println("Successfully created app: ", resp.Payload.App.Name)
return nil
}
func (a *appsCmd) update(c *cli.Context) error {
appName := c.Args().First()
patchedApp := &models.App{
Config: extractEnvConfig(c.StringSlice("config")),
}
err := a.patchApp(appName, patchedApp)
if err != nil {
return err
}
fmt.Println("app", appName, "updated")
return nil
}
func (a *appsCmd) configSet(c *cli.Context) error {
appName := c.Args().Get(0)
key := c.Args().Get(1)
value := c.Args().Get(2)
app := &models.App{
Config: make(map[string]string),
}
app.Config[key] = value
if err := a.patchApp(appName, app); err != nil {
return fmt.Errorf("error updating app configuration: %v", err)
}
fmt.Println(appName, "updated", key, "with", value)
return nil
}
func (a *appsCmd) configGet(c *cli.Context) error {
appName := c.Args().Get(0)
key := c.Args().Get(1)
resp, err := a.client.Apps.GetAppsApp(&apiapps.GetAppsAppParams{
App: appName,
Context: context.Background(),
})
if err != nil {
return err
}
val, ok := resp.Payload.App.Config[key]
if !ok {
return fmt.Errorf("config key does not exist")
}
fmt.Println(val)
return nil
}
func (a *appsCmd) configList(c *cli.Context) error {
appName := c.Args().Get(0)
resp, err := a.client.Apps.GetAppsApp(&apiapps.GetAppsAppParams{
App: appName,
Context: context.Background(),
})
if err != nil {
return err
}
for key, val := range resp.Payload.App.Config {
fmt.Printf("%s=%s\n", key, val)
}
return nil
}
func (a *appsCmd) configUnset(c *cli.Context) error {
appName := c.Args().Get(0)
key := c.Args().Get(1)
app := &models.App{
Config: make(map[string]string),
}
app.Config[key] = ""
if err := a.patchApp(appName, app); err != nil {
return fmt.Errorf("error updating app configuration: %v", err)
}
fmt.Printf("removed key '%s' from app '%s' \n", key, appName)
return nil
}
func (a *appsCmd) patchApp(appName string, app *models.App) error {
_, err := a.client.Apps.PatchAppsApp(&apiapps.PatchAppsAppParams{
Context: context.Background(),
App: appName,
Body: &models.AppWrapper{App: app},
})
if err != nil {
switch e := err.(type) {
case *apiapps.PatchAppsAppBadRequest:
return errors.New(e.Payload.Error.Message)
case *apiapps.PatchAppsAppNotFound:
return errors.New(e.Payload.Error.Message)
case *apiapps.PatchAppsAppDefault:
return errors.New(e.Payload.Error.Message)
default:
return fmt.Errorf("%v", err)
}
}
return nil
}
func (a *appsCmd) inspect(c *cli.Context) error {
if c.Args().Get(0) == "" {
return errors.New("missing app name after the inspect command")
}
appName := c.Args().First()
prop := c.Args().Get(1)
resp, err := a.client.Apps.GetAppsApp(&apiapps.GetAppsAppParams{
Context: context.Background(),
App: appName,
})
if err != nil {
switch e := err.(type) {
case *apiapps.GetAppsAppNotFound:
return fmt.Errorf("%v", e.Payload.Error.Message)
case *apiapps.GetAppsAppDefault:
return fmt.Errorf("%v", e.Payload.Error.Message)
default:
return fmt.Errorf("%v", err)
}
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", "\t")
if prop == "" {
enc.Encode(resp.Payload.App)
return nil
}
// TODO: we really need to marshal it here just to
// unmarshal as map[string]interface{}?
data, err := json.Marshal(resp.Payload.App)
if err != nil {
return fmt.Errorf("could not marshal app: %v", err)
}
var inspect map[string]interface{}
err = json.Unmarshal(data, &inspect)
if err != nil {
return fmt.Errorf("could not unmarshal data: %v", err)
}
jq := jsonq.NewQuery(inspect)
field, err := jq.Interface(strings.Split(prop, ".")...)
if err != nil {
return fmt.Errorf("failed to inspect field %v", prop)
}
enc.Encode(field)
return nil
}
func (a *appsCmd) delete(c *cli.Context) error {
appName := c.Args().First()
if appName == "" {
return errors.New("app name required to delete")
}
_, err := a.client.Apps.DeleteAppsApp(&apiapps.DeleteAppsAppParams{
Context: context.Background(),
App: appName,
})
if err != nil {
switch e := err.(type) {
case *apiapps.DeleteAppsAppNotFound:
return errors.New(e.Payload.Error.Message)
case *apiapps.DeleteAppsAppDefault:
return errors.New(e.Payload.Error.Message)
}
return fmt.Errorf("%v", err)
}
fmt.Println("App", appName, "deleted")
return nil
}