/
doit.go
352 lines (290 loc) · 10.5 KB
/
doit.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
/*
Copyright 2018 The Doctl Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/digitalocean/doctl"
"github.com/fatih/color"
"github.com/mattn/go-isatty"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
defaultConfigName = "config.yaml" // default name of config file
manageResourcesGroup = "manageResources"
configureDoctlGroup = "configureDoctl"
viewBillingGroup = "viewBilling"
)
var (
//DoitCmd is the root level doctl command that all other commands attach to
DoitCmd = &Command{ // base command
Command: &cobra.Command{
Use: "doctl",
Short: "doctl is a command line interface (CLI) for the DigitalOcean API.",
},
}
//Writer wires up stdout for all commands to write to
Writer = os.Stdout
//APIURL customize API base URL
APIURL string
//Context current auth context
Context string
//Output global output format
Output string
//Token global authorization token
Token string
//Trace toggles http tracing output
Trace bool
//Verbose toggle verbose output on and off
Verbose bool
//Interactive toggle interactive behavior
Interactive bool
// Retry settings to pass through to godo.RetryConfig
RetryMax int
RetryWaitMax int
RetryWaitMin int
requiredColor = color.New(color.Bold).SprintfFunc()
)
func init() {
var cfgFile string
initConfig()
rootPFlagSet := DoitCmd.PersistentFlags()
rootPFlagSet.StringVarP(&cfgFile, "config", "c",
filepath.Join(defaultConfigHome(), defaultConfigName), "Specify a custom config file")
viper.BindPFlag("config", rootPFlagSet.Lookup("config"))
rootPFlagSet.StringVarP(&APIURL, "api-url", "u", "", "Override default API endpoint")
viper.BindPFlag("api-url", rootPFlagSet.Lookup("api-url"))
rootPFlagSet.StringVarP(&Token, doctl.ArgAccessToken, "t", "", "API V2 access token")
viper.BindPFlag(doctl.ArgAccessToken, rootPFlagSet.Lookup(doctl.ArgAccessToken))
rootPFlagSet.StringVarP(&Output, doctl.ArgOutput, "o", "text", "Desired output format [text|json]")
viper.BindPFlag("output", rootPFlagSet.Lookup(doctl.ArgOutput))
rootPFlagSet.StringVarP(&Context, doctl.ArgContext, "", "", "Specify a custom authentication context name")
DoitCmd.RegisterFlagCompletionFunc(doctl.ArgContext, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return getAuthContextList(), cobra.ShellCompDirectiveNoFileComp
})
rootPFlagSet.BoolVarP(&Trace, "trace", "", false, "Show a log of network activity while performing a command")
rootPFlagSet.BoolVarP(&Verbose, doctl.ArgVerbose, "v", false, "Enable verbose output")
interactive := isTerminal(os.Stdout) && isTerminal(os.Stderr)
interactiveHelpText := "Enable interactive behavior. Defaults to true if the terminal supports it"
if !interactive {
// this is automatically added if interactive == true
interactiveHelpText += " (default false)"
}
rootPFlagSet.BoolVarP(&Interactive, doctl.ArgInteractive, "", interactive, interactiveHelpText)
rootPFlagSet.IntVar(&RetryMax, "http-retry-max", 5, "Set maximum number of retries for requests that fail with a 429 or 500-level error")
viper.BindPFlag("http-retry-max", rootPFlagSet.Lookup("http-retry-max"))
rootPFlagSet.IntVar(&RetryWaitMax, "http-retry-wait-max", 30, "Set the minimum number of seconds to wait before retrying a failed request")
viper.BindPFlag("http-retry-wait-max", rootPFlagSet.Lookup("http-retry-wait-max"))
DoitCmd.PersistentFlags().MarkHidden("http-retry-wait-max")
rootPFlagSet.IntVar(&RetryWaitMin, "http-retry-wait-min", 1, "Set the maximum number of seconds to wait before retrying a failed request")
viper.BindPFlag("http-retry-wait-min", rootPFlagSet.Lookup("http-retry-wait-min"))
DoitCmd.PersistentFlags().MarkHidden("http-retry-wait-min")
addCommands()
cobra.OnInitialize(initConfig)
}
func initConfig() {
viper.SetEnvPrefix("DIGITALOCEAN")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.SetConfigType("yaml")
cfgFile := viper.GetString("config")
viper.SetConfigFile(cfgFile)
viper.SetDefault("output", "text")
viper.SetDefault(doctl.ArgContext, doctl.ArgDefaultContext)
Context = strings.ToLower(Context)
if _, err := os.Stat(cfgFile); err == nil {
if err := viper.ReadInConfig(); err != nil {
log.Fatalln("Config initialization failed:", err)
}
}
}
// in case we ever want to change this, or let folks configure it...
func defaultConfigHome() string {
cfgDir, err := os.UserConfigDir()
checkErr(err)
return filepath.Join(cfgDir, "doctl")
}
func configHome() string {
ch := defaultConfigHome()
err := os.MkdirAll(ch, 0755)
checkErr(err)
return ch
}
// Execute executes the current command using DoitCmd.
func Execute() {
if err := DoitCmd.Execute(); err != nil {
if !strings.Contains(err.Error(), "unknown command") {
fmt.Println(err)
}
os.Exit(-1)
}
}
// AddCommands adds sub commands to the base command.
func addCommands() {
DoitCmd.AddGroup(&cobra.Group{ID: manageResourcesGroup, Title: "Manage DigitalOcean Resources:"})
DoitCmd.AddGroup(&cobra.Group{ID: configureDoctlGroup, Title: "Configure doctl:"})
DoitCmd.AddGroup(&cobra.Group{ID: viewBillingGroup, Title: "View Billing:"})
DoitCmd.AddCommand(Account())
DoitCmd.AddCommand(Apps())
DoitCmd.AddCommand(Auth())
DoitCmd.AddCommand(Balance())
DoitCmd.AddCommand(BillingHistory())
DoitCmd.AddCommand(Invoices())
DoitCmd.AddCommand(computeCmd())
DoitCmd.AddCommand(Kubernetes())
DoitCmd.AddCommand(Databases())
DoitCmd.AddCommand(Projects())
DoitCmd.AddCommand(Version())
DoitCmd.AddCommand(Registry())
DoitCmd.AddCommand(VPCs())
DoitCmd.AddCommand(OneClicks())
DoitCmd.AddCommand(Monitoring())
DoitCmd.AddCommand(Serverless())
}
func computeCmd() *Command {
cmd := &Command{
Command: &cobra.Command{
Use: "compute",
Short: "Display commands that manage infrastructure",
Long: `The subcommands under ` + "`" + `doctl compute` + "`" + ` are for managing DigitalOcean resources.`,
GroupID: manageResourcesGroup,
},
}
cmd.AddCommand(Actions())
cmd.AddCommand(CDN())
cmd.AddCommand(Certificate())
cmd.AddCommand(DropletAction())
cmd.AddCommand(Droplet())
cmd.AddCommand(Domain())
cmd.AddCommand(Firewall())
cmd.AddCommand(ReservedIP())
cmd.AddCommand(ReservedIPAction())
cmd.AddCommand(Images())
cmd.AddCommand(ImageAction())
cmd.AddCommand(LoadBalancer())
cmd.AddCommand(Plugin())
cmd.AddCommand(Region())
cmd.AddCommand(Size())
cmd.AddCommand(Snapshot())
cmd.AddCommand(SSHKeys())
cmd.AddCommand(Tags())
cmd.AddCommand(Volume())
cmd.AddCommand(VolumeAction())
// SSH is different since it doesn't have any subcommands. In this case, let's
// give it a parent at init time.
SSH(cmd)
return cmd
}
type flagOpt func(c *Command, name, key string)
func requiredOpt() flagOpt {
return func(c *Command, name, key string) {
c.MarkFlagRequired(key)
key = fmt.Sprintf("required.%s", key)
viper.Set(key, true)
u := c.Flag(name).Usage
c.Flag(name).Usage = fmt.Sprintf("%s %s", u, requiredColor("(required)"))
}
}
func hiddenFlag() flagOpt {
return func(c *Command, name, key string) {
c.Flags().MarkHidden(name)
}
}
// AddStringFlag adds a string flag to a command.
func AddStringFlag(cmd *Command, name, shorthand, dflt, desc string, opts ...flagOpt) {
fn := flagName(cmd, name)
// flagName only supports nesting three levels deep. We need to force the
// app dev config set/unset --dev-config flag to be nested deeper.
// i.e dev.config.set.dev-config over config.set.dev-config
// This prevents a conflict with the base config setting.
if name == doctl.ArgAppDevConfig && !strings.HasPrefix(fn, appDevConfigFileNamespace+".") {
fn = fmt.Sprintf("%s.%s", appDevConfigFileNamespace, fn)
}
cmd.Flags().StringP(name, shorthand, dflt, desc)
for _, o := range opts {
o(cmd, name, fn)
}
viper.BindPFlag(fn, cmd.Flags().Lookup(name))
}
// AddIntFlag adds an integr flag to a command.
func AddIntFlag(cmd *Command, name, shorthand string, def int, desc string, opts ...flagOpt) {
fn := flagName(cmd, name)
cmd.Flags().IntP(name, shorthand, def, desc)
viper.BindPFlag(fn, cmd.Flags().Lookup(name))
for _, o := range opts {
o(cmd, name, fn)
}
}
// AddBoolFlag adds a boolean flag to a command.
func AddBoolFlag(cmd *Command, name, shorthand string, def bool, desc string, opts ...flagOpt) {
fn := flagName(cmd, name)
cmd.Flags().BoolP(name, shorthand, def, desc)
viper.BindPFlag(fn, cmd.Flags().Lookup(name))
for _, o := range opts {
o(cmd, name, fn)
}
}
// AddStringSliceFlag adds a string slice flag to a command.
func AddStringSliceFlag(cmd *Command, name, shorthand string, def []string, desc string, opts ...flagOpt) {
fn := flagName(cmd, name)
cmd.Flags().StringSliceP(name, shorthand, def, desc)
viper.BindPFlag(fn, cmd.Flags().Lookup(name))
for _, o := range opts {
o(cmd, name, fn)
}
}
// AddStringMapStringFlag adds a map of strings by strings flag to a command.
func AddStringMapStringFlag(cmd *Command, name, shorthand string, def map[string]string, desc string, opts ...flagOpt) {
fn := flagName(cmd, name)
cmd.Flags().StringToStringP(name, shorthand, def, desc)
viper.BindPFlag(fn, cmd.Flags().Lookup(name))
for _, o := range opts {
o(cmd, name, fn)
}
}
// AddDurationFlag adds a duration flag to a command.
func AddDurationFlag(cmd *Command, name, shorthand string, def time.Duration, desc string, opts ...flagOpt) {
fn := flagName(cmd, name)
cmd.Flags().DurationP(name, shorthand, def, desc)
viper.BindPFlag(fn, cmd.Flags().Lookup(name))
for _, o := range opts {
o(cmd, name, fn)
}
}
func flagName(cmd *Command, name string) string {
if cmd.Parent() != nil {
p := cmd.Parent().Name()
if cmd.overrideNS != "" {
p = cmd.overrideNS
}
return fmt.Sprintf("%s.%s.%s", p, cmd.Name(), name)
}
return fmt.Sprintf("%s.%s", cmd.Name(), name)
}
func cmdNS(cmd *Command) string {
if cmd.Parent() != nil {
if cmd.overrideNS != "" {
return fmt.Sprintf("%s.%s", cmd.overrideNS, cmd.Name())
}
return fmt.Sprintf("%s.%s", cmd.Parent().Name(), cmd.Name())
}
return cmd.Name()
}
func isTerminal(f *os.File) bool {
return isatty.IsTerminal(f.Fd()) || isatty.IsCygwinTerminal(f.Fd())
}