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 <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2016-06-22 18:36:51 -04:00
parent 08784d7e0e
commit 58a14cd18c
2 changed files with 77 additions and 22 deletions

View File

@ -3,10 +3,20 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/docker/docker/api/client" "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"
"github.com/docker/docker/cli/cobraadaptor" "github.com/docker/docker/cli/cobraadaptor"
cliflags "github.com/docker/docker/cli/flags" cliflags "github.com/docker/docker/cli/flags"
@ -18,12 +28,14 @@ import (
"github.com/spf13/pflag" "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{ cmd := &cobra.Command{
Use: "docker [OPTIONS] COMMAND [arg...]", Use: "docker [OPTIONS] COMMAND [arg...]",
Short: "A self-sufficient runtime for containers.", Short: "A self-sufficient runtime for containers.",
SilenceUsage: true, SilenceUsage: true,
SilenceErrors: true, SilenceErrors: true,
TraverseChildren: true,
Args: cli.NoArgs, Args: cli.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
if opts.Version { if opts.Version {
@ -38,13 +50,66 @@ func newDockerCommand(dockerCli *client.DockerCli, opts *cliflags.ClientOptions)
return dockerCli.Initialize(opts) return dockerCli.Initialize(opts)
}, },
} }
cobraadaptor.SetupRootCommand(cmd, dockerCli) cobraadaptor.SetupRootCommand(cmd)
flags := cmd.Flags() flags := cmd.Flags()
flags.BoolVarP(&opts.Version, "version", "v", false, "Print version information and quit") flags.BoolVarP(&opts.Version, "version", "v", false, "Print version information and quit")
flags.StringVar(&opts.ConfigDir, "config", cliconfig.ConfigDir(), "Location of client config files") flags.StringVar(&opts.ConfigDir, "config", cliconfig.ConfigDir(), "Location of client config files")
opts.Common.InstallFlags(flags) 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 return cmd
} }
@ -53,9 +118,8 @@ func main() {
stdin, stdout, stderr := term.StdStreams() stdin, stdout, stderr := term.StdStreams()
logrus.SetOutput(stderr) logrus.SetOutput(stderr)
opts := cliflags.NewClientOptions() dockerCli := client.NewDockerCli(stdin, stdout, stderr)
dockerCli := client.NewDockerCli(stdin, stdout, stderr, opts) cmd := newDockerCommand(dockerCli)
cmd := newDockerCommand(dockerCli, opts)
if err := cmd.Execute(); err != nil { if err := cmd.Execute(); err != nil {
if sterr, ok := err.(cli.StatusError); ok { if sterr, ok := err.(cli.StatusError); ok {
@ -86,17 +150,10 @@ func dockerPreRun(flags *pflag.FlagSet, opts *cliflags.ClientOptions) {
opts.Common.SetDefaultOptions(flags) opts.Common.SetDefaultOptions(flags)
cliflags.SetDaemonLogLevel(opts.Common.LogLevel) cliflags.SetDaemonLogLevel(opts.Common.LogLevel)
// TODO: remove this, set a default in New, and pass it in opts
if opts.ConfigDir != "" { if opts.ConfigDir != "" {
cliconfig.SetConfigDir(opts.ConfigDir) cliconfig.SetConfigDir(opts.ConfigDir)
} }
if opts.Common.TrustKey == "" {
opts.Common.TrustKey = filepath.Join(
cliconfig.ConfigDir(),
cliflags.DefaultTrustKeyFile)
}
if opts.Common.Debug { if opts.Common.Debug {
utils.EnableDebug() utils.EnableDebug()
} }

View File

@ -8,16 +8,14 @@ import (
"github.com/docker/docker/utils" "github.com/docker/docker/utils"
"github.com/docker/docker/api/client" "github.com/docker/docker/api/client"
cliflags "github.com/docker/docker/cli/flags"
) )
func TestClientDebugEnabled(t *testing.T) { func TestClientDebugEnabled(t *testing.T) {
defer utils.DisableDebug() defer utils.DisableDebug()
opts := cliflags.NewClientOptions() cmd := newDockerCommand(&client.DockerCli{})
cmd := newDockerCommand(&client.DockerCli{}, opts) cmd.Flags().Set("debug", "true")
opts.Common.Debug = true
if err := cmd.PersistentPreRunE(cmd, []string{}); err != nil { if err := cmd.PersistentPreRunE(cmd, []string{}); err != nil {
t.Fatalf("Unexpected error: %s", err.Error()) t.Fatalf("Unexpected error: %s", err.Error())
} }