|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/morty-faas/cli/cliconfig" |
| 6 | + |
| 7 | + log "github.com/sirupsen/logrus" |
| 8 | + "github.com/spf13/cobra" |
| 9 | +) |
| 10 | + |
| 11 | +var removeContextCmd = &cobra.Command{ |
| 12 | + Use: "remove-context NAME", |
| 13 | + Short: "Remove a context", |
| 14 | + Long: `Remove a context from your configuration.`, |
| 15 | + Args: validateContextName, |
| 16 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 17 | + // Safe call, validation is performed by validateArgs automatically by cobra |
| 18 | + name := args[0] |
| 19 | + |
| 20 | + cfg := cmd.Context().Value(cliconfig.CtxKey{}).(*cliconfig.Config) |
| 21 | + |
| 22 | + log.Debugf("Remove context '%s'", name) |
| 23 | + |
| 24 | + currentContext, _ := cfg.GetCurrentContext() |
| 25 | + |
| 26 | + if err := cfg.RemoveContext(name); err != nil { |
| 27 | + return err |
| 28 | + } |
| 29 | + |
| 30 | + // if we delete the current context, we set the first context as the current context |
| 31 | + if currentContext.Name == name { |
| 32 | + if err := cfg.UseContext(cfg.Contexts[0].Name); err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + if err := cfg.Save(); err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + |
| 41 | + fmt.Printf("Success ! Your context '%s' has been deleted.\n", name) |
| 42 | + |
| 43 | + if currentContext.Name == name { |
| 44 | + fmt.Printf("Your current context has been set to '%s'.\n", cfg.Contexts[0].Name) |
| 45 | + } |
| 46 | + |
| 47 | + return nil |
| 48 | + }, |
| 49 | +} |
0 commit comments