From f60369dfe6b5abba1f3b9751234d5233eb2dfbbb Mon Sep 17 00:00:00 2001 From: Ryan Zhang Date: Tue, 5 Mar 2019 14:26:42 -0800 Subject: [PATCH] Export cli/command/config Signed-off-by: Ryan Zhang --- cli/command/config/create.go | 42 ++++++++++++++++++----------------- cli/command/config/inspect.go | 30 +++++++++++++------------ cli/command/config/ls.go | 30 +++++++++++++------------ cli/command/config/remove.go | 16 +++++++------ 4 files changed, 63 insertions(+), 55 deletions(-) diff --git a/cli/command/config/create.go b/cli/command/config/create.go index 04130313e6..fc19370ccb 100644 --- a/cli/command/config/create.go +++ b/cli/command/config/create.go @@ -15,16 +15,17 @@ import ( "github.com/spf13/cobra" ) -type createOptions struct { - name string - templateDriver string - file string - labels opts.ListOpts +// CreateOptions specifies some options that are used when creating a config. +type CreateOptions struct { + Name string + TemplateDriver string + File string + Labels opts.ListOpts } func newConfigCreateCommand(dockerCli command.Cli) *cobra.Command { - createOpts := createOptions{ - labels: opts.NewListOpts(opts.ValidateEnv), + createOpts := CreateOptions{ + Labels: opts.NewListOpts(opts.ValidateEnv), } cmd := &cobra.Command{ @@ -32,26 +33,27 @@ func newConfigCreateCommand(dockerCli command.Cli) *cobra.Command { Short: "Create a config from a file or STDIN", Args: cli.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { - createOpts.name = args[0] - createOpts.file = args[1] - return runConfigCreate(dockerCli, createOpts) + createOpts.Name = args[0] + createOpts.File = args[1] + return RunConfigCreate(dockerCli, createOpts) }, } flags := cmd.Flags() - flags.VarP(&createOpts.labels, "label", "l", "Config labels") - flags.StringVar(&createOpts.templateDriver, "template-driver", "", "Template driver") + flags.VarP(&createOpts.Labels, "label", "l", "Config labels") + flags.StringVar(&createOpts.TemplateDriver, "template-driver", "", "Template driver") flags.SetAnnotation("driver", "version", []string{"1.37"}) return cmd } -func runConfigCreate(dockerCli command.Cli, options createOptions) error { +// RunConfigCreate creates a config with the given options. +func RunConfigCreate(dockerCli command.Cli, options CreateOptions) error { client := dockerCli.Client() ctx := context.Background() var in io.Reader = dockerCli.In() - if options.file != "-" { - file, err := system.OpenSequential(options.file) + if options.File != "-" { + file, err := system.OpenSequential(options.File) if err != nil { return err } @@ -61,19 +63,19 @@ func runConfigCreate(dockerCli command.Cli, options createOptions) error { configData, err := ioutil.ReadAll(in) if err != nil { - return errors.Errorf("Error reading content from %q: %v", options.file, err) + return errors.Errorf("Error reading content from %q: %v", options.File, err) } spec := swarm.ConfigSpec{ Annotations: swarm.Annotations{ - Name: options.name, - Labels: opts.ConvertKVStringsToMap(options.labels.GetAll()), + Name: options.Name, + Labels: opts.ConvertKVStringsToMap(options.Labels.GetAll()), }, Data: configData, } - if options.templateDriver != "" { + if options.TemplateDriver != "" { spec.Templating = &swarm.Driver{ - Name: options.templateDriver, + Name: options.TemplateDriver, } } r, err := client.ConfigCreate(ctx, spec) diff --git a/cli/command/config/inspect.go b/cli/command/config/inspect.go index ef11db713f..cb39ce2799 100644 --- a/cli/command/config/inspect.go +++ b/cli/command/config/inspect.go @@ -11,41 +11,43 @@ import ( "github.com/spf13/cobra" ) -type inspectOptions struct { - names []string - format string - pretty bool +// InspectOptions contains options for the docker config inspect command. +type InspectOptions struct { + Names []string + Format string + Pretty bool } func newConfigInspectCommand(dockerCli command.Cli) *cobra.Command { - opts := inspectOptions{} + opts := InspectOptions{} cmd := &cobra.Command{ Use: "inspect [OPTIONS] CONFIG [CONFIG...]", Short: "Display detailed information on one or more configs", Args: cli.RequiresMinArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - opts.names = args - return runConfigInspect(dockerCli, opts) + opts.Names = args + return RunConfigInspect(dockerCli, opts) }, } - cmd.Flags().StringVarP(&opts.format, "format", "f", "", "Format the output using the given Go template") - cmd.Flags().BoolVar(&opts.pretty, "pretty", false, "Print the information in a human friendly format") + cmd.Flags().StringVarP(&opts.Format, "format", "f", "", "Format the output using the given Go template") + cmd.Flags().BoolVar(&opts.Pretty, "pretty", false, "Print the information in a human friendly format") return cmd } -func runConfigInspect(dockerCli command.Cli, opts inspectOptions) error { +// RunConfigInspect inspects the given Swarm config. +func RunConfigInspect(dockerCli command.Cli, opts InspectOptions) error { client := dockerCli.Client() ctx := context.Background() - if opts.pretty { - opts.format = "pretty" + if opts.Pretty { + opts.Format = "pretty" } getRef := func(id string) (interface{}, []byte, error) { return client.ConfigInspectWithRaw(ctx, id) } - f := opts.format + f := opts.Format // check if the user is trying to apply a template to the pretty format, which // is not supported @@ -58,7 +60,7 @@ func runConfigInspect(dockerCli command.Cli, opts inspectOptions) error { Format: NewFormat(f, false), } - if err := InspectFormatWrite(configCtx, opts.names, getRef); err != nil { + if err := InspectFormatWrite(configCtx, opts.Names, getRef); err != nil { return cli.StatusError{StatusCode: 1, Status: err.Error()} } return nil diff --git a/cli/command/config/ls.go b/cli/command/config/ls.go index 849eb2bbc7..c004a718de 100644 --- a/cli/command/config/ls.go +++ b/cli/command/config/ls.go @@ -13,14 +13,15 @@ import ( "vbom.ml/util/sortorder" ) -type listOptions struct { - quiet bool - format string - filter opts.FilterOpt +// ListOptions contains options for the docker config ls command. +type ListOptions struct { + Quiet bool + Format string + Filter opts.FilterOpt } func newConfigListCommand(dockerCli command.Cli) *cobra.Command { - listOpts := listOptions{filter: opts.NewFilterOpt()} + listOpts := ListOptions{Filter: opts.NewFilterOpt()} cmd := &cobra.Command{ Use: "ls [OPTIONS]", @@ -28,30 +29,31 @@ func newConfigListCommand(dockerCli command.Cli) *cobra.Command { Short: "List configs", Args: cli.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return runConfigList(dockerCli, listOpts) + return RunConfigList(dockerCli, listOpts) }, } flags := cmd.Flags() - flags.BoolVarP(&listOpts.quiet, "quiet", "q", false, "Only display IDs") - flags.StringVarP(&listOpts.format, "format", "", "", "Pretty-print configs using a Go template") - flags.VarP(&listOpts.filter, "filter", "f", "Filter output based on conditions provided") + flags.BoolVarP(&listOpts.Quiet, "quiet", "q", false, "Only display IDs") + flags.StringVarP(&listOpts.Format, "format", "", "", "Pretty-print configs using a Go template") + flags.VarP(&listOpts.Filter, "filter", "f", "Filter output based on conditions provided") return cmd } -func runConfigList(dockerCli command.Cli, options listOptions) error { +// RunConfigList lists Swarm configs. +func RunConfigList(dockerCli command.Cli, options ListOptions) error { client := dockerCli.Client() ctx := context.Background() - configs, err := client.ConfigList(ctx, types.ConfigListOptions{Filters: options.filter.Value()}) + configs, err := client.ConfigList(ctx, types.ConfigListOptions{Filters: options.Filter.Value()}) if err != nil { return err } - format := options.format + format := options.Format if len(format) == 0 { - if len(dockerCli.ConfigFile().ConfigFormat) > 0 && !options.quiet { + if len(dockerCli.ConfigFile().ConfigFormat) > 0 && !options.Quiet { format = dockerCli.ConfigFile().ConfigFormat } else { format = formatter.TableFormatKey @@ -64,7 +66,7 @@ func runConfigList(dockerCli command.Cli, options listOptions) error { configCtx := formatter.Context{ Output: dockerCli.Out(), - Format: NewFormat(format, options.quiet), + Format: NewFormat(format, options.Quiet), } return FormatWrite(configCtx, configs) } diff --git a/cli/command/config/remove.go b/cli/command/config/remove.go index 3240a5a3de..7c30bd4636 100644 --- a/cli/command/config/remove.go +++ b/cli/command/config/remove.go @@ -11,8 +11,9 @@ import ( "github.com/spf13/cobra" ) -type removeOptions struct { - names []string +// RemoveOptions contains options for the docker config rm command. +type RemoveOptions struct { + Names []string } func newConfigRemoveCommand(dockerCli command.Cli) *cobra.Command { @@ -22,21 +23,22 @@ func newConfigRemoveCommand(dockerCli command.Cli) *cobra.Command { Short: "Remove one or more configs", Args: cli.RequiresMinArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - opts := removeOptions{ - names: args, + opts := RemoveOptions{ + Names: args, } - return runConfigRemove(dockerCli, opts) + return RunConfigRemove(dockerCli, opts) }, } } -func runConfigRemove(dockerCli command.Cli, opts removeOptions) error { +// RunConfigRemove removes the given Swarm configs. +func RunConfigRemove(dockerCli command.Cli, opts RemoveOptions) error { client := dockerCli.Client() ctx := context.Background() var errs []string - for _, name := range opts.names { + for _, name := range opts.Names { if err := client.ConfigRemove(ctx, name); err != nil { errs = append(errs, err.Error()) continue