Skip to content

context: add shell-completion for context-names - #6016

Merged
vvoland merged 1 commit into
docker:masterfrom
thaJeztah:context_completion
Apr 17, 2025
Merged

context: add shell-completion for context-names#6016
vvoland merged 1 commit into
docker:masterfrom
thaJeztah:context_completion

Conversation

@thaJeztah

@thaJeztahthaJeztah commented Apr 16, 2025

Copy link
Copy Markdown
Member

For now, these are not exported and included in the cli/commands/contexts
package; a copy of this also lives in cmd/docker, but we need to find a
good place for these completions, as some of them bring in additional
dependencies.

Commands that accept multiple arguments provide completion, but removing
duplicates:

docker context inspect<TAB>
default desktop-linux (current) production tcd
docker context inspec default<TAB>
desktop-linux (current) production tcd
docker context inspect default tcd<TAB>
desktop-linux (current) production

For "context export", we provide completion for the first argument, after
which file-completion is provided:

# provides context names completion for the first argument
docker context export production<TAB>
default desktop-linux (current) production tcd
# then provides completion for filenames
docker context export desktop-linux<TAB>
build/ man/ TESTING.md
cli/ docker.Makefile go.mod
...

- What I did

- How I did it

- How to verify it

- Human readable description for the release notes

Add CLI shell-completion for context names

- A picture of a cute animal (not mandatory but encouraged)

Comment on lines 7 to 10
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/cli/context/store"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/network"

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Starting to consider if we should have sub-packages here, otherwise cli/command/completion will pull in too many other packages. We can either use cli/command/completion/xxx or alternatively, per command (cli/command/container/completion). Or .. maybe it should live elsewhere 🤔

@codecov-commenter

codecov-commenter commented Apr 16, 2025

Copy link
Copy Markdown

Comment threadcli/command/completion/functions.go Outdated
Comment on lines +55 to +59
// ContextNames implements shell completion for context-names.
func ContextNames(dockerCLI contextProvider) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
return func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
// TODO(thaJeztah): implement function similar to [store.Names] to (also) include descriptions.
names, _ := store.Names(dockerCLI.ContextStore())

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Missing some options here to differentiate for different commands;

  • some only accept a single arg
  • some should not include the current context (probably)
  • ideally, we'd be able to also add the context's description (which could be useful, if short that is)

Comment threadcli/command/completion/functions.go Outdated
names[i] += "\tcurrent"
}
}
return names, cobra.ShellCompDirectiveNoFileComp

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Should we have "no space at end" option for positional arguments?

@thaJeztah
thaJeztahforce-pushed the context_completion branch 3 times, most recently from b04c106 to 1a7d7d6CompareApril 17, 2025 08:11
@thaJeztahthaJeztah self-assigned this Apr 17, 2025
@thaJeztahthaJeztah added this to the 28.1.0 milestone Apr 17, 2025
@thaJeztah
thaJeztah marked this pull request as ready for review April 17, 2025 08:12
@thaJeztah

Copy link
Copy Markdown
MemberAuthor

I have one more small enhancement to not add a space after the limit is reached (and we don't use file-completion after it); let me push

@thaJeztah

Copy link
Copy Markdown
MemberAuthor

Oh, actually; not sure that works; let's go with what we have here for now

Comment threadcli/command/context/completion.go
For now, these are not exported and included in the cli/commands/contexts
package; a copy of this also lives in cmd/docker, but we need to find a
good place for these completions, as some of them bring in additional
dependencies.
Commands that accept multiple arguments provide completion, but removing
duplicates:
docker context inspect<TAB>
default desktop-linux (current) production tcd
docker context inspec default<TAB>
desktop-linux (current) production tcd
docker context inspect default tcd<TAB>
desktop-linux (current) production
For "context export", we provide completion for the first argument, after
which file-completion is provided:
# provides context names completion for the first argument
docker context export production<TAB>
default desktop-linux (current) production tcd
# then provides completion for filenames
docker context export desktop-linux<TAB>
build/ man/ TESTING.md
cli/ docker.Makefile go.mod
...
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Comment on lines +1 to +2
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
//go:build go1.22

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Note to self; need to update #5868 after this is merged.

@vvoland
vvoland merged commit 0e75283 into docker:masterApr 17, 2025
@thaJeztah
thaJeztah deleted the context_completion branch April 17, 2025 08:42
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@thaJeztah@codecov-commenter@vvoland@Benehiko