Skip to content
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
7 changes: 4 additions & 3 deletions cmd/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ func deleteContext(context, configPath string) error {
if localCfg == nil {
return fmt.Errorf("Nothing to logout from")
}
serverName, ok := localCfg.RemoveContext(context)
contextRef, ok := localCfg.RemoveContext(context)
if !ok {
return fmt.Errorf("Context %s does not exist", context)
}
_ = localCfg.RemoveUser(context)
_ = localCfg.RemoveServer(serverName)
_ = localCfg.RemoveUser(contextRef.User)
_ = localCfg.RemoveServer(contextRef.Server)
_ = localCfg.RemoveAuth(contextRef.Server)

if localCfg.IsEmpty() {
err := localCfg.DeleteLocalConfig(configPath)
Expand Down
54 changes: 54 additions & 0 deletions cmd/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,57 @@ func TestDeleteContext(t *testing.T) {
_, err = config.ReadLocalConfig(testConfigFilePath)
require.NoError(t, err)
}

func TestDeleteNamedContextRemovesReferencedUserAndAuth(t *testing.T) {
configPath := t.TempDir() + "/config"
localConfig := `current-context: staging
contexts:
- name: dev
server: http://localhost:8080
user: http://localhost:8080
instance: ""
- name: staging
server: http://localhost:8083
user: http://localhost:8083
instance: ""
servers:
- name: ""
server: http://localhost:8080
insecureTLS: true
keycloakEnable: true
- name: ""
server: http://localhost:8083
insecureTLS: true
keycloakEnable: true
users:
- name: http://localhost:8080
auth-token: stale-token
refresh-token: stale-refresh-token
- name: http://localhost:8083
auth-token: ""
refresh-token: ""
auths:
- server: http://localhost:8080
clientid: my-client
clientsecret: my-secret
`

err := os.WriteFile(configPath, []byte(localConfig), os.ModePerm)
require.NoError(t, err)
err = os.Chmod(configPath, 0o600)
require.NoError(t, err)

err = deleteContext("dev", configPath)
require.NoError(t, err)

localCfg, err := config.ReadLocalConfig(configPath)
require.NoError(t, err)
require.NotNil(t, localCfg)

assert.Equal(t, "staging", localCfg.CurrentContext)
assert.NotContains(t, localCfg.Contexts, config.ContextRef{Name: "dev", Server: "http://localhost:8080", User: "http://localhost:8080", Instance: ""})
assert.NotContains(t, localCfg.Servers, config.Server{Server: "http://localhost:8080", InsecureTLS: true, KeycloakEnable: true})
assert.NotContains(t, localCfg.Users, config.User{Name: "http://localhost:8080", AuthToken: "stale-token", RefreshToken: "stale-refresh-token"})
assert.NotContains(t, localCfg.Auths, config.Auth{Server: "http://localhost:8080", ClientId: "my-client", ClientSecret: "my-secret"})
assert.Contains(t, localCfg.Contexts, config.ContextRef{Name: "staging", Server: "http://localhost:8083", User: "http://localhost:8083", Instance: ""})
}
10 changes: 5 additions & 5 deletions pkg/config/localconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,15 @@ func (l *LocalConfig) UpsertContext(context ContextRef) {
l.Contexts = append(l.Contexts, context)
}

// RemoveContext and returns server name and true if context was removed successfully
func (l *LocalConfig) RemoveContext(serverName string) (string, bool) {
// RemoveContext removes a context reference and returns it if context was removed successfully.
func (l *LocalConfig) RemoveContext(name string) (ContextRef, bool) {
for i, c := range l.Contexts {
if c.Name == serverName {
if c.Name == name {
l.Contexts = append(l.Contexts[:i], l.Contexts[i+1:]...)
return c.Server, true
return c, true
}
}
return "", false
return ContextRef{}, false
}

// RemoveToken and returns true if user was removed successfully
Expand Down