From 315e242b9c34b8e6bd258d5b5a76952dc16a6d84 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Thu, 21 Apr 2016 17:51:28 -0400 Subject: [PATCH 1/2] 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 --- client.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/client.go b/client.go index e8c7f889f8..4d98a33cc4 100644 --- a/client.go +++ b/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() { From ec6cc96fa177a91b0516c11ab3c1dfc5c42ac8db Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Mon, 25 Apr 2016 12:05:42 -0400 Subject: [PATCH 2/2] Consolidate the files in client/ Signed-off-by: Daniel Nephin --- client.go | 37 -------------------------------- docker.go | 34 +++++++++++++++++++++++++++++ client_test.go => docker_test.go | 0 flags.go => usage.go | 16 ++++---------- flags_test.go => usage_test.go | 4 +++- 5 files changed, 41 insertions(+), 50 deletions(-) delete mode 100644 client.go rename client_test.go => docker_test.go (100%) rename flags.go => usage.go (51%) rename flags_test.go => usage_test.go (70%) diff --git a/client.go b/client.go deleted file mode 100644 index 4d98a33cc4..0000000000 --- a/client.go +++ /dev/null @@ -1,37 +0,0 @@ -package main - -import ( - "path/filepath" - - cliflags "github.com/docker/docker/cli/flags" - "github.com/docker/docker/cliconfig" - flag "github.com/docker/docker/pkg/mflag" - "github.com/docker/docker/utils" -) - -var ( - commonFlags = cliflags.InitCommonFlags() - clientFlags = &cliflags.ClientFlags{FlagSet: new(flag.FlagSet), Common: commonFlags} -) - -func init() { - - client := clientFlags.FlagSet - client.StringVar(&clientFlags.ConfigDir, []string{"-config"}, cliconfig.ConfigDir(), "Location of client config files") - - clientFlags.PostParse = func() { - clientFlags.Common.PostParse() - - if clientFlags.ConfigDir != "" { - cliconfig.SetConfigDir(clientFlags.ConfigDir) - } - - if clientFlags.Common.TrustKey == "" { - clientFlags.Common.TrustKey = filepath.Join(cliconfig.ConfigDir(), cliflags.DefaultTrustKeyFile) - } - - if clientFlags.Common.Debug { - utils.EnableDebug() - } - } -} diff --git a/docker.go b/docker.go index 838602164d..8397124932 100644 --- a/docker.go +++ b/docker.go @@ -3,16 +3,26 @@ package main import ( "fmt" "os" + "path/filepath" "github.com/Sirupsen/logrus" "github.com/docker/docker/api/client" "github.com/docker/docker/cli" + cliflags "github.com/docker/docker/cli/flags" + "github.com/docker/docker/cliconfig" "github.com/docker/docker/dockerversion" flag "github.com/docker/docker/pkg/mflag" "github.com/docker/docker/pkg/term" "github.com/docker/docker/utils" ) +var ( + commonFlags = cliflags.InitCommonFlags() + clientFlags = initClientFlags(commonFlags) + flHelp = flag.Bool([]string{"h", "-help"}, false, "Print usage") + flVersion = flag.Bool([]string{"v", "-version"}, false, "Print version information and quit") +) + func main() { // Set terminal emulation based on platform as required. stdin, stdout, stderr := term.StdStreams() @@ -30,6 +40,7 @@ func main() { help := "\nCommands:\n" + dockerCommands := sortCommands(cli.DockerCommandUsage) for _, cmd := range dockerCommands { help += fmt.Sprintf(" %-10.10s%s\n", cmd.Name, cmd.Description) } @@ -75,3 +86,26 @@ func showVersion() { fmt.Printf("Docker version %s, build %s\n", dockerversion.Version, dockerversion.GitCommit) } } + +func initClientFlags(commonFlags *cliflags.CommonFlags) *cliflags.ClientFlags { + clientFlags := &cliflags.ClientFlags{FlagSet: new(flag.FlagSet), Common: commonFlags} + client := clientFlags.FlagSet + client.StringVar(&clientFlags.ConfigDir, []string{"-config"}, cliconfig.ConfigDir(), "Location of client config files") + + clientFlags.PostParse = func() { + clientFlags.Common.PostParse() + + if clientFlags.ConfigDir != "" { + cliconfig.SetConfigDir(clientFlags.ConfigDir) + } + + if clientFlags.Common.TrustKey == "" { + clientFlags.Common.TrustKey = filepath.Join(cliconfig.ConfigDir(), cliflags.DefaultTrustKeyFile) + } + + if clientFlags.Common.Debug { + utils.EnableDebug() + } + } + return clientFlags +} diff --git a/client_test.go b/docker_test.go similarity index 100% rename from client_test.go rename to docker_test.go diff --git a/flags.go b/usage.go similarity index 51% rename from flags.go rename to usage.go index 35a8108880..792d178073 100644 --- a/flags.go +++ b/usage.go @@ -4,12 +4,6 @@ import ( "sort" "github.com/docker/docker/cli" - flag "github.com/docker/docker/pkg/mflag" -) - -var ( - flHelp = flag.Bool([]string{"h", "-help"}, false, "Print usage") - flVersion = flag.Bool([]string{"v", "-version"}, false, "Print version information and quit") ) type byName []cli.Command @@ -18,13 +12,11 @@ func (a byName) Len() int { return len(a) } func (a byName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a byName) Less(i, j int) bool { return a[i].Name < a[j].Name } -var dockerCommands []cli.Command - // TODO(tiborvass): do not show 'daemon' on client-only binaries -func init() { - for _, cmd := range cli.DockerCommands { - dockerCommands = append(dockerCommands, cmd) - } +func sortCommands(commands []cli.Command) []cli.Command { + dockerCommands := make([]cli.Command, len(commands)) + copy(dockerCommands, commands) sort.Sort(byName(dockerCommands)) + return dockerCommands } diff --git a/flags_test.go b/usage_test.go similarity index 70% rename from flags_test.go rename to usage_test.go index 28021ba4c9..0453265db8 100644 --- a/flags_test.go +++ b/usage_test.go @@ -3,11 +3,13 @@ package main import ( "sort" "testing" + + "github.com/docker/docker/cli" ) // Tests if the subcommands of docker are sorted func TestDockerSubcommandsAreSorted(t *testing.T) { - if !sort.IsSorted(byName(dockerCommands)) { + if !sort.IsSorted(byName(cli.DockerCommandUsage)) { t.Fatal("Docker subcommands are not in sorted order") } }