Skip to content

Commit

Permalink
feat: add remove context command
Browse files Browse the repository at this point in the history
  • Loading branch information
mflagey committed Apr 25, 2023
1 parent 7930f74 commit b7b5cc4
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cliconfig/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions cliconfig/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
49 changes: 49 additions & 0 deletions cmd/config/remove-context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package config

import (
"fmt"
"morty/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
},
}
1 change: 1 addition & 0 deletions cmd/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b7b5cc4

Please sign in to comment.