-
Notifications
You must be signed in to change notification settings - Fork 11
/
auth_select.go
70 lines (55 loc) · 1.65 KB
/
auth_select.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
package auth
import (
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"github.com/axiomhq/cli/internal/cmdutil"
)
func newSelectCmd(f *cmdutil.Factory) *cobra.Command {
var activeDeployment string
cmd := &cobra.Command{
Use: "select [<alias>]",
Short: "Select an Axiom configuration",
Long: heredoc.Doc(`
Select an Axiom configuration to use by default and persist the choice
in the configuration file.
`),
DisableFlagsInUseLine: true,
Args: cmdutil.PopulateFromArgs(f, &activeDeployment),
ValidArgsFunction: deploymentCompletionFunc(f.Config),
Example: heredoc.Doc(`
# Select the deployment to use by default:
$ axiom auth select
# Specify the deployment to use by default:
$ axiom auth select axiom-eu-west-1
`),
PreRunE: cmdutil.ChainRunFuncs(
cmdutil.AsksForSetup(f, NewLoginCmd(f)),
cmdutil.NeedsDeployments(f),
cmdutil.NeedsValidDeployment(f, &activeDeployment),
),
RunE: func(*cobra.Command, []string) error {
if activeDeployment == "" {
options := f.Config.DeploymentAliases()
if err := survey.AskOne(&survey.Select{
Message: "Which deployment to select?",
Default: options[0],
Options: options,
}, &f.Config.ActiveDeployment, f.IO.SurveyIO()); err != nil {
return err
}
} else {
f.Config.ActiveDeployment = activeDeployment
}
if err := f.Config.Write(); err != nil {
return err
}
cs := f.IO.ColorScheme()
fmt.Fprintf(f.IO.ErrOut(), "%s Now using deployment %s by default\n",
cs.SuccessIcon(), cs.Bold(f.Config.ActiveDeployment))
return nil
},
}
return cmd
}