diff --git a/cliconfig/context.go b/cliconfig/context.go index 1b6b46d..f9fe8dc 100644 --- a/cliconfig/context.go +++ b/cliconfig/context.go @@ -25,6 +25,7 @@ const ( var ( ErrContextNotFound = errors.New("context not found") ErrContextAlreadyExistsWithName = errors.New("context already exists with the same name") + ErrRemoveDefaultContext = errors.New("cannot delete the default context") ) type ( @@ -86,6 +87,20 @@ func (c *Config) AddContext(context *Context) error { return nil } +// RemoveContext remove a context from the list of known contexts +func (c *Config) RemoveContext(name string) error { + if name == "localhost" { + return ErrRemoveDefaultContext + } + for i, ctx := range c.Contexts { + if ctx.Name == name { + c.Contexts = append(c.Contexts[:i], c.Contexts[i+1:]...) + return nil + } + } + return ErrContextNotFound +} + // hasContext return true if the given context exists, false otherwise func (c *Config) hasContext(name string) bool { for _, ctx := range c.Contexts { diff --git a/cliconfig/context_test.go b/cliconfig/context_test.go index 47c8ce0..36705b1 100644 --- a/cliconfig/context_test.go +++ b/cliconfig/context_test.go @@ -118,6 +118,30 @@ func Test_AddContext(t *testing.T) { assert.ErrorIs(t, err, ErrContextAlreadyExistsWithName) } +func Test_RemoveContext(t *testing.T) { + expected := Context{ + Name: "test", + Gateway: "test", + Registry: "test", + } + + config := defaultConfig() + + err := config.AddContext(&expected) + assert.NoError(t, err) + assert.Len(t, config.Contexts, 2) + + err = config.RemoveContext("test") + assert.NoError(t, err) + assert.Len(t, config.Contexts, 1) + + err = config.RemoveContext("thiscontextdoesnotexist") + assert.ErrorIs(t, err, ErrContextNotFound) + + err = config.RemoveContext("localhost") + assert.ErrorIs(t, err, ErrRemoveDefaultContext) +} + func Test_SanitizeUrl(t *testing.T) { url := "http://localhost:8080/" expected := "http://localhost:8080" diff --git a/cmd/config/remove-context.go b/cmd/config/remove-context.go new file mode 100644 index 0000000..398e0d5 --- /dev/null +++ b/cmd/config/remove-context.go @@ -0,0 +1,49 @@ +package config + +import ( + "fmt" + "github.com/morty-faas/cli/cliconfig" + + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +var removeContextCmd = &cobra.Command{ + Use: "remove-context NAME", + Short: "Remove a context", + Long: `Remove a context from your configuration.`, + Args: validateContextName, + RunE: func(cmd *cobra.Command, args []string) error { + // Safe call, validation is performed by validateArgs automatically by cobra + name := args[0] + + cfg := cmd.Context().Value(cliconfig.CtxKey{}).(*cliconfig.Config) + + log.Debugf("Remove context '%s'", name) + + currentContext, _ := cfg.GetCurrentContext() + + if err := cfg.RemoveContext(name); err != nil { + return err + } + + // if we delete the current context, we set the first context as the current context + if currentContext.Name == name { + if err := cfg.UseContext(cfg.Contexts[0].Name); err != nil { + return err + } + } + + if err := cfg.Save(); err != nil { + return err + } + + fmt.Printf("Success ! Your context '%s' has been deleted.\n", name) + + if currentContext.Name == name { + fmt.Printf("Your current context has been set to '%s'.\n", cfg.Contexts[0].Name) + } + + return nil + }, +} diff --git a/cmd/config/root.go b/cmd/config/root.go index dd3487d..8158e8f 100644 --- a/cmd/config/root.go +++ b/cmd/config/root.go @@ -20,6 +20,7 @@ func init() { RootCmd.AddCommand(currentContextCmd) RootCmd.AddCommand(useContextCmd) RootCmd.AddCommand(listContextCmd) + RootCmd.AddCommand(removeContextCmd) } func validateContextName(cmd *cobra.Command, args []string) error {