From 58a14cd18cb99ccf02a615e8f87aad99f64b8a42 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 22 Jun 2016 18:36:51 -0400 Subject: [PATCH] Update unit tests for new cobra root command. Cleanup cobra integration Update windows files for cobra and pflags Cleanup SetupRootcmd, and remove unnecessary SetFlagErrorFunc. Use cobra command traversal Signed-off-by: Daniel Nephin --- docker.go | 93 ++++++++++++++++++++++++++++++++++++++++---------- docker_test.go | 6 ++-- 2 files changed, 77 insertions(+), 22 deletions(-) diff --git a/docker.go b/docker.go index bd04a3a1bd..0ae3906abb 100644 --- a/docker.go +++ b/docker.go @@ -3,10 +3,20 @@ package main import ( "fmt" "os" - "path/filepath" "github.com/Sirupsen/logrus" "github.com/docker/docker/api/client" + "github.com/docker/docker/api/client/container" + "github.com/docker/docker/api/client/image" + "github.com/docker/docker/api/client/network" + "github.com/docker/docker/api/client/node" + "github.com/docker/docker/api/client/plugin" + "github.com/docker/docker/api/client/registry" + "github.com/docker/docker/api/client/service" + "github.com/docker/docker/api/client/stack" + "github.com/docker/docker/api/client/swarm" + "github.com/docker/docker/api/client/system" + "github.com/docker/docker/api/client/volume" "github.com/docker/docker/cli" "github.com/docker/docker/cli/cobraadaptor" cliflags "github.com/docker/docker/cli/flags" @@ -18,13 +28,15 @@ import ( "github.com/spf13/pflag" ) -func newDockerCommand(dockerCli *client.DockerCli, opts *cliflags.ClientOptions) *cobra.Command { +func newDockerCommand(dockerCli *client.DockerCli) *cobra.Command { + opts := cliflags.NewClientOptions() cmd := &cobra.Command{ - Use: "docker [OPTIONS] COMMAND [arg...]", - Short: "A self-sufficient runtime for containers.", - SilenceUsage: true, - SilenceErrors: true, - Args: cli.NoArgs, + Use: "docker [OPTIONS] COMMAND [arg...]", + Short: "A self-sufficient runtime for containers.", + SilenceUsage: true, + SilenceErrors: true, + TraverseChildren: true, + Args: cli.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { if opts.Version { showVersion() @@ -38,13 +50,66 @@ func newDockerCommand(dockerCli *client.DockerCli, opts *cliflags.ClientOptions) return dockerCli.Initialize(opts) }, } - cobraadaptor.SetupRootCommand(cmd, dockerCli) + cobraadaptor.SetupRootCommand(cmd) flags := cmd.Flags() flags.BoolVarP(&opts.Version, "version", "v", false, "Print version information and quit") flags.StringVar(&opts.ConfigDir, "config", cliconfig.ConfigDir(), "Location of client config files") opts.Common.InstallFlags(flags) + cmd.SetOutput(dockerCli.Out()) + cmd.AddCommand( + newDaemonCommand(), + node.NewNodeCommand(dockerCli), + service.NewServiceCommand(dockerCli), + stack.NewStackCommand(dockerCli), + stack.NewTopLevelDeployCommand(dockerCli), + swarm.NewSwarmCommand(dockerCli), + container.NewAttachCommand(dockerCli), + container.NewCommitCommand(dockerCli), + container.NewCopyCommand(dockerCli), + container.NewCreateCommand(dockerCli), + container.NewDiffCommand(dockerCli), + container.NewExecCommand(dockerCli), + container.NewExportCommand(dockerCli), + container.NewKillCommand(dockerCli), + container.NewLogsCommand(dockerCli), + container.NewPauseCommand(dockerCli), + container.NewPortCommand(dockerCli), + container.NewPsCommand(dockerCli), + container.NewRenameCommand(dockerCli), + container.NewRestartCommand(dockerCli), + container.NewRmCommand(dockerCli), + container.NewRunCommand(dockerCli), + container.NewStartCommand(dockerCli), + container.NewStatsCommand(dockerCli), + container.NewStopCommand(dockerCli), + container.NewTopCommand(dockerCli), + container.NewUnpauseCommand(dockerCli), + container.NewUpdateCommand(dockerCli), + container.NewWaitCommand(dockerCli), + image.NewBuildCommand(dockerCli), + image.NewHistoryCommand(dockerCli), + image.NewImagesCommand(dockerCli), + image.NewLoadCommand(dockerCli), + image.NewRemoveCommand(dockerCli), + image.NewSaveCommand(dockerCli), + image.NewPullCommand(dockerCli), + image.NewPushCommand(dockerCli), + image.NewSearchCommand(dockerCli), + image.NewImportCommand(dockerCli), + image.NewTagCommand(dockerCli), + network.NewNetworkCommand(dockerCli), + system.NewEventsCommand(dockerCli), + system.NewInspectCommand(dockerCli), + registry.NewLoginCommand(dockerCli), + registry.NewLogoutCommand(dockerCli), + system.NewVersionCommand(dockerCli), + volume.NewVolumeCommand(dockerCli), + system.NewInfoCommand(dockerCli), + ) + plugin.NewPluginCommand(cmd, dockerCli) + return cmd } @@ -53,9 +118,8 @@ func main() { stdin, stdout, stderr := term.StdStreams() logrus.SetOutput(stderr) - opts := cliflags.NewClientOptions() - dockerCli := client.NewDockerCli(stdin, stdout, stderr, opts) - cmd := newDockerCommand(dockerCli, opts) + dockerCli := client.NewDockerCli(stdin, stdout, stderr) + cmd := newDockerCommand(dockerCli) if err := cmd.Execute(); err != nil { if sterr, ok := err.(cli.StatusError); ok { @@ -86,17 +150,10 @@ func dockerPreRun(flags *pflag.FlagSet, opts *cliflags.ClientOptions) { opts.Common.SetDefaultOptions(flags) cliflags.SetDaemonLogLevel(opts.Common.LogLevel) - // TODO: remove this, set a default in New, and pass it in opts if opts.ConfigDir != "" { cliconfig.SetConfigDir(opts.ConfigDir) } - if opts.Common.TrustKey == "" { - opts.Common.TrustKey = filepath.Join( - cliconfig.ConfigDir(), - cliflags.DefaultTrustKeyFile) - } - if opts.Common.Debug { utils.EnableDebug() } diff --git a/docker_test.go b/docker_test.go index a1e84f1396..72d2311521 100644 --- a/docker_test.go +++ b/docker_test.go @@ -8,16 +8,14 @@ import ( "github.com/docker/docker/utils" "github.com/docker/docker/api/client" - cliflags "github.com/docker/docker/cli/flags" ) func TestClientDebugEnabled(t *testing.T) { defer utils.DisableDebug() - opts := cliflags.NewClientOptions() - cmd := newDockerCommand(&client.DockerCli{}, opts) + cmd := newDockerCommand(&client.DockerCli{}) + cmd.Flags().Set("debug", "true") - opts.Common.Debug = true if err := cmd.PersistentPreRunE(cmd, []string{}); err != nil { t.Fatalf("Unexpected error: %s", err.Error()) }