forked from sacloud/usacloud
/
cli_auth_status_gen.go
403 lines (369 loc) · 11 KB
/
cli_auth_status_gen.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
397
398
399
400
401
402
403
// Code generated by 'github.com/sacloud/usacloud/tools/gen-cli-commands'; DO NOT EDIT
package cli
import (
"encoding/json"
"fmt"
"strings"
"github.com/imdario/mergo"
"github.com/sacloud/usacloud/command"
"github.com/sacloud/usacloud/command/completion"
"github.com/sacloud/usacloud/command/funcs"
"github.com/sacloud/usacloud/command/params"
"github.com/sacloud/usacloud/schema"
"gopkg.in/urfave/cli.v2"
)
func init() {
showParam := params.NewShowAuthStatusParam()
cliCommand := &cli.Command{
Name: "auth-status",
Usage: "A manage commands of AuthStatus",
Action: func(c *cli.Context) error {
comm := c.App.Command("show")
if comm != nil {
return comm.Action(c)
}
return cli.ShowSubcommandHelp(c)
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "param-template",
Usage: "Set input parameter from string(JSON)",
},
&cli.StringFlag{
Name: "param-template-file",
Usage: "Set input parameter from file",
},
&cli.BoolFlag{
Name: "generate-skeleton",
Usage: "Output skelton of parameter JSON",
},
&cli.StringFlag{
Name: "output-type",
Aliases: []string{"out"},
Usage: "Output type [table/json/csv/tsv]",
},
&cli.StringSliceFlag{
Name: "column",
Aliases: []string{"col"},
Usage: "Output columns(using when '--output-type' is in [csv/tsv] only)",
},
&cli.BoolFlag{
Name: "quiet",
Aliases: []string{"q"},
Usage: "Only display IDs",
},
&cli.StringFlag{
Name: "format",
Aliases: []string{"fmt"},
Usage: "Output format(see text/template package document for detail)",
},
&cli.StringFlag{
Name: "format-file",
Usage: "Output format from file(see text/template package document for detail)",
},
&cli.StringFlag{
Name: "query",
Usage: "JMESPath query(using when '--output-type' is json only)",
},
},
Subcommands: []*cli.Command{
{
Name: "show",
Usage: "Show AuthStatus (default)",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "param-template",
Usage: "Set input parameter from string(JSON)",
},
&cli.StringFlag{
Name: "param-template-file",
Usage: "Set input parameter from file",
},
&cli.BoolFlag{
Name: "generate-skeleton",
Usage: "Output skelton of parameter JSON",
},
&cli.StringFlag{
Name: "output-type",
Aliases: []string{"out"},
Usage: "Output type [table/json/csv/tsv]",
},
&cli.StringSliceFlag{
Name: "column",
Aliases: []string{"col"},
Usage: "Output columns(using when '--output-type' is in [csv/tsv] only)",
},
&cli.BoolFlag{
Name: "quiet",
Aliases: []string{"q"},
Usage: "Only display IDs",
},
&cli.StringFlag{
Name: "format",
Aliases: []string{"fmt"},
Usage: "Output format(see text/template package document for detail)",
},
&cli.StringFlag{
Name: "format-file",
Usage: "Output format from file(see text/template package document for detail)",
},
&cli.StringFlag{
Name: "query",
Usage: "JMESPath query(using when '--output-type' is json only)",
},
},
ShellComplete: func(c *cli.Context) {
if c.NArg() < 3 { // invalid args
return
}
if err := checkConfigVersion(); err != nil {
return
}
if err := applyConfigFromFile(c); err != nil {
return
}
// c.Args() == arg1 arg2 arg3 -- [cur] [prev] [commandName]
args := c.Args().Slice()
commandName := args[c.NArg()-1]
prev := args[c.NArg()-2]
cur := args[c.NArg()-3]
// set real args
realArgs := args[0 : c.NArg()-3]
// Validate global params
command.GlobalOption.Validate(false)
// set default output-type
// when params have output-type option and have empty value
var outputTypeHolder interface{} = showParam
if v, ok := outputTypeHolder.(command.OutputTypeHolder); ok {
if v.GetOutputType() == "" {
v.SetOutputType(command.GlobalOption.DefaultOutputType)
}
}
// build command context
ctx := command.NewContext(c, realArgs, showParam)
// Set option values
if c.IsSet("param-template") {
showParam.ParamTemplate = c.String("param-template")
}
if c.IsSet("param-template-file") {
showParam.ParamTemplateFile = c.String("param-template-file")
}
if c.IsSet("generate-skeleton") {
showParam.GenerateSkeleton = c.Bool("generate-skeleton")
}
if c.IsSet("output-type") {
showParam.OutputType = c.String("output-type")
}
if c.IsSet("column") {
showParam.Column = c.StringSlice("column")
}
if c.IsSet("quiet") {
showParam.Quiet = c.Bool("quiet")
}
if c.IsSet("format") {
showParam.Format = c.String("format")
}
if c.IsSet("format-file") {
showParam.FormatFile = c.String("format-file")
}
if c.IsSet("query") {
showParam.Query = c.String("query")
}
if strings.HasPrefix(prev, "-") {
// prev if flag , is values setted?
if strings.Contains(prev, "=") {
if strings.HasPrefix(cur, "-") {
completion.FlagNames(c, commandName)
return
} else {
completion.AuthStatusShowCompleteArgs(ctx, showParam, cur, prev, commandName)
return
}
}
// cleanup flag name
name := prev
for {
if !strings.HasPrefix(name, "-") {
break
}
name = strings.Replace(name, "-", "", 1)
}
// flag is exists? , is BoolFlag?
exists := false
for _, flag := range c.App.Command(commandName).Flags {
for _, n := range flag.Names() {
if n == name {
exists = true
break
}
}
if exists {
if _, ok := flag.(*cli.BoolFlag); ok {
if strings.HasPrefix(cur, "-") {
completion.FlagNames(c, commandName)
return
} else {
completion.AuthStatusShowCompleteArgs(ctx, showParam, cur, prev, commandName)
return
}
} else {
// prev is flag , call completion func of each flags
completion.AuthStatusShowCompleteFlags(ctx, showParam, name, cur)
return
}
}
}
// here, prev is wrong, so noop.
} else {
if strings.HasPrefix(cur, "-") {
completion.FlagNames(c, commandName)
return
} else {
completion.AuthStatusShowCompleteArgs(ctx, showParam, cur, prev, commandName)
return
}
}
},
Action: func(c *cli.Context) error {
if err := checkConfigVersion(); err != nil {
return err
}
if err := applyConfigFromFile(c); err != nil {
return err
}
showParam.ParamTemplate = c.String("param-template")
showParam.ParamTemplateFile = c.String("param-template-file")
strInput, err := command.GetParamTemplateValue(showParam)
if err != nil {
return err
}
if strInput != "" {
p := params.NewShowAuthStatusParam()
err := json.Unmarshal([]byte(strInput), p)
if err != nil {
return fmt.Errorf("Failed to parse JSON: %s", err)
}
mergo.Merge(showParam, p, mergo.WithOverride)
}
// Set option values
if c.IsSet("param-template") {
showParam.ParamTemplate = c.String("param-template")
}
if c.IsSet("param-template-file") {
showParam.ParamTemplateFile = c.String("param-template-file")
}
if c.IsSet("generate-skeleton") {
showParam.GenerateSkeleton = c.Bool("generate-skeleton")
}
if c.IsSet("output-type") {
showParam.OutputType = c.String("output-type")
}
if c.IsSet("column") {
showParam.Column = c.StringSlice("column")
}
if c.IsSet("quiet") {
showParam.Quiet = c.Bool("quiet")
}
if c.IsSet("format") {
showParam.Format = c.String("format")
}
if c.IsSet("format-file") {
showParam.FormatFile = c.String("format-file")
}
if c.IsSet("query") {
showParam.Query = c.String("query")
}
// Validate global params
if errors := command.GlobalOption.Validate(false); len(errors) > 0 {
return command.FlattenErrorsWithPrefix(errors, "GlobalOptions")
}
var outputTypeHolder interface{} = showParam
if v, ok := outputTypeHolder.(command.OutputTypeHolder); ok {
if v.GetOutputType() == "" {
v.SetOutputType(command.GlobalOption.DefaultOutputType)
}
}
// Generate skeleton
if showParam.GenerateSkeleton {
showParam.GenerateSkeleton = false
showParam.FillValueToSkeleton()
d, err := json.MarshalIndent(showParam, "", "\t")
if err != nil {
return fmt.Errorf("Failed to Marshal JSON: %s", err)
}
fmt.Fprintln(command.GlobalOption.Out, string(d))
return nil
}
// Validate specific for each command params
if errors := showParam.Validate(); len(errors) > 0 {
return command.FlattenErrorsWithPrefix(errors, "Options")
}
// create command context
ctx := command.NewContext(c, c.Args().Slice(), showParam)
// Run command with params
return funcs.AuthStatusShow(ctx, showParam)
},
},
},
}
// build Category-Resource mapping
AppendResourceCategoryMap("auth-status", &schema.Category{
Key: "auth",
DisplayName: "Authentication",
Order: 5,
})
// build Category-Command mapping
AppendCommandCategoryMap("auth-status", "show", &schema.Category{
Key: "default",
DisplayName: "",
Order: 2147483647,
})
// build Category-Param mapping
AppendFlagCategoryMap("auth-status", "show", "column", &schema.Category{
Key: "output",
DisplayName: "Output options",
Order: 2147483637,
})
AppendFlagCategoryMap("auth-status", "show", "format", &schema.Category{
Key: "output",
DisplayName: "Output options",
Order: 2147483637,
})
AppendFlagCategoryMap("auth-status", "show", "format-file", &schema.Category{
Key: "output",
DisplayName: "Output options",
Order: 2147483637,
})
AppendFlagCategoryMap("auth-status", "show", "generate-skeleton", &schema.Category{
Key: "Input",
DisplayName: "Input options",
Order: 2147483627,
})
AppendFlagCategoryMap("auth-status", "show", "output-type", &schema.Category{
Key: "output",
DisplayName: "Output options",
Order: 2147483637,
})
AppendFlagCategoryMap("auth-status", "show", "param-template", &schema.Category{
Key: "Input",
DisplayName: "Input options",
Order: 2147483627,
})
AppendFlagCategoryMap("auth-status", "show", "param-template-file", &schema.Category{
Key: "Input",
DisplayName: "Input options",
Order: 2147483627,
})
AppendFlagCategoryMap("auth-status", "show", "query", &schema.Category{
Key: "output",
DisplayName: "Output options",
Order: 2147483637,
})
AppendFlagCategoryMap("auth-status", "show", "quiet", &schema.Category{
Key: "output",
DisplayName: "Output options",
Order: 2147483637,
})
// append command to GlobalContext
Commands = append(Commands, cliCommand)
}