context: add shell-completion for context-names
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>
This commit is contained in:
80
cli/command/context/completion_test.go
Normal file
80
cli/command/context/completion_test.go
Normal file
@ -0,0 +1,80 @@
|
||||
package context
|
||||
|
||||
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))
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user