-
Notifications
You must be signed in to change notification settings - Fork 240
/
flag.go
340 lines (277 loc) · 7.49 KB
/
flag.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
// Package flag implements flag-related functionality.
package flag
import (
"context"
"github.com/spf13/cobra"
)
const (
// AccessTokenName denotes the name of the access token flag.
AccessTokenName = "access-token"
// VerboseName denotes the name of the verbose flag.
VerboseName = "verbose"
// JSONOutputName denotes the name of the json output flag.
JSONOutputName = "json"
// LocalOnlyName denotes the name of the local-only flag.
LocalOnlyName = "local-only"
// OrgName denotes the name of the org flag.
OrgName = "org"
// RegionName denotes the name of the region flag.
RegionName = "region"
// YesName denotes the name of the yes flag.
YesName = "yes"
// AppName denotes the name of the app flag.
AppName = "app"
// AppConfigFilePathName denotes the name of the app config file path flag.
AppConfigFilePathName = "config"
// ImageName denotes the name of the image flag.
ImageName = "image"
// NowName denotes the name of the now flag.
NowName = "now"
// NoDeploy denotes the name of the no deploy flag.
NoDeployName = "no-deploy"
// GenerateName denotes the name of the generate name flag.
GenerateNameFlagName = "generate-name"
// DetachName denotes the name of the detach flag.
DetachName = "detach"
)
// Flag wraps the set of flags.
type Flag interface {
addTo(*cobra.Command)
}
// Add adds flag to cmd, binding them on v should v not be nil.
func Add(cmd *cobra.Command, flags ...Flag) {
for _, flag := range flags {
flag.addTo(cmd)
}
}
// Bool wraps the set of boolean flags.
type Bool struct {
Name string
Shorthand string
Description string
Default bool
Hidden bool
}
func (b Bool) addTo(cmd *cobra.Command) {
flags := cmd.Flags()
if b.Shorthand != "" {
_ = flags.BoolP(b.Name, b.Shorthand, b.Default, b.Description)
} else {
_ = flags.Bool(b.Name, b.Default, b.Description)
}
f := flags.Lookup(b.Name)
f.Hidden = b.Hidden
}
// String wraps the set of string flags.
type String struct {
Name string
Shorthand string
Description string
Default string
ConfName string
EnvName string
Hidden bool
}
func (s String) addTo(cmd *cobra.Command) {
flags := cmd.Flags()
if s.Shorthand != "" {
_ = flags.StringP(s.Name, s.Shorthand, s.Default, s.Description)
} else {
_ = flags.String(s.Name, s.Default, s.Description)
}
f := flags.Lookup(s.Name)
f.Hidden = s.Hidden
}
// Int wraps the set of int flags.
type Int struct {
Name string
Shorthand string
Description string
Default int
Hidden bool
}
func (i Int) addTo(cmd *cobra.Command) {
flags := cmd.Flags()
if i.Shorthand != "" {
_ = flags.IntP(i.Name, i.Shorthand, i.Default, i.Description)
} else {
_ = flags.Int(i.Name, i.Default, i.Description)
}
f := flags.Lookup(i.Name)
f.Hidden = i.Hidden
}
// StringSlice wraps the set of string slice flags.
type StringSlice struct {
Name string
Shorthand string
Description string
Default []string
ConfName string
EnvName string
Hidden bool
}
func (ss StringSlice) addTo(cmd *cobra.Command) {
flags := cmd.Flags()
if ss.Shorthand != "" {
_ = flags.StringSliceP(ss.Name, ss.Shorthand, ss.Default, ss.Description)
} else {
_ = flags.StringSlice(ss.Name, ss.Default, ss.Description)
}
f := flags.Lookup(ss.Name)
f.Hidden = ss.Hidden
}
// Org returns an org string flag.
func Org() String {
return String{
Name: OrgName,
Description: "The organization to operate on",
Shorthand: "o",
}
}
// Region returns a region string flag.
func Region() String {
return String{
Name: RegionName,
Description: "The region to operate on",
Shorthand: "r",
}
}
// Yes returns a yes bool flag.
func Yes() Bool {
return Bool{
Name: YesName,
Shorthand: "y",
Description: "Accept all confirmations",
}
}
// App returns an app string flag.
func App() String {
return String{
Name: AppName,
Shorthand: "a",
Description: "Application name",
}
}
// AppConfig returns an app config string flag.
func AppConfig() String {
return String{
Name: AppConfigFilePathName,
Shorthand: "c",
Description: "Path to application configuration file",
}
}
// Image returns a Docker image config string flag.
func Image() String {
return String{
Name: ImageName,
Shorthand: "i",
Description: "The image tag or ID to deploy",
}
}
// Now returns a boolean flag for deploying immediately
func Now() Bool {
return Bool{
Name: NowName,
Description: "Deploy now without confirmation",
Default: false,
}
}
// GenerateName returns a boolean flag for generating an application name
func GenerateName() Bool {
return Bool{
Name: GenerateNameFlagName,
Description: "Always generate a name for the app",
Default: false,
}
}
const remoteOnlyName = "remote-only"
// RemoteOnly returns a boolean flag for deploying remotely
func RemoteOnly(defaultValue bool) Bool {
return Bool{
Name: remoteOnlyName,
Description: "Perform builds on a remote builder instance instead of using the local docker daemon",
Default: defaultValue,
}
}
func GetRemoteOnly(ctx context.Context) bool {
return GetBool(ctx, remoteOnlyName)
}
const localOnlyName = "local-only"
// RemoteOnly returns a boolean flag for deploying remotely
func LocalOnly() Bool {
return Bool{
Name: localOnlyName,
Description: "Only perform builds locally using the local docker daemon",
}
}
func GetLocalOnly(ctx context.Context) bool {
return GetBool(ctx, localOnlyName)
}
const detachName = "detach"
// Detach returns a boolean flag for detaching during deployment
func Detach() Bool {
return Bool{
Name: detachName,
Description: "Return immediately instead of monitoring deployment progress",
}
}
func GetDetach(ctx context.Context) bool {
return GetBool(ctx, detachName)
}
const buildOnlyName = "build-only"
// BuildOnly returns a boolean flag for building without a deployment
func BuildOnly() Bool {
return Bool{
Name: buildOnlyName,
Description: "Build but do not deploy",
}
}
func GetBuildOnly(ctx context.Context) bool {
return GetBool(ctx, buildOnlyName)
}
const pushName = "push"
// Push returns a boolean flag to force pushing a build image to the registry
func Push() Bool {
return Bool{
Name: pushName,
Description: "Push image to registry after build is complete",
Default: false,
}
}
const dockerfileName = "dockerfile"
func Dockerfile() String {
return String{
Name: dockerfileName,
Description: "Path to a Dockerfile. Defaults to the Dockerfile in the working directory.",
}
}
func ImageLabel() String {
return String{
Name: "image-label",
Description: `Image label to use when tagging and pushing to the fly registry. Defaults to "deployment-{timestamp}".`,
}
}
func NoCache() Bool {
return Bool{
Name: "no-cache",
Description: "Do not use the build cache when building the image",
}
}
func BuildSecret() StringSlice {
return StringSlice{
Name: "build-secret",
Description: "Set of build secrets of NAME=VALUE pairs. Can be specified multiple times. See https://docs.docker.com/develop/develop-images/build_enhancements/#new-docker-build-secret-information",
}
}
func BuildArg() StringSlice {
return StringSlice{
Name: "build-arg",
Description: "Set of build time variables in the form of NAME=VALUE pairs. Can be specified multiple times.",
}
}
func BuildTarget() String {
return String{
Name: "build-target",
Description: "Set the target build stage to build if the Dockerfile has more than one stage",
}
}