Skip to content

feat(completion): add autocompletion for IDs, output and runtime#86

Open
feloy wants to merge 5 commits intokortex-hub:mainfrom
feloy:completion
Open

feat(completion): add autocompletion for IDs, output and runtime#86
feloy wants to merge 5 commits intokortex-hub:mainfrom
feloy:completion

Conversation

@feloy
Copy link
Contributor

@feloy feloy commented Mar 20, 2026

Adds intelligent shell completion that filters workspace IDs based on their runtime state. The start and remove commands show only non-running workspaces, while stop shows only running workspaces. This prevents users from attempting invalid operations and improves the CLI experience.

Adds configurable shell completion for the --output flag. Each command can specify its own list of valid output formats through the newOutputFlagCompletion factory. Currently all commands support only 'json', but the design allows easy extension for future formats.

Adds shell completion for the --runtime flag that lists all available runtimes. The completion automatically filters out the "fake" runtime which is only used for testing. Also adds ListRuntimes() method to the Manager interface to expose registered runtime types.

Fixes #85

feloy and others added 3 commits March 20, 2026 09:26
Adds intelligent shell completion that filters workspace IDs based on
their runtime state. The start and remove commands show only non-running
workspaces, while stop shows only running workspaces. This prevents users
from attempting invalid operations and improves the CLI experience.

Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
Signed-off-by: Philippe Martin <phmartin@redhat.com>
Adds configurable shell completion for the --output flag. Each command
can specify its own list of valid output formats through the
newOutputFlagCompletion factory. Currently all commands support only
'json', but the design allows easy extension for future formats.

Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
Signed-off-by: Philippe Martin <phmartin@redhat.com>
Adds shell completion for the --runtime flag that lists all available
runtimes. The completion automatically filters out the "fake" runtime
which is only used for testing. Also adds ListRuntimes() method to the
Manager interface to expose registered runtime types.

Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
Signed-off-by: Philippe Martin <phmartin@redhat.com>
@codecov-commenter
Copy link

codecov-commenter commented Mar 20, 2026

Codecov Report

❌ Patch coverage is 80.21978% with 18 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/runtimesetup/register.go 0.00% 10 Missing ⚠️
pkg/cmd/autocomplete.go 79.48% 4 Missing and 4 partials ⚠️

📢 Thoughts on this report? Let us know!

@coderabbitai
Copy link

coderabbitai bot commented Mar 20, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3d09a5ad-8295-488e-9afe-62342b3f387a

📥 Commits

Reviewing files that changed from the base of the PR and between 099da30 and 27a84b4.

📒 Files selected for processing (1)
  • pkg/cmd/autocomplete_test.go
✅ Files skipped from review due to trivial changes (1)
  • pkg/cmd/autocomplete_test.go

📝 Walkthrough

Walkthrough

Adds Cobra shell autocompletion helpers for workspace ID arguments (filtered by runtime state), output and runtime flag value completions, integrates those into several workspace/alias commands, and exposes a runtimesetup.ListAvailable() helper.

Changes

Cohort / File(s) Summary
Autocomplete core & tests
pkg/cmd/autocomplete.go, pkg/cmd/autocomplete_test.go
New helpers: getFilteredWorkspaceIDs (reads --storage, validates path, constructs instances.Manager, lists instances, filters by state), completeNonRunningWorkspaceID, completeRunningWorkspaceID, newOutputFlagCompletion, and completeRuntimeFlag. Comprehensive unit tests cover filtering, directives, and edge cases.
Workspace commands (completion wiring)
pkg/cmd/workspace_remove.go, pkg/cmd/workspace_start.go, pkg/cmd/workspace_stop.go
Register ValidArgsFunction for positional ID completion (non-running for remove/start, running for stop) and register --output flag completion (restricted to json).
Command aliases (propagate completion)
pkg/cmd/remove.go, pkg/cmd/start.go, pkg/cmd/stop.go
Alias commands now propagate the underlying workspace command ValidArgsFunction so aliases receive the same argument completion behavior.
Init & list command flag completion
pkg/cmd/init.go, pkg/cmd/workspace_list.go
Register flag completion: --runtime (uses completeRuntimeFlag) in init; --output completion limited to json in workspace list.
Runtimesetup helper
pkg/runtimesetup/register.go
Adds exported ListAvailable() which instantiates runtime factories, filters out unavailable runtimes, and returns available runtime type names.

Sequence Diagram(s)

sequenceDiagram
  participant Shell as "Shell/Cobra"
  participant Cmd as "cobra.Command (flag/arg completion)"
  participant AC as "autocomplete helper"
  participant FS as "Filesystem (storage dir)"
  participant IM as "instances.Manager"
  participant RS as "runtimesetup"

  Shell->>Cmd: user requests completion
  Cmd->>AC: invoke ValidArgsFunction / FlagCompletion
  AC->>Cmd: read global `--storage` flag
  Cmd-->>AC: flag value
  AC->>FS: resolve & stat storage path
  FS-->>AC: exists? / error
  alt storage exists
    AC->>IM: NewManager(storage)
    IM-->>AC: manager
    AC->>IM: List instances
    IM-->>AC: instances list
    AC->>AC: filter by state predicate (running/non-running)
    AC->>Cmd: return completions + ShellCompDirectiveNoFileComp
  else storage missing or errors
    AC->>Cmd: return [] + ShellCompDirectiveNoFileComp / ShellCompDirectiveError
  end
  note over Cmd,RS: separate flow for `--runtime` completion
  Cmd->>RS: ListAvailable()
  RS-->>Cmd: available runtimes (excluding "fake")
  Cmd->>Shell: runtime completions + ShellCompDirectiveNoFileComp
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding autocompletion for IDs, output, and runtime flags.
Description check ✅ Passed The description provides detailed context about intelligent shell completion for workspace IDs based on runtime state, configurable output flag completion, and runtime flag completion.
Linked Issues check ✅ Passed The PR fulfills #85 objectives by implementing intelligent autocompletion for workspace IDs filtered by runtime state, output formats, and runtime types.
Out of Scope Changes check ✅ Passed All changes are scoped to implementing shell completion for workspace IDs, output flags, and runtime flags as required by issue #85.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (2)
pkg/cmd/autocomplete_test.go (2)

424-447: Test only verifies negative case, not positive case.

This test confirms that "fake" is excluded from completions but doesn't verify that legitimate runtimes are returned. Since no runtimes are registered in the test setup, completions may be empty, making the "fake not in completions" check vacuously true.

Consider registering at least one non-fake runtime and verifying it appears in completions to strengthen this test.

💡 Suggested approach
 	t.Run("returns available runtimes excluding fake", func(t *testing.T) {
 		t.Parallel()

 		storageDir := t.TempDir()

+		// Create manager and register runtimes
+		manager, err := instances.NewManager(storageDir)
+		if err != nil {
+			t.Fatalf("failed to create manager: %v", err)
+		}
+		// Register fake runtime (should be excluded)
+		if err := manager.RegisterRuntime(fake.New()); err != nil {
+			t.Fatalf("failed to register fake runtime: %v", err)
+		}
+		// TODO: Register a real runtime (e.g., podman) to verify it IS returned

 		// Create a command with the storage flag
 		cmd := &cobra.Command{}
 		cmd.Flags().String("storage", storageDir, "test storage flag")

 		// Call completion function
 		completions, directive := completeRuntimeFlag(cmd, []string{}, "")

 		// Verify "fake" is not in the completions
 		for _, completion := range completions {
 			if completion == "fake" {
 				t.Errorf("Expected 'fake' runtime to be filtered out, but it was included")
 			}
 		}

+		// TODO: Verify that registered non-fake runtimes ARE in completions
+		// if len(completions) == 0 {
+		//     t.Errorf("Expected at least one runtime completion")
+		// }

 		// Verify directive
 		if directive != cobra.ShellCompDirectiveNoFileComp {
 			t.Errorf("Expected ShellCompDirectiveNoFileComp, got %v", directive)
 		}
 	})
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/cmd/autocomplete_test.go` around lines 424 - 447, The test only checks
that "fake" is absent but never ensures any real runtimes are returned; update
the test that calls completeRuntimeFlag to register at least one real runtime
before invoking it (e.g., create/register a runtime named "node" or "docker" via
the same runtime registration API used in production), then assert that the
expected runtime appears in the completions and that "fake" is not present,
keeping the existing checks for the directive and using the same cmd with
cmd.Flags().String("storage", storageDir, ...).

136-155: Test name and setup are misaligned.

The test is named "returns empty list when list fails" but the setup doesn't actually cause the list operation to fail—it uses a valid temp directory with no corrupted data. The comment on lines 140-141 mentions "List() may fail on corrupted data" but no corruption is introduced.

Additionally, the assertion on lines 152-154 is very permissive, accepting either empty completions OR an error directive, which doesn't match the specific behavior described in the test name.

Consider either:

  1. Renaming to "returns empty list when no instances exist" (which matches what's actually tested), or
  2. Actually inducing a failure condition (e.g., corrupting the storage directory structure)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/cmd/autocomplete_test.go` around lines 136 - 155, The test name and
assertions don't match the setup: the test uses a valid temp storageDir and
doesn't induce List() failure for completeNonRunningWorkspaceID, so either
rename the t.Run to "returns empty list when no instances exist" and update the
comment and assertion to expect len(completions)==0 and
directive==cobra.ShellCompDirectiveNoFileComp, or actually cause List() to fail
(e.g., create storageDir then write corrupt data or invalid permissions before
calling completeNonRunningWorkspaceID) and keep the current name but assert that
directive==cobra.ShellCompDirectiveError; update related variables (storageDir,
cmd) and comments accordingly to reflect the chosen approach.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@pkg/cmd/autocomplete.go`:
- Around line 21-27: The completion handlers are calling instances.NewManager(),
which creates storage dirs; change the completion code to avoid creating on-disk
state by checking whether the storage path exists before instantiating a
manager. Specifically, in the autocomplete command completion functions that
call instances.NewManager(), first query the runtime storage path (via
runtimesetup or the same config source used elsewhere) and if the path does not
exist/is empty, short-circuit and return no suggestions; otherwise only then
call instances.NewManager(). Alternatively, if instances.NewManager supports a
non-creating option, use that (e.g., a constructor flag like "no-create");
update all occurrences (the calls around instances.NewManager() noted in the
review) to apply this guard so tab-completion never creates directories.

In `@pkg/instances/manager_test.go`:
- Around line 1614-1617: The test currently asserts the fake runtime by checking
runtimes[0] which is order-dependent; change the assertion to check that the
"runtimes" slice contains "fake" (e.g., iterate runtimes or use a helper
contains function) and fail the test if not found so the check is
order-independent—update the assertion around the runtimes variable in the test
(the block that currently does if len(runtimes) > 0 && runtimes[0] != "fake") to
verify containment instead.

---

Nitpick comments:
In `@pkg/cmd/autocomplete_test.go`:
- Around line 424-447: The test only checks that "fake" is absent but never
ensures any real runtimes are returned; update the test that calls
completeRuntimeFlag to register at least one real runtime before invoking it
(e.g., create/register a runtime named "node" or "docker" via the same runtime
registration API used in production), then assert that the expected runtime
appears in the completions and that "fake" is not present, keeping the existing
checks for the directive and using the same cmd with
cmd.Flags().String("storage", storageDir, ...).
- Around line 136-155: The test name and assertions don't match the setup: the
test uses a valid temp storageDir and doesn't induce List() failure for
completeNonRunningWorkspaceID, so either rename the t.Run to "returns empty list
when no instances exist" and update the comment and assertion to expect
len(completions)==0 and directive==cobra.ShellCompDirectiveNoFileComp, or
actually cause List() to fail (e.g., create storageDir then write corrupt data
or invalid permissions before calling completeNonRunningWorkspaceID) and keep
the current name but assert that directive==cobra.ShellCompDirectiveError;
update related variables (storageDir, cmd) and comments accordingly to reflect
the chosen approach.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 12fc07cf-740b-4fa7-91fc-8e3e2a849e9c

📥 Commits

Reviewing files that changed from the base of the PR and between 071ae05 and 2966216.

📒 Files selected for processing (12)
  • pkg/cmd/autocomplete.go
  • pkg/cmd/autocomplete_test.go
  • pkg/cmd/init.go
  • pkg/cmd/remove.go
  • pkg/cmd/start.go
  • pkg/cmd/stop.go
  • pkg/cmd/workspace_list.go
  • pkg/cmd/workspace_remove.go
  • pkg/cmd/workspace_start.go
  • pkg/cmd/workspace_stop.go
  • pkg/instances/manager.go
  • pkg/instances/manager_test.go

@feloy feloy requested review from benoitf and jeffmaury March 20, 2026 10:55
@feloy feloy marked this pull request as draft March 20, 2026 11:01
Tab-completion was calling instances.NewManager() which creates
storage directories as a side effect. Modified completion handlers
to check if storage exists before instantiating a manager for
workspace IDs, and added runtimesetup.ListAvailable() for runtime
flag completion that doesn't require a manager instance.

Removed the now-unused Manager.ListRuntimes() method.

Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
Signed-off-by: Philippe Martin <phmartin@redhat.com>
@feloy feloy marked this pull request as ready for review March 20, 2026 11:33
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@pkg/cmd/autocomplete_test.go`:
- Around line 136-154: The test uses t.TempDir() which exists so it won't
simulate a failing List; change storageDir to a non-existent path (e.g.,
filepath.Join(t.TempDir(), "missing")) so manager/List will fail when
completeNonRunningWorkspaceID is called, and tighten the expectation to assert a
deterministic failure: require completions to be empty and directive to equal
cobra.ShellCompDirectiveError; update the test setup where
cmd.Flags().String("storage", storageDir, ...) and the final assertion to check
both len(completions) == 0 and directive == cobra.ShellCompDirectiveError.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: bbdbe2a3-1ddd-446d-8ab7-20ac00eb303e

📥 Commits

Reviewing files that changed from the base of the PR and between 2966216 and 099da30.

📒 Files selected for processing (3)
  • pkg/cmd/autocomplete.go
  • pkg/cmd/autocomplete_test.go
  • pkg/runtimesetup/register.go

Signed-off-by: Philippe Martin <phmartin@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

enhance autocompletion to auto-complete args (ID, etc)

2 participants