-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontext_set.go
More file actions
59 lines (47 loc) · 1.54 KB
/
context_set.go
File metadata and controls
59 lines (47 loc) · 1.54 KB
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
package context
import (
"fmt"
"github.com/spf13/cobra"
"github.com/stackvista/stackstate-cli/internal/common"
"github.com/stackvista/stackstate-cli/internal/config"
"github.com/stackvista/stackstate-cli/internal/di"
)
type SetArgs struct {
Name string
}
func SetCommand(cli *di.Deps) *cobra.Command {
args := &SetArgs{}
cmd := &cobra.Command{
Use: "set",
Short: "Set the active context to use for CLI commands",
Long: "Set the active connection context. All subsequent CLI commands will use this context's server URL and credentials.",
Example: `# switch to the production context
sts context set --name production
# switch to a different environment
sts context set --name staging`,
RunE: cli.CmdRunEWithConfig(RunContextSetCommand(args, cli)),
}
common.AddRequiredNameFlagVar(cmd, &args.Name, "Name of the context")
return cmd
}
func RunContextSetCommand(args *SetArgs, cli *di.Deps) func(cli *di.Deps, cmd *cobra.Command, cfg *config.Config) common.CLIError {
return func(cli *di.Deps, cmd *cobra.Command, cfg *config.Config) common.CLIError {
_, err := cfg.GetContext(args.Name)
if err != nil {
return common.NewNotFoundError(err)
}
cfg.CurrentContext = args.Name
if err := config.WriteConfig(cli.ConfigPath, cfg); err != nil {
return common.NewWriteFileError(err, cli.ConfigPath)
}
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{
"current-context": args.Name,
})
} else {
cli.Printer.PrintLn(fmt.Sprintf("Current context set to %s", args.Name))
// No output
}
return nil
}
}