From 11b4bd19f571f16f786bfca2451a14a3afe452e8 Mon Sep 17 00:00:00 2001 From: Christopher Crone Date: Thu, 14 May 2020 18:29:09 +0200 Subject: [PATCH] Refactor global CLI options Signed-off-by: Christopher Crone --- cli/cmd/context.go | 14 +++++--------- cli/main.go | 18 ++++++++---------- cli/options/options.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 19 deletions(-) create mode 100644 cli/options/options.go diff --git a/cli/cmd/context.go b/cli/cmd/context.go index 651d2f4d1..bc044b92d 100644 --- a/cli/cmd/context.go +++ b/cli/cmd/context.go @@ -34,15 +34,15 @@ import ( "text/tabwriter" "github.com/hashicorp/go-multierror" - "github.com/pkg/errors" "github.com/spf13/cobra" cliconfig "github.com/docker/api/cli/config" + cliopts "github.com/docker/api/cli/options" "github.com/docker/api/context/store" ) // ContextCommand manages contexts -func ContextCommand() *cobra.Command { +func ContextCommand(opts *cliopts.GlobalOpts) *cobra.Command { cmd := &cobra.Command{ Use: "context", Short: "Manage contexts", @@ -52,7 +52,7 @@ func ContextCommand() *cobra.Command { createCommand(), listCommand(), removeCommand(), - useCommand(), + useCommand(opts), ) return cmd @@ -109,17 +109,13 @@ func removeCommand() *cobra.Command { } } -func useCommand() *cobra.Command { +func useCommand(opts *cliopts.GlobalOpts) *cobra.Command { return &cobra.Command{ Use: "use CONTEXT", Short: "Set the default context", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - config := cmd.Flag(cliconfig.ConfigFlagName) - if config == nil || config.Value.String() == "" { - panic(errors.New("no value for config directory")) - } - return runUse(cmd.Context(), config.Value.String(), args[0]) + return runUse(cmd.Context(), opts.Config, args[0]) }, } } diff --git a/cli/main.go b/cli/main.go index 66b96b92c..649a799e6 100644 --- a/cli/main.go +++ b/cli/main.go @@ -49,6 +49,7 @@ import ( "github.com/docker/api/cli/cmd/compose" "github.com/docker/api/cli/cmd/run" cliconfig "github.com/docker/api/cli/config" + cliopts "github.com/docker/api/cli/options" apicontext "github.com/docker/api/context" "github.com/docker/api/context/store" ) @@ -57,12 +58,6 @@ var ( runningOwnCommand bool ) -type mainOpts struct { - apicontext.ContextFlags - cliconfig.ConfigFlags - debug bool -} - func init() { // initial hack to get the path of the project's bin dir // into the env of this cli for development @@ -86,7 +81,7 @@ func isOwnCommand(cmd *cobra.Command) bool { } func main() { - var opts mainOpts + var opts cliopts.GlobalOpts root := &cobra.Command{ Use: "docker", Long: "docker for the 2020s", @@ -105,7 +100,7 @@ func main() { } root.AddCommand( - cmd.ContextCommand(), + cmd.ContextCommand(&opts), cmd.PsCommand(), cmd.ServeCommand(), run.Command(), @@ -124,19 +119,22 @@ func main() { helpFunc(cmd, args) }) - root.PersistentFlags().BoolVarP(&opts.debug, "debug", "d", false, "enable debug output in the logs") + root.PersistentFlags().BoolVarP(&opts.Debug, "debug", "d", false, "enable debug output in the logs") opts.AddConfigFlags(root.PersistentFlags()) opts.AddContextFlags(root.PersistentFlags()) // populate the opts with the global flags _ = root.PersistentFlags().Parse(os.Args[1:]) - if opts.debug { + if opts.Debug { logrus.SetLevel(logrus.DebugLevel) } ctx, cancel := newSigContext() defer cancel() + if opts.Config == "" { + fatal(errors.New("config path cannot be empty")) + } config, err := cliconfig.LoadFile(opts.Config) if err != nil { fatal(errors.Wrap(err, "unable to find configuration file")) diff --git a/cli/options/options.go b/cli/options/options.go new file mode 100644 index 000000000..ac97dbfab --- /dev/null +++ b/cli/options/options.go @@ -0,0 +1,40 @@ +/* + Copyright (c) 2020 Docker Inc. + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation + files (the "Software"), to deal in the Software without + restriction, including without limitation the rights to use, copy, + modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, + INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, + WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH + THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +package options + +import ( + apicontext "github.com/docker/api/context" + cliconfig "github.com/docker/api/cli/config" +) + +// GlobalOpts contains the global CLI options +type GlobalOpts struct { + apicontext.ContextFlags + cliconfig.ConfigFlags + Debug bool +}