/
arg_specs.go
204 lines (175 loc) · 5.28 KB
/
arg_specs.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
package core
import (
"context"
"fmt"
"strings"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/scaleway-sdk-go/validation"
)
type ArgSpecs []*ArgSpec
func (s ArgSpecs) GetPositionalArg() *ArgSpec {
var positionalArg *ArgSpec
for _, argSpec := range s {
if argSpec.Positional {
if positionalArg != nil {
panic(fmt.Errorf("more than one positional parameter detected: %s and %s are flagged as positional arg", positionalArg.Name, argSpec.Name))
}
positionalArg = argSpec
}
}
return positionalArg
}
// GetDeprecated gets all fields filtered by the deprecation state.
func (s ArgSpecs) GetDeprecated(deprecated bool) ArgSpecs {
result := ArgSpecs{}
for _, argSpec := range s {
if argSpec.Deprecated == deprecated {
result = append(result, argSpec)
}
}
return result
}
func (s ArgSpecs) GetByName(name string) *ArgSpec {
for _, spec := range s {
if spec.Name == name {
return spec
}
}
return nil
}
func (s *ArgSpecs) DeleteByName(name string) {
for i, spec := range *s {
if spec.Name == name {
*s = append((*s)[:i], (*s)[i+1:]...)
return
}
}
panic(fmt.Errorf("in DeleteByName: %s not found", name))
}
func (s *ArgSpecs) AddBefore(name string, argSpec *ArgSpec) {
for i, spec := range *s {
if spec.Name == name {
newSpecs := ArgSpecs(nil)
newSpecs = append(newSpecs, (*s)[:i]...)
newSpecs = append(newSpecs, argSpec)
newSpecs = append(newSpecs, (*s)[i:]...)
*s = newSpecs
return
}
}
panic(fmt.Errorf("in AddBefore: %s not found", name))
}
type ArgSpec struct {
// Name of the argument.
Name string
// Short description.
Short string
// Required defines whether the argument is required.
Required bool
// Default is the argument default value.
Default DefaultFunc
// EnumValues contains all possible values of an enum.
EnumValues []string
// AutoCompleteFunc is used to autocomplete possible values for a given argument.
AutoCompleteFunc AutoCompleteArgFunc
// ValidateFunc validates an argument.
ValidateFunc ArgSpecValidateFunc
// Positional defines whether the argument is a positional argument. NB: a positional argument is required.
Positional bool
// Only one argument of the same OneOfGroup could be specified
OneOfGroup string
// Deprecated is used to flag an argument as deprecated.
// Use the short field to indicate migration tips for users.
Deprecated bool
// CanLoadFile allow to use @ prefix to load a file as content
CanLoadFile bool
}
func (a *ArgSpec) Prefix() string {
return a.Name + "="
}
func (a *ArgSpec) IsPartOfMapOrSlice() bool {
return strings.Contains(a.Name, sliceSchema) || strings.Contains(a.Name, mapSchema)
}
func (a *ArgSpec) ConflictWith(b *ArgSpec) bool {
return (a.OneOfGroup != "" && b.OneOfGroup != "") &&
(a.OneOfGroup == b.OneOfGroup)
}
type DefaultFunc func(ctx context.Context) (value string, doc string)
func ZoneArgSpec(zones ...scw.Zone) *ArgSpec {
enumValues := []string(nil)
for _, zone := range zones {
enumValues = append(enumValues, zone.String())
}
return &ArgSpec{
Name: "zone",
Short: "Zone to target. If none is passed will use default zone from the config",
EnumValues: enumValues,
ValidateFunc: func(argSpec *ArgSpec, value interface{}) error {
if validation.IsZone(value.(scw.Zone).String()) {
return nil
}
return &CliError{
Err: fmt.Errorf("invalid zone %s", value),
Hint: "Zone format should look like XX-XXX-X (e.g. fr-par-1)",
}
},
Default: func(ctx context.Context) (value string, doc string) {
client := ExtractClient(ctx)
zone, _ := client.GetDefaultZone()
return zone.String(), zone.String()
},
}
}
func RegionArgSpec(regions ...scw.Region) *ArgSpec {
enumValues := []string(nil)
for _, region := range regions {
enumValues = append(enumValues, region.String())
}
return &ArgSpec{
Name: "region",
Short: "Region to target. If none is passed will use default region from the config",
EnumValues: enumValues,
ValidateFunc: func(argSpec *ArgSpec, value interface{}) error {
if validation.IsRegion(value.(scw.Region).String()) {
return nil
}
return &CliError{
Err: fmt.Errorf("invalid region %s", value),
Hint: "Region format should look like XX-XXX (e.g. fr-par)",
}
},
Default: func(ctx context.Context) (value string, doc string) {
client := ExtractClient(ctx)
region, _ := client.GetDefaultRegion()
return region.String(), region.String()
},
}
}
func ProjectIDArgSpec() *ArgSpec {
return &ArgSpec{
Name: "project-id",
Short: "Project ID to use. If none is passed the default project ID will be used",
ValidateFunc: ValidateProjectID(),
}
}
func ProjectArgSpec() *ArgSpec {
return &ArgSpec{
Name: "project",
Short: "Project ID to use. If none is passed the default project ID will be used",
ValidateFunc: ValidateProjectID(),
}
}
func OrganizationArgSpec() *ArgSpec {
return &ArgSpec{
Name: "organization",
Short: "Organization ID to use. If none is passed the default organization ID will be used",
ValidateFunc: ValidateOrganizationID(),
}
}
func OrganizationIDArgSpec() *ArgSpec {
return &ArgSpec{
Name: "organization-id",
Short: "Organization ID to use. If none is passed the default organization ID will be used",
ValidateFunc: ValidateOrganizationID(),
}
}