Skip to content

fix: support listing and deleting orphaned or invalid contexts (#631) - #641

Open
aryarathoree wants to merge 1 commit into
microcks:masterfrom
aryarathoree:fix/orphaned-context-list-delete
Open

aryarathoree wants to merge 1 commit into
microcks:masterfrom
aryarathoree:fix/orphaned-context-list-delete

Conversation

@aryarathoree

Copy link
Copy Markdown
Contributor

Resolve contexts gracefully when listing so that an orphaned or invalid context does not block viewing other valid contexts. Remove contexts directly in deleteContext without requiring full context resolution, and clean up unreferenced users and servers idempotently.

Fixes #631

Description

  • Graceful Context Listing: Updated listMicrocksContexts in cmd/context.go to construct summaries directly from contextRef.Name and contextRef.Server using GetServer when available, rather than failing the entire listing loop with ResolveContext if an orphaned context references an undefined server or user.
  • Unblock Context Deletion: Updated deleteContext in cmd/context.go to remove contexts directly from localCfg.Contexts without requiring ResolveContext(context) to resolve first, enabling users to clean up broken contexts via microcks context <name> --delete.
  • Idempotent Resource Cleanup: Made garbage collection of unreferenced users (RemoveUser) and servers (RemoveServer) idempotent so that already-absent resources do not trigger fatal errors (errors.KindAPI) or prevent saving configuration changes.
  • Guard Configuration Deletion: Ensured the configuration file is only deleted on disk when both localCfg.IsEmpty() and len(localCfg.Contexts) == 0.
  • Unit Tests: Added regression tests in cmd/context_test.go covering JSON and text context listing with orphaned entries, programmatic and CLI context deletion, and complete cleanup when deleting the sole context.

…cks#631)

Resolve contexts gracefully when listing so that an orphaned or invalid context does not block viewing other valid contexts. Remove contexts directly in deleteContext without requiring full context resolution, and clean up unreferenced users and servers idempotently.

Fixes microcks#631

Signed-off-by: Arya Rathore <aryarathoree@gmail.com>

@Harsh4902 Harsh4902 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@aryarathoree Please have a look on a couple of suggestions. Happy to merge after resolving them.

Comment thread cmd/context.go
}

if localCfg.IsEmpty() {
if localCfg.IsEmpty() && len(localCfg.Contexts) == 0 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IsEmpty() only checks servers, so this can still delete a config containing a local instance’s container ID. Could we coordinate this guard with #642 and test that deleting a context preserves the records needed to stop a running instance?

Comment thread cmd/context.go
if err != nil {
return errors.Wrap(errors.KindEnvironment, err)
}
targetContext := localCfg.Contexts[contextIndex]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you add a CLI regression test with current-context: broken-ctx? The command calls ReadLocalConfig before dispatching, and that validates the current context. If its server or user is missing, both list and delete still fail before reaching the new code.

@Harsh4902 Harsh4902 added component/cli keep-open Explicitily keep open go Pull requests that update go code labels Nov 26, 2026
@Harsh4902 Harsh4902 added this to the Next milestone Nov 26, 2026

This branch was successfully deployed

1 active deployment
Build — 8171e483 Deployed Nov 25, 2026 by aryarathoree via build-verify-package #907
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component/cli go Pull requests that update go code keep-open Explicitily keep open

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot list or delete contexts when an orphaned or invalid context exists in local config

2 participants