Skip to content

Commit 1ecc135

Browse files
committed
feat: add remove context command
1 parent 7930f74 commit 1ecc135

4 files changed

Lines changed: 89 additions & 0 deletions

File tree

cliconfig/context.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const (
2525
var (
2626
ErrContextNotFound = errors.New("context not found")
2727
ErrContextAlreadyExistsWithName = errors.New("context already exists with the same name")
28+
ErrRemoveDefaultContext = errors.New("cannot delete the default context")
2829
)
2930

3031
type (
@@ -86,6 +87,20 @@ func (c *Config) AddContext(context *Context) error {
8687
return nil
8788
}
8889

90+
// RemoveContext remove a context from the list of known contexts
91+
func (c *Config) RemoveContext(name string) error {
92+
if name == "localhost" {
93+
return ErrRemoveDefaultContext
94+
}
95+
for i, ctx := range c.Contexts {
96+
if ctx.Name == name {
97+
c.Contexts = append(c.Contexts[:i], c.Contexts[i+1:]...)
98+
return nil
99+
}
100+
}
101+
return ErrContextNotFound
102+
}
103+
89104
// hasContext return true if the given context exists, false otherwise
90105
func (c *Config) hasContext(name string) bool {
91106
for _, ctx := range c.Contexts {

cliconfig/context_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,30 @@ func Test_AddContext(t *testing.T) {
118118
assert.ErrorIs(t, err, ErrContextAlreadyExistsWithName)
119119
}
120120

121+
func Test_RemoveContext(t *testing.T) {
122+
expected := Context{
123+
Name: "test",
124+
Gateway: "test",
125+
Registry: "test",
126+
}
127+
128+
config := defaultConfig()
129+
130+
err := config.AddContext(&expected)
131+
assert.NoError(t, err)
132+
assert.Len(t, config.Contexts, 2)
133+
134+
err = config.RemoveContext("test")
135+
assert.NoError(t, err)
136+
assert.Len(t, config.Contexts, 1)
137+
138+
err = config.RemoveContext("thiscontextdoesnotexist")
139+
assert.ErrorIs(t, err, ErrContextNotFound)
140+
141+
err = config.RemoveContext("localhost")
142+
assert.ErrorIs(t, err, ErrRemoveDefaultContext)
143+
}
144+
121145
func Test_SanitizeUrl(t *testing.T) {
122146
url := "http://localhost:8080/"
123147
expected := "http://localhost:8080"

cmd/config/remove-context.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

cmd/config/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func init() {
2020
RootCmd.AddCommand(currentContextCmd)
2121
RootCmd.AddCommand(useContextCmd)
2222
RootCmd.AddCommand(listContextCmd)
23+
RootCmd.AddCommand(removeContextCmd)
2324
}
2425

2526
func validateContextName(cmd *cobra.Command, args []string) error {

0 commit comments

Comments
 (0)