Explorar el Código

Refactor global CLI options

Signed-off-by: Christopher Crone <[email protected]>
Christopher Crone hace 5 años
padre
commit
11b4bd19f5
Se han modificado 3 ficheros con 53 adiciones y 19 borrados
  1. 5 9
      cli/cmd/context.go
  2. 8 10
      cli/main.go
  3. 40 0
      cli/options/options.go

+ 5 - 9
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])
 		},
 	}
 }

+ 8 - 10
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"))

+ 40 - 0
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
+}