-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontext_delete.go
More file actions
67 lines (53 loc) · 1.79 KB
/
context_delete.go
File metadata and controls
67 lines (53 loc) · 1.79 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
60
61
62
63
64
65
66
67
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 DeleteArgs struct {
Name string
}
func DeleteCommand(cli *di.Deps) *cobra.Command {
args := &DeleteArgs{}
cmd := &cobra.Command{
Use: "delete",
Short: "Delete a saved context from the CLI configuration",
Long: "Delete a connection context from the CLI configuration file. The currently active context cannot be deleted; switch to a different context first.",
Example: `# delete an unused context
sts context delete --name old-staging`,
RunE: cli.CmdRunEWithConfig(RunContextDeleteCommand(args)),
}
common.AddRequiredNameFlagVar(cmd, &args.Name, "Name of the context")
return cmd
}
func RunContextDeleteCommand(args *DeleteArgs) 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 {
if args.Name == cfg.CurrentContext {
return common.NewCLIArgParseError(fmt.Errorf("cannot delete the current context (%s)", args.Name))
}
found := -1
for i, c := range cfg.Contexts {
if c.Name == args.Name {
found = i
break
}
}
if found == -1 {
return common.NewNotFoundError(fmt.Errorf("context with name '%s' not found", args.Name))
}
cfg.Contexts = append(cfg.Contexts[:found], cfg.Contexts[found+1:]...)
if err := config.WriteConfig(cli.ConfigPath, cfg); err != nil {
return common.NewWriteFileError(err, cli.ConfigPath)
}
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{
"deleted context": args.Name,
})
} else {
cli.Printer.Success(fmt.Sprintf("Deleted context '%s'\n", args.Name))
}
return nil
}
}