Skip to content

Add shell auto-completion for commands and flags - #36

Merged
alicefr merged 1 commit into
mainfrom
shell-out-completion
May 29, 2026
Merged

Add shell auto-completion for commands and flags#36
alicefr merged 1 commit into
mainfrom
shell-out-completion

Conversation

@alicefr

@alicefr alicefr commented May 29, 2026

Copy link
Copy Markdown
Collaborator

Fixes: #30

Summary by Sourcery

Add shell completion support for clusters and nodes across CLI commands.

New Features:

  • Add shell completion for the --cluster-name flag based on existing bink clusters.
  • Add shell completion for node-related flags and positional arguments using current cluster containers and labels.
  • Add shell completion for the node role flag with predefined role values and control-plane node discovery.

@sourcery-ai

sourcery-ai Bot commented May 29, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds centralized shell auto-completion helpers for clusters and nodes and wires them into relevant cobra commands and flags so that cluster names, node names, and control-plane nodes can be auto-completed in the CLI.

Sequence diagram for CLI node name auto-completion

sequenceDiagram
    actor User
    participant Shell
    participant CobraCommand
    participant CLICompletion as CLICompletion
    participant PodmanClient

    User->>Shell: Request completion for node flag/arg
    Shell->>CobraCommand: Invoke completion callback
    CobraCommand->>CLICompletion: CompleteNodeNames(cmd, args, toComplete)
    CLICompletion->>PodmanClient: podman.NewClient()
    CLICompletion->>PodmanClient: ContainerList(ctx, "label=bink.cluster-name=")
    loop For each container
        CLICompletion->>PodmanClient: ContainerInspect(ctx, ctr, "bink.component")
        CLICompletion->>PodmanClient: ContainerInspect(ctx, ctr, "bink.node-name")
    end
    CLICompletion-->>CobraCommand: []string nodeNames, ShellCompDirectiveNoFileComp
    CobraCommand-->>Shell: nodeNames
    Shell-->>User: Present node name suggestions
Loading

File-Level Changes

Change Details Files
Introduce shared CLI completion helpers for clusters, nodes, and control-plane nodes backed by podman container metadata.
  • Add new internal/cli/completion.go with completion functions for cluster names, node names in the current cluster, and control-plane node names
  • Use podman client to list containers with bink-specific labels and derive completion candidates from label values
  • Guard failures in podman interactions by returning no completions and disabling file completion
internal/cli/completion.go
Wire completion helpers into existing cobra commands and flags to provide shell auto-completion for cluster-name, node selection, and node role flags.
  • Register flag completion for the global --cluster-name flag using CompleteClusterNames
  • Enable positional argument completion for node ssh and node remove using CompleteNodeNames
  • Add role flag completion (static values worker/control-plane) and control-plane flag completion using CompleteControlPlaneNodes
  • Register node flag completion for api expose using CompleteNodeNames
  • Import the new cli package where completion helpers are used
cmd/bink/main.go
internal/cli/node/add.go
internal/cli/node/ssh.go
internal/cli/node/remove.go
internal/cli/api/expose.go

Assessment against linked issues

Issue Objective Addressed Explanation
https://github.com/alicefr/bink/issues/30 Implement dynamic shell auto-completion for node-related arguments (e.g., bink node ssh <Tab> listing node names from the current cluster).
https://github.com/alicefr/bink/issues/30 Implement shell auto-completion for relevant flags (e.g., cluster name, node name, role, control-plane node selection).
https://github.com/alicefr/bink/issues/30 Provide a user-facing way to expose/enable shell completion (e.g., a dedicated command or flag such as bink shell --bash). The diff only adds Cobra completion functions and wires them to commands/flags; it does not introduce any top-level command, subcommand, or flag (such as bink shell --bash or bink completion) that outputs shell completion scripts or otherwise exposes completion setup to the user.

Possibly linked issues

  • Add shell auto-completion #30: PR introduces cobra-based shell completion, including dynamic node name completion for bink node ssh, matching the issue.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • The completion helpers all use context.Background(); consider using cmd.Context() instead so that completions respect cancellation/timeouts wired into the CLI context.
  • The three completion functions duplicate a fair amount of podman client and container-listing logic; extracting a shared helper (e.g., for listing cluster containers and pulling labels) would simplify maintenance and reduce the chance of inconsistent behavior across completions.
  • The completion functions currently ignore the toComplete prefix and always return all candidates; filtering the returned names by toComplete would make completions more efficient and aligned with typical shell completion expectations.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The completion helpers all use `context.Background()`; consider using `cmd.Context()` instead so that completions respect cancellation/timeouts wired into the CLI context.
- The three completion functions duplicate a fair amount of podman client and container-listing logic; extracting a shared helper (e.g., for listing cluster containers and pulling labels) would simplify maintenance and reduce the chance of inconsistent behavior across completions.
- The completion functions currently ignore the `toComplete` prefix and always return all candidates; filtering the returned names by `toComplete` would make completions more efficient and aligned with typical shell completion expectations.

## Individual Comments

### Comment 1
<location path="internal/cli/completion.go" line_range="16-25" />
<code_context>
+	"github.com/bootc-dev/bink/internal/podman"
+)
+
+func CompleteClusterNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
+	client, err := podman.NewClient()
+	if err != nil {
+		return nil, cobra.ShellCompDirectiveNoFileComp
+	}
+
+	ctx := context.Background()
+	containers, err := client.ContainerList(ctx, "name="+config.ContainerNamePrefix)
+	if err != nil {
+		return nil, cobra.ShellCompDirectiveNoFileComp
+	}
+
+	seen := make(map[string]bool)
+	var names []string
+	for _, ctr := range containers {
+		clusterName, err := client.ContainerInspect(ctx, ctr, `{{index .Config.Labels "bink.cluster-name"}}`)
+		if err != nil || clusterName == "" {
+			continue
+		}
+		if !seen[clusterName] {
+			seen[clusterName] = true
+			names = append(names, clusterName)
+		}
+	}
+	return names, cobra.ShellCompDirectiveNoFileComp
+}
+
</code_context>
<issue_to_address>
**suggestion (performance):** Use cmd.Context() and honor the toComplete prefix in completion functions

These helpers currently use context.Background() and ignore toComplete. Please use cmd.Context() so completion respects CLI cancellation (e.g. SIGINT), and filter the returned names by toComplete to avoid unnecessary results, especially in clusters with many containers.

Suggested implementation:

```golang
package cli

import (
	"context"
	"strings"

	"github.com/spf13/cobra"
	"github.com/spf13/viper"

	"github.com/bootc-dev/bink/internal/config"
	"github.com/bootc-dev/bink/internal/podman"
)

```

```golang
	ctx := cmd.Context()

```

```golang
		if !seen[clusterName] && strings.HasPrefix(clusterName, toComplete) {
			seen[clusterName] = true
			names = append(names, clusterName)
		}

```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread internal/cli/completion.go
Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@alicefr
alicefr force-pushed the shell-out-completion branch from e36ae15 to f1b6792 Compare May 29, 2026 07:36
@alicefr
alicefr merged commit 35e51bf into main May 29, 2026
6 checks passed
@alicefr
alicefr deleted the shell-out-completion branch June 18, 2026 10:24
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.

Add shell auto-completion

1 participant