From 33c9edaf6c5401fc1891713d1ad8d861e6cea51f Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Thu, 21 Apr 2016 17:51:28 -0400 Subject: [PATCH] Cleanup the structure of the cli package. Move all flags into cli/flags Move usage help into cli/usage.go Signed-off-by: Daniel Nephin --- api/client/cli.go | 4 ++-- cli/{ => flags}/client.go | 2 +- cli/flags/common.go | 21 +++++++++++++++++---- cli/{common.go => usage.go} | 19 ------------------- cmd/docker/client.go | 3 +-- cmd/dockerd/daemon.go | 5 ++--- cmd/dockerd/daemon_test.go | 20 ++++++++++---------- cmd/dockerd/daemon_unix_test.go | 12 ++++++------ 8 files changed, 39 insertions(+), 47 deletions(-) rename cli/{ => flags}/client.go (94%) rename cli/{common.go => usage.go} (86%) diff --git a/api/client/cli.go b/api/client/cli.go index 273174a250..ed0a75dc81 100644 --- a/api/client/cli.go +++ b/api/client/cli.go @@ -9,7 +9,7 @@ import ( "runtime" "github.com/docker/docker/api" - "github.com/docker/docker/cli" + cliflags "github.com/docker/docker/cli/flags" "github.com/docker/docker/cliconfig" "github.com/docker/docker/cliconfig/credentials" "github.com/docker/docker/dockerversion" @@ -112,7 +112,7 @@ func (cli *DockerCli) restoreTerminal(in io.Closer) error { // The key file, protocol (i.e. unix) and address are passed in as strings, along with the tls.Config. If the tls.Config // is set the client scheme will be set to https. // The client will be given a 32-second timeout (see https://github.com/docker/docker/pull/8035). -func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientFlags) *DockerCli { +func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cliflags.ClientFlags) *DockerCli { cli := &DockerCli{ in: in, out: out, diff --git a/cli/client.go b/cli/flags/client.go similarity index 94% rename from cli/client.go rename to cli/flags/client.go index 6a82eb52a5..cc7309db4b 100644 --- a/cli/client.go +++ b/cli/flags/client.go @@ -1,4 +1,4 @@ -package cli +package flags import flag "github.com/docker/docker/pkg/mflag" diff --git a/cli/flags/common.go b/cli/flags/common.go index d23696979b..4726b04f2a 100644 --- a/cli/flags/common.go +++ b/cli/flags/common.go @@ -6,7 +6,6 @@ import ( "path/filepath" "github.com/Sirupsen/logrus" - "github.com/docker/docker/cli" "github.com/docker/docker/cliconfig" "github.com/docker/docker/opts" flag "github.com/docker/docker/pkg/mflag" @@ -31,9 +30,23 @@ var ( dockerTLSVerify = os.Getenv("DOCKER_TLS_VERIFY") != "" ) +// CommonFlags are flags common to both the client and the daemon. +type CommonFlags struct { + FlagSet *flag.FlagSet + PostParse func() + + Debug bool + Hosts []string + LogLevel string + TLS bool + TLSVerify bool + TLSOptions *tlsconfig.Options + TrustKey string +} + // InitCommonFlags initializes flags common to both client and daemon -func InitCommonFlags() *cli.CommonFlags { - var commonFlags = &cli.CommonFlags{FlagSet: new(flag.FlagSet)} +func InitCommonFlags() *CommonFlags { + var commonFlags = &CommonFlags{FlagSet: new(flag.FlagSet)} if dockerCertPath == "" { dockerCertPath = cliconfig.ConfigDir() @@ -60,7 +73,7 @@ func InitCommonFlags() *cli.CommonFlags { return commonFlags } -func postParseCommon(commonFlags *cli.CommonFlags) { +func postParseCommon(commonFlags *CommonFlags) { cmd := commonFlags.FlagSet SetDaemonLogLevel(commonFlags.LogLevel) diff --git a/cli/common.go b/cli/usage.go similarity index 86% rename from cli/common.go rename to cli/usage.go index 7f6a24ba1f..4b0eb0e0c3 100644 --- a/cli/common.go +++ b/cli/usage.go @@ -1,24 +1,5 @@ package cli -import ( - flag "github.com/docker/docker/pkg/mflag" - "github.com/docker/go-connections/tlsconfig" -) - -// CommonFlags represents flags that are common to both the client and the daemon. -type CommonFlags struct { - FlagSet *flag.FlagSet - PostParse func() - - Debug bool - Hosts []string - LogLevel string - TLS bool - TLSVerify bool - TLSOptions *tlsconfig.Options - TrustKey string -} - // Command is the struct containing the command name and description type Command struct { Name string diff --git a/cmd/docker/client.go b/cmd/docker/client.go index e8c7f889f8..4d98a33cc4 100644 --- a/cmd/docker/client.go +++ b/cmd/docker/client.go @@ -3,7 +3,6 @@ package main import ( "path/filepath" - "github.com/docker/docker/cli" cliflags "github.com/docker/docker/cli/flags" "github.com/docker/docker/cliconfig" flag "github.com/docker/docker/pkg/mflag" @@ -12,7 +11,7 @@ import ( var ( commonFlags = cliflags.InitCommonFlags() - clientFlags = &cli.ClientFlags{FlagSet: new(flag.FlagSet), Common: commonFlags} + clientFlags = &cliflags.ClientFlags{FlagSet: new(flag.FlagSet), Common: commonFlags} ) func init() { diff --git a/cmd/dockerd/daemon.go b/cmd/dockerd/daemon.go index e6e5ccbd70..a020d9b844 100644 --- a/cmd/dockerd/daemon.go +++ b/cmd/dockerd/daemon.go @@ -23,7 +23,6 @@ import ( systemrouter "github.com/docker/docker/api/server/router/system" "github.com/docker/docker/api/server/router/volume" "github.com/docker/docker/builder/dockerfile" - "github.com/docker/docker/cli" cliflags "github.com/docker/docker/cli/flags" "github.com/docker/docker/cliconfig" "github.com/docker/docker/daemon" @@ -51,7 +50,7 @@ const ( // DaemonCli represents the daemon CLI. type DaemonCli struct { *daemon.Config - commonFlags *cli.CommonFlags + commonFlags *cliflags.CommonFlags configFile *string } @@ -345,7 +344,7 @@ func shutdownDaemon(d *daemon.Daemon, timeout time.Duration) { } } -func loadDaemonCliConfig(config *daemon.Config, flags *flag.FlagSet, commonConfig *cli.CommonFlags, configFile string) (*daemon.Config, error) { +func loadDaemonCliConfig(config *daemon.Config, flags *flag.FlagSet, commonConfig *cliflags.CommonFlags, configFile string) (*daemon.Config, error) { config.Debug = commonConfig.Debug config.Hosts = commonConfig.Hosts config.LogLevel = commonConfig.LogLevel diff --git a/cmd/dockerd/daemon_test.go b/cmd/dockerd/daemon_test.go index feaa9aae26..c16e11aec9 100644 --- a/cmd/dockerd/daemon_test.go +++ b/cmd/dockerd/daemon_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/Sirupsen/logrus" - "github.com/docker/docker/cli" + cliflags "github.com/docker/docker/cli/flags" "github.com/docker/docker/daemon" "github.com/docker/docker/opts" "github.com/docker/docker/pkg/mflag" @@ -16,7 +16,7 @@ import ( func TestLoadDaemonCliConfigWithoutOverriding(t *testing.T) { c := &daemon.Config{} - common := &cli.CommonFlags{ + common := &cliflags.CommonFlags{ Debug: true, } @@ -35,7 +35,7 @@ func TestLoadDaemonCliConfigWithoutOverriding(t *testing.T) { func TestLoadDaemonCliConfigWithTLS(t *testing.T) { c := &daemon.Config{} - common := &cli.CommonFlags{ + common := &cliflags.CommonFlags{ TLS: true, TLSOptions: &tlsconfig.Options{ CAFile: "/tmp/ca.pem", @@ -57,7 +57,7 @@ func TestLoadDaemonCliConfigWithTLS(t *testing.T) { func TestLoadDaemonCliConfigWithConflicts(t *testing.T) { c := &daemon.Config{} - common := &cli.CommonFlags{} + common := &cliflags.CommonFlags{} f, err := ioutil.TempFile("", "docker-config-") if err != nil { t.Fatal(err) @@ -93,7 +93,7 @@ func TestLoadDaemonCliConfigWithConflicts(t *testing.T) { func TestLoadDaemonCliConfigWithTLSVerify(t *testing.T) { c := &daemon.Config{} - common := &cli.CommonFlags{ + common := &cliflags.CommonFlags{ TLSOptions: &tlsconfig.Options{ CAFile: "/tmp/ca.pem", }, @@ -126,7 +126,7 @@ func TestLoadDaemonCliConfigWithTLSVerify(t *testing.T) { func TestLoadDaemonCliConfigWithExplicitTLSVerifyFalse(t *testing.T) { c := &daemon.Config{} - common := &cli.CommonFlags{ + common := &cliflags.CommonFlags{ TLSOptions: &tlsconfig.Options{ CAFile: "/tmp/ca.pem", }, @@ -159,7 +159,7 @@ func TestLoadDaemonCliConfigWithExplicitTLSVerifyFalse(t *testing.T) { func TestLoadDaemonCliConfigWithoutTLSVerify(t *testing.T) { c := &daemon.Config{} - common := &cli.CommonFlags{ + common := &cliflags.CommonFlags{ TLSOptions: &tlsconfig.Options{ CAFile: "/tmp/ca.pem", }, @@ -191,7 +191,7 @@ func TestLoadDaemonCliConfigWithoutTLSVerify(t *testing.T) { func TestLoadDaemonCliConfigWithLogLevel(t *testing.T) { c := &daemon.Config{} - common := &cli.CommonFlags{} + common := &cliflags.CommonFlags{} f, err := ioutil.TempFile("", "docker-config-") if err != nil { @@ -223,7 +223,7 @@ func TestLoadDaemonCliConfigWithLogLevel(t *testing.T) { func TestLoadDaemonConfigWithEmbeddedOptions(t *testing.T) { c := &daemon.Config{} - common := &cli.CommonFlags{} + common := &cliflags.CommonFlags{} flags := mflag.NewFlagSet("test", mflag.ContinueOnError) flags.String([]string{"-tlscacert"}, "", "") @@ -256,7 +256,7 @@ func TestLoadDaemonConfigWithEmbeddedOptions(t *testing.T) { func TestLoadDaemonConfigWithRegistryOptions(t *testing.T) { c := &daemon.Config{} - common := &cli.CommonFlags{} + common := &cliflags.CommonFlags{} flags := mflag.NewFlagSet("test", mflag.ContinueOnError) c.ServiceOptions.InstallCliFlags(flags, absentFromHelp) diff --git a/cmd/dockerd/daemon_unix_test.go b/cmd/dockerd/daemon_unix_test.go index f10f6a9227..a72468eddb 100644 --- a/cmd/dockerd/daemon_unix_test.go +++ b/cmd/dockerd/daemon_unix_test.go @@ -6,7 +6,7 @@ import ( "io/ioutil" "testing" - "github.com/docker/docker/cli" + cliflags "github.com/docker/docker/cli/flags" "github.com/docker/docker/daemon" "github.com/docker/docker/opts" "github.com/docker/docker/pkg/mflag" @@ -14,7 +14,7 @@ import ( func TestLoadDaemonCliConfigWithDaemonFlags(t *testing.T) { c := &daemon.Config{} - common := &cli.CommonFlags{ + common := &cliflags.CommonFlags{ Debug: true, LogLevel: "info", } @@ -61,7 +61,7 @@ func TestLoadDaemonCliConfigWithDaemonFlags(t *testing.T) { func TestLoadDaemonConfigWithNetwork(t *testing.T) { c := &daemon.Config{} - common := &cli.CommonFlags{} + common := &cliflags.CommonFlags{} flags := mflag.NewFlagSet("test", mflag.ContinueOnError) flags.String([]string{"-bip"}, "", "") flags.String([]string{"-ip"}, "", "") @@ -92,7 +92,7 @@ func TestLoadDaemonConfigWithNetwork(t *testing.T) { func TestLoadDaemonConfigWithMapOptions(t *testing.T) { c := &daemon.Config{} - common := &cli.CommonFlags{} + common := &cliflags.CommonFlags{} flags := mflag.NewFlagSet("test", mflag.ContinueOnError) flags.Var(opts.NewNamedMapOpts("cluster-store-opts", c.ClusterOpts, nil), []string{"-cluster-store-opt"}, "") @@ -136,7 +136,7 @@ func TestLoadDaemonConfigWithMapOptions(t *testing.T) { func TestLoadDaemonConfigWithTrueDefaultValues(t *testing.T) { c := &daemon.Config{} - common := &cli.CommonFlags{} + common := &cliflags.CommonFlags{} flags := mflag.NewFlagSet("test", mflag.ContinueOnError) flags.BoolVar(&c.EnableUserlandProxy, []string{"-userland-proxy"}, true, "") @@ -181,7 +181,7 @@ func TestLoadDaemonConfigWithTrueDefaultValues(t *testing.T) { func TestLoadDaemonConfigWithTrueDefaultValuesLeaveDefaults(t *testing.T) { c := &daemon.Config{} - common := &cli.CommonFlags{} + common := &cliflags.CommonFlags{} flags := mflag.NewFlagSet("test", mflag.ContinueOnError) flags.BoolVar(&c.EnableUserlandProxy, []string{"-userland-proxy"}, true, "")