/
config.go
504 lines (429 loc) · 14.3 KB
/
config.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package cmd
import (
"context"
"fmt"
"io"
"path/filepath"
"runtime"
"strings"
"github.com/MakeNowJust/heredoc/v2"
"github.com/azure/azure-dev/cli/azd/cmd/actions"
"github.com/azure/azure-dev/cli/azd/internal"
"github.com/azure/azure-dev/cli/azd/pkg/alpha"
"github.com/azure/azure-dev/cli/azd/pkg/config"
"github.com/azure/azure-dev/cli/azd/pkg/input"
"github.com/azure/azure-dev/cli/azd/pkg/output"
"github.com/azure/azure-dev/cli/azd/pkg/output/ux"
"github.com/spf13/cobra"
)
var userConfigPath string
// Setup account command category
func configActions(root *actions.ActionDescriptor, rootOptions *internal.GlobalCommandOptions) *actions.ActionDescriptor {
userConfigDir, err := config.GetUserConfigDir()
if rootOptions.GenerateStaticHelp {
userConfigPath = heredoc.Doc(`the configuration path.
The default value of the config directory is:
* ` + output.WithBackticks(`$HOME/.azd`) + ` on Linux and macOS
* ` + output.WithBackticks(`%USERPROFILE%\.azd`) + ` on Windows
The configuration directory can be overridden by specifying a path in the AZD_CONFIG_DIR environment variable`)
} else if err != nil {
userConfigPath = output.WithBackticks(filepath.Join("$AZURE_CONFIG_DIR", "config.json"))
} else {
userConfigPath = output.WithBackticks(filepath.Join(userConfigDir, "config.json"))
}
var defaultConfigPath string
if runtime.GOOS == "windows" {
defaultConfigPath = filepath.Join("%USERPROFILE%", ".azd")
} else {
defaultConfigPath = filepath.Join("$HOME", ".azd")
}
var helpConfigPaths string
if rootOptions.GenerateStaticHelp {
//nolint:lll
helpConfigPaths = heredoc.Doc(`
Available since ` + output.WithBackticks("azure-dev-cli_0.4.0-beta.1") + `.
The easiest way to configure ` + output.WithBackticks("azd") + ` for the first time is to run [` + output.WithBackticks("azd init") + `](#azd-init). The subscription and location you select will be stored in the ` + output.WithBackticks("config.json") + ` file located in the config directory. To configure ` + output.WithBackticks("azd") + ` anytime afterwards, you'll use [` + output.WithBackticks("azd config set") + `](#azd-config-set).
The default value of the config directory is:
* $HOME/.azd on Linux and macOS
* %USERPROFILE%\.azd on Windows
`)
} else {
helpConfigPaths = heredoc.Docf(`
The easiest way to initially configure azd is to run %s.
The subscription and location you select will be stored at %s.
The default configuration path is %s.`,
output.WithBackticks("azd init"),
userConfigPath,
output.WithBackticks(defaultConfigPath))
}
longDescription := heredoc.Docf(`
Manage the Azure Developer CLI user configuration, which includes your default Azure subscription and location.
%s
The configuration directory can be overridden by specifying a path in the AZD_CONFIG_DIR environment variable.`,
helpConfigPaths)
group := root.Add("config", &actions.ActionDescriptorOptions{
Command: &cobra.Command{
Use: "config",
Short: "Manage azd configurations (ex: default Azure subscription, location).",
Long: longDescription,
},
HelpOptions: actions.ActionHelpOptions{
Description: getCmdConfigHelpDescription,
Footer: getCmdConfigHelpFooter,
},
GroupingOptions: actions.CommandGroupOptions{
RootLevelHelp: actions.CmdGroupConfig,
},
})
group.Add("show", &actions.ActionDescriptorOptions{
Command: &cobra.Command{
Short: "Show all the configuration values.",
Long: `Show all configuration values in ` + userConfigPath + `.`,
},
ActionResolver: newConfigShowAction,
OutputFormats: []output.Format{output.JsonFormat},
DefaultFormat: output.JsonFormat,
})
group.Add("list", &actions.ActionDescriptorOptions{
Command: &cobra.Command{
Short: "List all the configuration values. (Deprecated. Use azd config show)",
Hidden: true,
},
ActionResolver: newConfigListAction,
OutputFormats: []output.Format{output.JsonFormat},
DefaultFormat: output.JsonFormat,
})
group.Add("get", &actions.ActionDescriptorOptions{
Command: &cobra.Command{
Use: "get <path>",
Short: "Gets a configuration.",
Long: `Gets a configuration in ` + userConfigPath + `.`,
Args: cobra.ExactArgs(1),
},
ActionResolver: newConfigGetAction,
OutputFormats: []output.Format{output.JsonFormat},
DefaultFormat: output.JsonFormat,
})
group.Add("set", &actions.ActionDescriptorOptions{
Command: &cobra.Command{
Use: "set <path> <value>",
Short: "Sets a configuration.",
Long: `Sets a configuration in ` + userConfigPath + `.`,
Args: cobra.ExactArgs(2),
Example: `$ azd config set defaults.subscription <yourSubscriptionID>
$ azd config set defaults.location eastus`,
},
ActionResolver: newConfigSetAction,
})
group.Add("unset", &actions.ActionDescriptorOptions{
Command: &cobra.Command{
Use: "unset <path>",
Short: "Unsets a configuration.",
Long: `Removes a configuration in ` + userConfigPath + `.`,
Example: `$ azd config unset defaults.location`,
Args: cobra.ExactArgs(1),
},
ActionResolver: newConfigUnsetAction,
})
group.Add("reset", &actions.ActionDescriptorOptions{
Command: &cobra.Command{
Short: "Resets configuration to default.",
Long: `Resets all configuration in ` + userConfigPath + ` to the default.`,
},
ActionResolver: newConfigResetAction,
FlagsResolver: newConfigResetFlags,
})
group.Add("list-alpha", &actions.ActionDescriptorOptions{
Command: &cobra.Command{
Short: "Display the list of available features in alpha stage.",
},
HelpOptions: actions.ActionHelpOptions{
Footer: getCmdListAlphaHelpFooter,
},
ActionResolver: newConfigListAlphaAction,
})
return group
}
// azd config show
type configShowAction struct {
configManager config.UserConfigManager
formatter output.Formatter
writer io.Writer
}
func newConfigShowAction(
configManager config.UserConfigManager, formatter output.Formatter, writer io.Writer,
) actions.Action {
return &configShowAction{
configManager: configManager,
formatter: formatter,
writer: writer,
}
}
// Executes the `azd config show` action
func (a *configShowAction) Run(ctx context.Context) (*actions.ActionResult, error) {
azdConfig, err := a.configManager.Load()
if err != nil {
return nil, err
}
values := azdConfig.Raw()
if a.formatter.Kind() == output.JsonFormat {
err := a.formatter.Format(values, a.writer, nil)
if err != nil {
return nil, fmt.Errorf("failing formatting config values: %w", err)
}
}
return nil, nil
}
// azd config list - Deprecated
type configListAction struct {
configShow *configShowAction
console input.Console
}
func newConfigListAction(
console input.Console, configShow *configShowAction,
) actions.Action {
return &configListAction{
configShow: configShow,
console: console,
}
}
func (a *configListAction) Run(ctx context.Context) (*actions.ActionResult, error) {
fmt.Fprintln(
a.console.Handles().Stderr,
output.WithWarningFormat(
"WARNING: `azd config list` is deprecated and will be removed in a future release."))
fmt.Fprintln(
a.console.Handles().Stderr,
"Next time use `azd config show`")
return a.configShow.Run(ctx)
}
// azd config get <path>
type configGetAction struct {
configManager config.UserConfigManager
formatter output.Formatter
writer io.Writer
args []string
}
func newConfigGetAction(
configManager config.UserConfigManager,
formatter output.Formatter,
writer io.Writer,
args []string,
) actions.Action {
return &configGetAction{
configManager: configManager,
formatter: formatter,
writer: writer,
args: args,
}
}
// Executes the `azd config get <path>` action
func (a *configGetAction) Run(ctx context.Context) (*actions.ActionResult, error) {
azdConfig, err := a.configManager.Load()
if err != nil {
return nil, err
}
key := a.args[0]
value, ok := azdConfig.Get(key)
if !ok {
return nil, fmt.Errorf("no value stored at path '%s'", key)
}
if a.formatter.Kind() == output.JsonFormat {
err := a.formatter.Format(value, a.writer, nil)
if err != nil {
return nil, fmt.Errorf("failing formatting config values: %w", err)
}
}
return nil, nil
}
// azd config set <path> <value>
type configSetAction struct {
configManager config.UserConfigManager
args []string
}
func newConfigSetAction(configManager config.UserConfigManager, args []string) actions.Action {
return &configSetAction{
configManager: configManager,
args: args,
}
}
// Executes the `azd config set <path> <value>` action
func (a *configSetAction) Run(ctx context.Context) (*actions.ActionResult, error) {
azdConfig, err := a.configManager.Load()
if err != nil {
return nil, err
}
path := a.args[0]
value := a.args[1]
err = azdConfig.Set(path, value)
if err != nil {
return nil, fmt.Errorf("failed setting configuration value '%s' to '%s'. %w", path, value, err)
}
return nil, a.configManager.Save(azdConfig)
}
// azd config unset <path>
type configUnsetAction struct {
configManager config.UserConfigManager
args []string
}
func newConfigUnsetAction(configManager config.UserConfigManager, args []string) actions.Action {
return &configUnsetAction{
configManager: configManager,
args: args,
}
}
// Executes the `azd config unset <path>` action
func (a *configUnsetAction) Run(ctx context.Context) (*actions.ActionResult, error) {
azdConfig, err := a.configManager.Load()
if err != nil {
return nil, err
}
path := a.args[0]
err = azdConfig.Unset(path)
if err != nil {
return nil, fmt.Errorf("failed removing configuration with path '%s'. %w", path, err)
}
return nil, a.configManager.Save(azdConfig)
}
// azd config reset
type configResetActionFlags struct {
force bool
}
func newConfigResetFlags(cmd *cobra.Command) *configResetActionFlags {
flags := &configResetActionFlags{}
cmd.Flags().BoolVarP(&flags.force, "force", "f", false, "Force reset without confirmation.")
return flags
}
type configResetAction struct {
console input.Console
configManager config.UserConfigManager
flags *configResetActionFlags
args []string
}
func newConfigResetAction(
console input.Console,
configManager config.UserConfigManager,
flags *configResetActionFlags, args []string,
) actions.Action {
return &configResetAction{
console: console,
configManager: configManager,
flags: flags,
args: args,
}
}
// Executes the `azd config reset` action
func (a *configResetAction) Run(ctx context.Context) (*actions.ActionResult, error) {
a.console.MessageUxItem(ctx, &ux.MessageTitle{
Title: "Reset configuration (azd config reset)",
})
spinnerMessage := "Resetting azd configuration"
a.console.ShowSpinner(ctx, spinnerMessage, input.Step)
if !a.flags.force {
// nolint:lll
warningMessage := "WARNING: Resetting azd configuration will remove all stored values including defaults, feature flags and custom template sources.\n\n"
a.console.Message(ctx, output.WithWarningFormat(warningMessage))
confirm, err := a.console.Confirm(ctx, input.ConsoleOptions{
Message: "Continue with reset?",
DefaultValue: false,
})
if !confirm || err != nil {
a.console.StopSpinner(ctx, spinnerMessage, input.StepSkipped)
if err != nil {
return nil, fmt.Errorf("user cancelled reset confirmation, %w", err)
}
return nil, nil
}
}
err := a.configManager.Save(config.NewEmptyConfig())
a.console.StopSpinner(ctx, spinnerMessage, input.GetStepResultFormat(err))
if err != nil {
return nil, err
}
return &actions.ActionResult{
Message: &actions.ResultMessage{
Header: "Configuration reset",
},
}, nil
}
func getCmdConfigHelpDescription(*cobra.Command) string {
return generateCmdHelpDescription(
"Manage the Azure Developer CLI user configuration.",
[]string{
formatHelpNote(fmt.Sprintf("The default configuration path is: %s.",
output.WithLinkFormat("%HOME/.azd"),
)),
formatHelpNote(fmt.Sprintf("The configuration directory can be overridden by specifying a path"+
" in the %s environment variable.", output.WithBold("AZD_CONFIG_DIR"),
)),
formatHelpNote(fmt.Sprintf(
"The default values for azd prompts like subscription and location are stored with the key: %s.",
output.WithLinkFormat("defaults"),
)),
})
}
func getCmdConfigHelpFooter(c *cobra.Command) string {
return generateCmdHelpSamplesBlock(map[string]string{
"Set the default Azure subscription.": fmt.Sprintf("%s %s",
output.WithHighLightFormat("azd config set defaults.subscription"),
output.WithWarningFormat("<yourSubscriptionID>")),
"Set the default Azure deployment location.": fmt.Sprintf("%s %s",
output.WithHighLightFormat("azd config set defaults.location"),
output.WithWarningFormat("<location>")),
})
}
type configListAlphaAction struct {
alphaFeaturesManager *alpha.FeatureManager
console input.Console
args []string
}
func (a *configListAlphaAction) Run(ctx context.Context) (*actions.ActionResult, error) {
features, err := a.alphaFeaturesManager.ListFeatures()
if err != nil {
return nil, err
}
var alphaOutput []string
for _, alphaFeature := range features {
alphaOutput = append(alphaOutput,
strings.Join(
[]string{
fmt.Sprintf("Name: %s", alphaFeature.Id),
fmt.Sprintf("Description: %s", alphaFeature.Description),
fmt.Sprintf("Status: %s", alphaFeature.Status),
},
"\n",
))
}
a.console.Message(ctx, strings.Join(alphaOutput, "\n\n"))
// No UX output
return nil, nil
}
func newConfigListAlphaAction(
alphaFeaturesManager *alpha.FeatureManager,
console input.Console,
args []string) actions.Action {
return &configListAlphaAction{
alphaFeaturesManager: alphaFeaturesManager,
console: console,
args: args,
}
}
func getCmdListAlphaHelpFooter(*cobra.Command) string {
return generateCmdHelpSamplesBlock(map[string]string{
"Displays a list of all available features in the alpha stage": output.WithHighLightFormat(
"azd config list-alpha",
),
"Turn on a specific alpha feature": output.WithHighLightFormat(
"azd config set alpha.<feature-name> on",
),
"Turn off a specific alpha feature": output.WithHighLightFormat(
"azd config set alpha.<feature-name> off",
),
"Turn on all alpha features": output.WithHighLightFormat(
"azd config set alpha.all on",
),
"Turn off all alpha features": output.WithHighLightFormat(
"azd config set alpha.all off",
),
})
}