mirror of https://github.com/docker/cli.git
Compare commits
5 Commits
e018b7ad75
...
2a3088b827
Author | SHA1 | Date |
---|---|---|
Harald Albers | 2a3088b827 | |
Harald Albers | 3eaad535b3 | |
Harald Albers | 4f7abb8db6 | |
Sebastiaan van Stijn | 062eecf14a | |
Harald Albers | 3f7b156c85 |
|
@ -19,6 +19,7 @@ type fakeClient struct {
|
|||
eventsFn func(context.Context, events.ListOptions) (<-chan events.Message, <-chan error)
|
||||
containerPruneFunc func(ctx context.Context, pruneFilters filters.Args) (container.PruneReport, error)
|
||||
networkPruneFunc func(ctx context.Context, pruneFilter filters.Args) (network.PruneReport, error)
|
||||
containerListFunc func(context.Context, container.ListOptions) ([]container.Summary, error)
|
||||
}
|
||||
|
||||
func (cli *fakeClient) ServerVersion(ctx context.Context) (types.Version, error) {
|
||||
|
@ -46,3 +47,10 @@ func (cli *fakeClient) NetworksPrune(ctx context.Context, pruneFilter filters.Ar
|
|||
}
|
||||
return network.PruneReport{}, nil
|
||||
}
|
||||
|
||||
func (cli *fakeClient) ContainerList(ctx context.Context, options container.ListOptions) ([]container.Summary, error) {
|
||||
if cli.containerListFunc != nil {
|
||||
return cli.containerListFunc(ctx, options)
|
||||
}
|
||||
return []container.Summary{}, nil
|
||||
}
|
||||
|
|
|
@ -154,7 +154,7 @@ func eventTypeNames() []string {
|
|||
// The list is derived from eventActions.
|
||||
// Actions that are not suitable for usage in completions are removed.
|
||||
func validEventNames() []string {
|
||||
var names []string
|
||||
names := []string{}
|
||||
for _, eventAction := range eventActions {
|
||||
if strings.Contains(string(eventAction), " ") {
|
||||
continue
|
||||
|
@ -191,7 +191,7 @@ func imageNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command) []st
|
|||
if err != nil {
|
||||
return []string{}
|
||||
}
|
||||
var names []string
|
||||
names := []string{}
|
||||
for _, img := range list {
|
||||
names = append(names, img.RepoTags...)
|
||||
}
|
||||
|
@ -205,7 +205,7 @@ func networkNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command) []
|
|||
if err != nil {
|
||||
return []string{}
|
||||
}
|
||||
var names []string
|
||||
names := []string{}
|
||||
for _, nw := range list {
|
||||
names = append(names, nw.Name)
|
||||
}
|
||||
|
@ -219,7 +219,7 @@ func nodeNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command) []str
|
|||
if err != nil {
|
||||
return []string{}
|
||||
}
|
||||
var names []string
|
||||
names := []string{}
|
||||
for _, node := range list {
|
||||
names = append(names, node.Description.Hostname)
|
||||
}
|
||||
|
@ -233,7 +233,7 @@ func volumeNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command) []s
|
|||
if err != nil {
|
||||
return []string{}
|
||||
}
|
||||
var names []string
|
||||
names := []string{}
|
||||
for _, v := range list.Volumes {
|
||||
names = append(names, v.Name)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/cli/internal/test"
|
||||
"github.com/docker/cli/internal/test/builders"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/spf13/cobra"
|
||||
"gotest.tools/v3/assert"
|
||||
)
|
||||
|
||||
// Successful completion lists all container names, prefixed with "container=".
|
||||
// Filtering the completions by the current word is delegated to the completion script.
|
||||
func TestCompleteEventFilterContainer(t *testing.T) {
|
||||
cli := test.NewFakeCli(&fakeClient{
|
||||
containerListFunc: func(_ context.Context, _ container.ListOptions) ([]container.Summary, error) {
|
||||
return []container.Summary{
|
||||
*builders.Container("foo"),
|
||||
*builders.Container("bar"),
|
||||
}, nil
|
||||
},
|
||||
})
|
||||
|
||||
completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "container=")
|
||||
|
||||
assert.DeepEqual(t, completions, []string{"container=foo", "container=bar"})
|
||||
assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||
}
|
||||
|
||||
// In case of API errors, no completions are returned.
|
||||
func TestCompleteEventFilterContainerAPIError(t *testing.T) {
|
||||
cli := test.NewFakeCli(&fakeClient{
|
||||
containerListFunc: func(_ context.Context, _ container.ListOptions) ([]container.Summary, error) {
|
||||
return nil, errors.New("API error")
|
||||
},
|
||||
})
|
||||
|
||||
completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "container=")
|
||||
|
||||
assert.DeepEqual(t, completions, []string{})
|
||||
assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||
}
|
|
@ -5081,7 +5081,7 @@ _docker_system_events() {
|
|||
return
|
||||
;;
|
||||
daemon)
|
||||
local name=$(__docker_q info | sed -n 's/^\(ID\|Name\): //p')
|
||||
local name=$(__docker_q info --format '{{.Info.Name}} {{.Info.ID}}')
|
||||
COMPREPLY=( $( compgen -W "$name" -- "${cur##*=}" ) )
|
||||
return
|
||||
;;
|
||||
|
|
Loading…
Reference in New Issue