Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add remove context command #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Comment on lines +92 to +94
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why block the remove of the default context ? For me every context can be deleted. It does not make sense to block just this one.

Also, please ensure that there is no error if a user removes all the contexts. In that case, the default context should be used.

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"
"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
},
}
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