-
Notifications
You must be signed in to change notification settings - Fork 2k
context: add shell-completion for context-names #6016
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+147
−13
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// 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 | ||
|
||
package context | ||
|
||
import ( | ||
"slices" | ||
thaJeztah marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
"github.com/docker/cli/cli/context/store" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type contextProvider interface { | ||
ContextStore() store.Store | ||
CurrentContext() string | ||
} | ||
|
||
// completeContextNames implements shell completion for context-names. | ||
// | ||
// FIXME(thaJeztah): export, and remove duplicate of this function in cmd/docker. | ||
func completeContextNames(dockerCLI contextProvider, limit int, withFileComp bool) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) { | ||
return func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
if limit > 0 && len(args) >= limit { | ||
if withFileComp { | ||
// Provide file/path completion after context name (for "docker context export") | ||
return nil, cobra.ShellCompDirectiveDefault | ||
} | ||
return nil, cobra.ShellCompDirectiveNoFileComp | ||
} | ||
|
||
// TODO(thaJeztah): implement function similar to [store.Names] to (also) include descriptions. | ||
names, _ := store.Names(dockerCLI.ContextStore()) | ||
out := make([]string, 0, len(names)) | ||
for _, name := range names { | ||
if slices.Contains(args, name) { | ||
// Already completed | ||
continue | ||
} | ||
if name == dockerCLI.CurrentContext() { | ||
name += "\tcurrent" | ||
} | ||
out = append(out, name) | ||
} | ||
return out, cobra.ShellCompDirectiveNoFileComp | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package context | 8000||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/docker/cli/cli/context/store" | ||
"github.com/spf13/cobra" | ||
"gotest.tools/v3/assert" | ||
is "gotest.tools/v3/assert/cmp" | ||
) | ||
|
||
type fakeContextProvider struct { | ||
contextStore store.Store | ||
} | ||
|
||
func (c *fakeContextProvider) ContextStore() store.Store { | ||
return c.contextStore | ||
} | ||
|
||
func (*fakeContextProvider) CurrentContext() string { | ||
return "default" | ||
} | ||
|
||
type fakeContextStore struct { | ||
store.Store | ||
names []string | ||
} | ||
|
||
func (f fakeContextStore) List() (c []store.Metadata, _ error) { | ||
for _, name := range f.names { | ||
c = append(c, store.Metadata{Name: name}) | ||
} | ||
return c, nil | ||
} | ||
|
||
func TestCompleteContextNames(t *testing.T) { | ||
allNames := []string{"context-b", "context-c", "context-a"} | ||
cli := &fakeContextProvider{ | ||
contextStore: fakeContextStore{ | ||
names: allNames, | ||
}, | ||
} | ||
|
||
t.Run("with limit", func(t *testing.T) { | ||
compFunc := completeContextNames(cli, 1, false) | ||
values, directives := compFunc(nil, nil, "") | ||
assert.Check(t, is.Equal(directives, cobra.ShellCompDirectiveNoFileComp)) | ||
assert.Check(t, is.DeepEqual(values, allNames)) | ||
|
||
values, directives = compFunc(nil, []string{"context-c"}, "") | ||
assert.Check(t, is.Equal(directives, cobra.ShellCompDirectiveNoFileComp)) | ||
assert.Check(t, is.Len(values, 0)) | ||
}) | ||
|
||
t.Run("with limit and file completion", func(t *testing.T) { | ||
compFunc := completeContextNames(cli, 1, true) | ||
values, directives := compFunc(nil, nil, "") | ||
assert.Check(t, is.Equal(directives, cobra.ShellCompDirectiveNoFileComp)) | ||
assert.Check(t, is.DeepEqual(values, allNames)) | ||
|
||
values, directives = compFunc(nil, []string{"context-c"}, "") | ||
assert.Check(t, is.Equal(directives, cobra.ShellCompDirectiveDefault), "should provide filenames completion after limit") | ||
assert.Check(t, is.Len(values, 0)) | ||
}) | ||
|
||
t.Run("without limits", func(t *testing.T) { | ||
compFunc := completeContextNames(cli, -1, false) | ||
values, directives := compFunc(nil, []string{"context-c"}, "") | ||
assert.Check(t, is.Equal(directives, cobra.ShellCompDirectiveNoFileComp)) | ||
assert.Check(t, is.DeepEqual(values, []string{"context-b", "context-a"}), "should not contain already completed") | ||
|
||
values, directives = compFunc(nil, []string{"context-c", "context-a"}, "") | ||
assert.Check(t, is.Equal(directives, cobra.ShellCompDirectiveNoFileComp)) | ||
assert.Check(t, is.DeepEqual(values, []string{"context-b"}), "should not contain already completed") | ||
|
||
values, directives = compFunc(nil, []string{"context-c", "context-a", "context-b"}, "") | ||
assert.Check(t, is.Equal(directives, cobra.ShellCompDirectiveNoFileComp), "should provide filenames completion after limit") | ||
assert.Check(t, is.Len(values, 0)) | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.