mirror of https://github.com/docker/cli.git
Compare commits
17 Commits
24a2740fc7
...
aa1d172257
Author | SHA1 | Date |
---|---|---|
Harald Albers | aa1d172257 | |
Sebastiaan van Stijn | abb8e9b78a | |
Laura Brehm | 7029147458 | |
Harald Albers | 3517b1bf87 | |
Sebastiaan van Stijn | 6b9083776f | |
Sebastiaan van Stijn | fb61156b05 | |
Harald Albers | 6baf2a0347 | |
Harald Albers | 3eaad535b3 | |
Harald Albers | 4f7abb8db6 | |
Sebastiaan van Stijn | 87acf77aef | |
Harald Albers | 8ae6ad6256 | |
Harald Albers | c7af8da7b9 | |
Harald Albers | c5306f3c1a | |
Harald Albers | 3f3ff40ee4 | |
Harald Albers | c17b87a713 | |
Harald Albers | a09c2047d4 | |
Harald Albers | 616b7c974b |
|
@ -58,9 +58,9 @@ func NewLoginCommand(dockerCli command.Cli) *cobra.Command {
|
|||
return cmd
|
||||
}
|
||||
|
||||
func verifyloginOptions(dockerCli command.Cli, opts *loginOptions) error {
|
||||
func verifyLoginOptions(dockerCli command.Cli, opts *loginOptions) error {
|
||||
if opts.password != "" {
|
||||
fmt.Fprintln(dockerCli.Err(), "WARNING! Using --password via the CLI is insecure. Use --password-stdin.")
|
||||
_, _ = fmt.Fprintln(dockerCli.Err(), "WARNING! Using --password via the CLI is insecure. Use --password-stdin.")
|
||||
if opts.passwordStdin {
|
||||
return errors.New("--password and --password-stdin are mutually exclusive")
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ func verifyloginOptions(dockerCli command.Cli, opts *loginOptions) error {
|
|||
}
|
||||
|
||||
func runLogin(ctx context.Context, dockerCli command.Cli, opts loginOptions) error {
|
||||
if err := verifyloginOptions(dockerCli, &opts); err != nil {
|
||||
if err := verifyLoginOptions(dockerCli, &opts); err != nil {
|
||||
return err
|
||||
}
|
||||
var (
|
||||
|
@ -174,7 +174,7 @@ func loginUser(ctx context.Context, dockerCli command.Cli, opts loginOptions, de
|
|||
if !errors.Is(err, manager.ErrDeviceLoginStartFail) {
|
||||
return response, err
|
||||
}
|
||||
fmt.Fprint(dockerCli.Err(), "Failed to start web-based login - falling back to command line login...\n\n")
|
||||
_, _ = fmt.Fprint(dockerCli.Err(), "Failed to start web-based login - falling back to command line login...\n\n")
|
||||
}
|
||||
|
||||
return loginWithUsernameAndPassword(ctx, dockerCli, opts, defaultUsername, serverAddress)
|
||||
|
|
|
@ -3,6 +3,8 @@ package system
|
|||
import (
|
||||
"context"
|
||||
|
||||
"github.com/docker/docker/api/types/system"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/events"
|
||||
|
@ -19,6 +21,9 @@ 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)
|
||||
infoFunc func(ctx context.Context) (system.Info, error)
|
||||
networkListFunc func(ctx context.Context, options network.ListOptions) ([]network.Summary, error)
|
||||
}
|
||||
|
||||
func (cli *fakeClient) ServerVersion(ctx context.Context) (types.Version, error) {
|
||||
|
@ -46,3 +51,24 @@ 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
|
||||
}
|
||||
|
||||
func (cli *fakeClient) Info(ctx context.Context) (system.Info, error) {
|
||||
if cli.infoFunc != nil {
|
||||
return cli.infoFunc(ctx)
|
||||
}
|
||||
return system.Info{}, nil
|
||||
}
|
||||
|
||||
func (cli *fakeClient) NetworkList(ctx context.Context, options network.ListOptions) ([]network.Summary, error) {
|
||||
if cli.networkListFunc != nil {
|
||||
return cli.networkListFunc(ctx, options)
|
||||
}
|
||||
return []network.Summary{}, nil
|
||||
}
|
||||
|
|
|
@ -0,0 +1,241 @@
|
|||
package system
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/image"
|
||||
"github.com/docker/docker/api/types/network"
|
||||
"github.com/docker/docker/api/types/volume"
|
||||
|
||||
"github.com/docker/docker/api/types/events"
|
||||
|
||||
"github.com/docker/cli/cli/command/completion"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
eventFilters = []string{"container", "daemon", "event", "image", "label", "network", "node", "scope", "type", "volume"}
|
||||
|
||||
// eventTypes is a list of all event types.
|
||||
// This should be moved to the moby codebase once its usage is consolidated here.
|
||||
eventTypes = []events.Type{
|
||||
events.BuilderEventType,
|
||||
events.ConfigEventType,
|
||||
events.ContainerEventType,
|
||||
events.DaemonEventType,
|
||||
events.ImageEventType,
|
||||
events.NetworkEventType,
|
||||
events.NodeEventType,
|
||||
events.PluginEventType,
|
||||
events.SecretEventType,
|
||||
events.ServiceEventType,
|
||||
events.VolumeEventType,
|
||||
}
|
||||
|
||||
// eventActions is a list of all event actions.
|
||||
// This should be moved to the moby codebase once its usage is consolidated here.
|
||||
eventActions = []events.Action{
|
||||
events.ActionCreate,
|
||||
events.ActionStart,
|
||||
events.ActionRestart,
|
||||
events.ActionStop,
|
||||
events.ActionCheckpoint,
|
||||
events.ActionPause,
|
||||
events.ActionUnPause,
|
||||
events.ActionAttach,
|
||||
events.ActionDetach,
|
||||
events.ActionResize,
|
||||
events.ActionUpdate,
|
||||
events.ActionRename,
|
||||
events.ActionKill,
|
||||
events.ActionDie,
|
||||
events.ActionOOM,
|
||||
events.ActionDestroy,
|
||||
events.ActionRemove,
|
||||
events.ActionCommit,
|
||||
events.ActionTop,
|
||||
events.ActionCopy,
|
||||
events.ActionArchivePath,
|
||||
events.ActionExtractToDir,
|
||||
events.ActionExport,
|
||||
events.ActionImport,
|
||||
events.ActionSave,
|
||||
events.ActionLoad,
|
||||
events.ActionTag,
|
||||
events.ActionUnTag,
|
||||
events.ActionPush,
|
||||
events.ActionPull,
|
||||
events.ActionPrune,
|
||||
events.ActionDelete,
|
||||
events.ActionEnable,
|
||||
events.ActionDisable,
|
||||
events.ActionConnect,
|
||||
events.ActionDisconnect,
|
||||
events.ActionReload,
|
||||
events.ActionMount,
|
||||
events.ActionUnmount,
|
||||
events.ActionExecCreate,
|
||||
events.ActionExecStart,
|
||||
events.ActionExecDie,
|
||||
events.ActionExecDetach,
|
||||
events.ActionHealthStatus,
|
||||
events.ActionHealthStatusRunning,
|
||||
events.ActionHealthStatusHealthy,
|
||||
events.ActionHealthStatusUnhealthy,
|
||||
}
|
||||
)
|
||||
|
||||
// completeFilters provides completion for the filters that can be used with `--filter`.
|
||||
func completeFilters(dockerCLI completion.APIClientProvider) completion.ValidArgsFn {
|
||||
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||
if strings.HasPrefix(toComplete, "container=") {
|
||||
return prefixWith("container=", containerNames(dockerCLI, cmd, args, toComplete)), cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
if strings.HasPrefix(toComplete, "daemon=") {
|
||||
return prefixWith("daemon=", daemonNames(dockerCLI, cmd)), cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
if strings.HasPrefix(toComplete, "event=") {
|
||||
return prefixWith("event=", validEventNames()), cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
if strings.HasPrefix(toComplete, "image=") {
|
||||
return prefixWith("image=", imageNames(dockerCLI, cmd)), cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
if strings.HasPrefix(toComplete, "label=") {
|
||||
return nil, cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
if strings.HasPrefix(toComplete, "network=") {
|
||||
return prefixWith("network=", networkNames(dockerCLI, cmd)), cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
if strings.HasPrefix(toComplete, "node=") {
|
||||
return prefixWith("node=", nodeNames(dockerCLI, cmd)), cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
if strings.HasPrefix(toComplete, "scope=") {
|
||||
return prefixWith("scope=", []string{"local", "swarm"}), cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
if strings.HasPrefix(toComplete, "type=") {
|
||||
return prefixWith("type=", eventTypeNames()), cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
if strings.HasPrefix(toComplete, "volume=") {
|
||||
return prefixWith("volume=", volumeNames(dockerCLI, cmd)), cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
|
||||
return postfixWith("=", eventFilters), cobra.ShellCompDirectiveNoSpace
|
||||
}
|
||||
}
|
||||
|
||||
func prefixWith(prefix string, values []string) []string {
|
||||
result := make([]string, len(values))
|
||||
for i, v := range values {
|
||||
result[i] = prefix + v
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func postfixWith(postfix string, values []string) []string {
|
||||
result := make([]string, len(values))
|
||||
for i, v := range values {
|
||||
result[i] = v + postfix
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// eventTypeNames provides a list of all event types.
|
||||
// The list is derived from eventTypes.
|
||||
func eventTypeNames() []string {
|
||||
names := make([]string, len(eventTypes))
|
||||
for i, eventType := range eventTypes {
|
||||
names[i] = string(eventType)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// validEventNames provides a list of all event actions.
|
||||
// The list is derived from eventActions.
|
||||
// Actions that are not suitable for usage in completions are removed.
|
||||
func validEventNames() []string {
|
||||
names := []string{}
|
||||
for _, eventAction := range eventActions {
|
||||
if strings.Contains(string(eventAction), " ") {
|
||||
continue
|
||||
}
|
||||
names = append(names, string(eventAction))
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// containerNames contacts the API to get names and optionally IDs of containers.
|
||||
// In case of an error, an empty list is returned.
|
||||
func containerNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command, args []string, toComplete string) []string {
|
||||
names, _ := completion.ContainerNames(dockerCLI, true)(cmd, args, toComplete)
|
||||
if names == nil {
|
||||
return []string{}
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// daemonNames contacts the API to get name and ID of the current docker daemon.
|
||||
// In case of an error, an empty list is returned.
|
||||
func daemonNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command) []string {
|
||||
info, err := dockerCLI.Client().Info(cmd.Context())
|
||||
if err != nil {
|
||||
return []string{}
|
||||
}
|
||||
return []string{info.Name, info.ID}
|
||||
}
|
||||
|
||||
// imageNames contacts the API to get a list of image names.
|
||||
// In case of an error, an empty list is returned.
|
||||
func imageNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command) []string {
|
||||
list, err := dockerCLI.Client().ImageList(cmd.Context(), image.ListOptions{})
|
||||
if err != nil {
|
||||
return []string{}
|
||||
}
|
||||
names := []string{}
|
||||
for _, img := range list {
|
||||
names = append(names, img.RepoTags...)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// networkNames contacts the API to get a list of network names.
|
||||
// In case of an error, an empty list is returned.
|
||||
func networkNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command) []string {
|
||||
list, err := dockerCLI.Client().NetworkList(cmd.Context(), network.ListOptions{})
|
||||
if err != nil {
|
||||
return []string{}
|
||||
}
|
||||
names := []string{}
|
||||
for _, nw := range list {
|
||||
names = append(names, nw.Name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// nodeNames contacts the API to get a list of node names.
|
||||
// In case of an error, an empty list is returned.
|
||||
func nodeNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command) []string {
|
||||
list, err := dockerCLI.Client().NodeList(cmd.Context(), types.NodeListOptions{})
|
||||
if err != nil {
|
||||
return []string{}
|
||||
}
|
||||
names := []string{}
|
||||
for _, node := range list {
|
||||
names = append(names, node.Description.Hostname)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// volumeNames contacts the API to get a list of volume names.
|
||||
// In case of an error, an empty list is returned.
|
||||
func volumeNames(dockerCLI completion.APIClientProvider, cmd *cobra.Command) []string {
|
||||
list, err := dockerCLI.Client().VolumeList(cmd.Context(), volume.ListOptions{})
|
||||
if err != nil {
|
||||
return []string{}
|
||||
}
|
||||
names := []string{}
|
||||
for _, v := range list.Volumes {
|
||||
names = append(names, v.Name)
|
||||
}
|
||||
return names
|
||||
}
|
|
@ -0,0 +1,113 @@
|
|||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types/system"
|
||||
|
||||
"github.com/docker/docker/api/types/network"
|
||||
|
||||
"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("c1"),
|
||||
*builders.Container("c2"),
|
||||
}, nil
|
||||
},
|
||||
})
|
||||
|
||||
completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "container=")
|
||||
|
||||
assert.DeepEqual(t, completions, []string{"container=c1", "container=c2"})
|
||||
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)
|
||||
}
|
||||
|
||||
// Successful completion lists the name and id of the current Docker daemon, prefixed with "daemon=".
|
||||
// Filtering the completions by the current word is delegated to the completion script.
|
||||
func TestCompleteEventFilterDaemon(t *testing.T) {
|
||||
cli := test.NewFakeCli(&fakeClient{
|
||||
infoFunc: func(ctx context.Context) (system.Info, error) {
|
||||
return system.Info{
|
||||
ID: "daemon-id",
|
||||
Name: "daemon-name",
|
||||
}, nil
|
||||
},
|
||||
})
|
||||
|
||||
completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "daemon=")
|
||||
|
||||
assert.DeepEqual(t, completions, []string{"daemon=daemon-name", "daemon=daemon-id"})
|
||||
assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||
}
|
||||
|
||||
// In case of API errors, no completions are returned.
|
||||
func TestCompleteEventFilterDaemonAPIError(t *testing.T) {
|
||||
cli := test.NewFakeCli(&fakeClient{
|
||||
infoFunc: func(ctx context.Context) (system.Info, error) {
|
||||
return system.Info{}, errors.New("API error")
|
||||
},
|
||||
})
|
||||
|
||||
completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "daemon=")
|
||||
|
||||
assert.DeepEqual(t, completions, []string{})
|
||||
assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||
}
|
||||
|
||||
// Successful completion lists all network names, prefixed with "network=".
|
||||
// Filtering the completions by the current word is delegated to the completion script.
|
||||
func TestCompleteEventFilterNetwork(t *testing.T) {
|
||||
cli := test.NewFakeCli(&fakeClient{
|
||||
networkListFunc: func(_ context.Context, _ network.ListOptions) ([]network.Summary, error) {
|
||||
return []network.Summary{
|
||||
*builders.NetworkResource(builders.NetworkResourceName("nw1")),
|
||||
*builders.NetworkResource(builders.NetworkResourceName("nw2")),
|
||||
}, nil
|
||||
},
|
||||
})
|
||||
|
||||
completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "network=")
|
||||
|
||||
assert.DeepEqual(t, completions, []string{"network=nw1", "network=nw2"})
|
||||
assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||
}
|
||||
|
||||
// In case of API errors, no completions are returned.
|
||||
func TestCompleteEventFilterNetworkAPIError(t *testing.T) {
|
||||
cli := test.NewFakeCli(&fakeClient{
|
||||
networkListFunc: func(_ context.Context, _ network.ListOptions) ([]network.Summary, error) {
|
||||
return nil, errors.New("API error")
|
||||
},
|
||||
})
|
||||
|
||||
completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "network=")
|
||||
|
||||
assert.DeepEqual(t, completions, []string{})
|
||||
assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||
}
|
|
@ -50,6 +50,8 @@ func NewEventsCommand(dockerCli command.Cli) *cobra.Command {
|
|||
flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided")
|
||||
flags.StringVar(&options.format, "format", "", flagsHelper.InspectFormatHelp) // using the same flag description as "inspect" commands for now.
|
||||
|
||||
_ = cmd.RegisterFlagCompletionFunc("filter", completeFilters(dockerCli))
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
|
|
|
@ -199,7 +199,7 @@ func PruneFilters(dockerCli Cli, pruneFilters filters.Args) filters.Args {
|
|||
// AddPlatformFlag adds `platform` to a set of flags for API version 1.32 and later.
|
||||
func AddPlatformFlag(flags *pflag.FlagSet, target *string) {
|
||||
flags.StringVar(target, "platform", os.Getenv("DOCKER_DEFAULT_PLATFORM"), "Set platform if server is multi-platform capable")
|
||||
flags.SetAnnotation("platform", "version", []string{"1.32"})
|
||||
_ = flags.SetAnnotation("platform", "version", []string{"1.32"})
|
||||
}
|
||||
|
||||
// ValidateOutputPath validates the output paths of the `export` and `save` commands.
|
||||
|
|
|
@ -5,7 +5,9 @@ import (
|
|||
"strconv"
|
||||
)
|
||||
|
||||
// Enabled returns whether cli hints are enabled or not
|
||||
// Enabled returns whether cli hints are enabled or not. Hints are enabled by
|
||||
// default, but can be disabled through the "DOCKER_CLI_HINTS" environment
|
||||
// variable.
|
||||
func Enabled() bool {
|
||||
if v := os.Getenv("DOCKER_CLI_HINTS"); v != "" {
|
||||
enabled, err := strconv.ParseBool(v)
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
package hints
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gotest.tools/v3/assert"
|
||||
)
|
||||
|
||||
func TestEnabled(t *testing.T) {
|
||||
tests := []struct {
|
||||
doc string
|
||||
env string
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
doc: "default",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
doc: "DOCKER_CLI_HINTS=1",
|
||||
env: "1",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
doc: "DOCKER_CLI_HINTS=true",
|
||||
env: "true",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
doc: "DOCKER_CLI_HINTS=0",
|
||||
env: "0",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
doc: "DOCKER_CLI_HINTS=false",
|
||||
env: "false",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
doc: "DOCKER_CLI_HINTS=not-a-bool",
|
||||
env: "not-a-bool",
|
||||
expected: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.doc, func(t *testing.T) {
|
||||
t.Setenv("DOCKER_CLI_HINTS", tc.env)
|
||||
assert.Equal(t, Enabled(), tc.expected)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue