Export cli/command/config

Signed-off-by: Ryan Zhang <ryan.zhang@docker.com>
This commit is contained in:
Ryan Zhang 2019-03-05 14:26:42 -08:00
parent 79e1cabf17
commit f60369dfe6
4 changed files with 63 additions and 55 deletions

View File

@ -15,16 +15,17 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
type createOptions struct { // CreateOptions specifies some options that are used when creating a config.
name string type CreateOptions struct {
templateDriver string Name string
file string TemplateDriver string
labels opts.ListOpts File string
Labels opts.ListOpts
} }
func newConfigCreateCommand(dockerCli command.Cli) *cobra.Command { func newConfigCreateCommand(dockerCli command.Cli) *cobra.Command {
createOpts := createOptions{ createOpts := CreateOptions{
labels: opts.NewListOpts(opts.ValidateEnv), Labels: opts.NewListOpts(opts.ValidateEnv),
} }
cmd := &cobra.Command{ cmd := &cobra.Command{
@ -32,26 +33,27 @@ func newConfigCreateCommand(dockerCli command.Cli) *cobra.Command {
Short: "Create a config from a file or STDIN", Short: "Create a config from a file or STDIN",
Args: cli.ExactArgs(2), Args: cli.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
createOpts.name = args[0] createOpts.Name = args[0]
createOpts.file = args[1] createOpts.File = args[1]
return runConfigCreate(dockerCli, createOpts) return RunConfigCreate(dockerCli, createOpts)
}, },
} }
flags := cmd.Flags() flags := cmd.Flags()
flags.VarP(&createOpts.labels, "label", "l", "Config labels") flags.VarP(&createOpts.Labels, "label", "l", "Config labels")
flags.StringVar(&createOpts.templateDriver, "template-driver", "", "Template driver") flags.StringVar(&createOpts.TemplateDriver, "template-driver", "", "Template driver")
flags.SetAnnotation("driver", "version", []string{"1.37"}) flags.SetAnnotation("driver", "version", []string{"1.37"})
return cmd 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() client := dockerCli.Client()
ctx := context.Background() ctx := context.Background()
var in io.Reader = dockerCli.In() var in io.Reader = dockerCli.In()
if options.file != "-" { if options.File != "-" {
file, err := system.OpenSequential(options.file) file, err := system.OpenSequential(options.File)
if err != nil { if err != nil {
return err return err
} }
@ -61,19 +63,19 @@ func runConfigCreate(dockerCli command.Cli, options createOptions) error {
configData, err := ioutil.ReadAll(in) configData, err := ioutil.ReadAll(in)
if err != nil { 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{ spec := swarm.ConfigSpec{
Annotations: swarm.Annotations{ Annotations: swarm.Annotations{
Name: options.name, Name: options.Name,
Labels: opts.ConvertKVStringsToMap(options.labels.GetAll()), Labels: opts.ConvertKVStringsToMap(options.Labels.GetAll()),
}, },
Data: configData, Data: configData,
} }
if options.templateDriver != "" { if options.TemplateDriver != "" {
spec.Templating = &swarm.Driver{ spec.Templating = &swarm.Driver{
Name: options.templateDriver, Name: options.TemplateDriver,
} }
} }
r, err := client.ConfigCreate(ctx, spec) r, err := client.ConfigCreate(ctx, spec)

View File

@ -11,41 +11,43 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
type inspectOptions struct { // InspectOptions contains options for the docker config inspect command.
names []string type InspectOptions struct {
format string Names []string
pretty bool Format string
Pretty bool
} }
func newConfigInspectCommand(dockerCli command.Cli) *cobra.Command { func newConfigInspectCommand(dockerCli command.Cli) *cobra.Command {
opts := inspectOptions{} opts := InspectOptions{}
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "inspect [OPTIONS] CONFIG [CONFIG...]", Use: "inspect [OPTIONS] CONFIG [CONFIG...]",
Short: "Display detailed information on one or more configs", Short: "Display detailed information on one or more configs",
Args: cli.RequiresMinArgs(1), Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
opts.names = args opts.Names = args
return runConfigInspect(dockerCli, opts) return RunConfigInspect(dockerCli, opts)
}, },
} }
cmd.Flags().StringVarP(&opts.format, "format", "f", "", "Format the output using the given Go template") 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().BoolVar(&opts.Pretty, "pretty", false, "Print the information in a human friendly format")
return cmd 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() client := dockerCli.Client()
ctx := context.Background() ctx := context.Background()
if opts.pretty { if opts.Pretty {
opts.format = "pretty" opts.Format = "pretty"
} }
getRef := func(id string) (interface{}, []byte, error) { getRef := func(id string) (interface{}, []byte, error) {
return client.ConfigInspectWithRaw(ctx, id) 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 // check if the user is trying to apply a template to the pretty format, which
// is not supported // is not supported
@ -58,7 +60,7 @@ func runConfigInspect(dockerCli command.Cli, opts inspectOptions) error {
Format: NewFormat(f, false), 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 cli.StatusError{StatusCode: 1, Status: err.Error()}
} }
return nil return nil

View File

@ -13,14 +13,15 @@ import (
"vbom.ml/util/sortorder" "vbom.ml/util/sortorder"
) )
type listOptions struct { // ListOptions contains options for the docker config ls command.
quiet bool type ListOptions struct {
format string Quiet bool
filter opts.FilterOpt Format string
Filter opts.FilterOpt
} }
func newConfigListCommand(dockerCli command.Cli) *cobra.Command { func newConfigListCommand(dockerCli command.Cli) *cobra.Command {
listOpts := listOptions{filter: opts.NewFilterOpt()} listOpts := ListOptions{Filter: opts.NewFilterOpt()}
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "ls [OPTIONS]", Use: "ls [OPTIONS]",
@ -28,30 +29,31 @@ func newConfigListCommand(dockerCli command.Cli) *cobra.Command {
Short: "List configs", Short: "List configs",
Args: cli.NoArgs, Args: cli.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return runConfigList(dockerCli, listOpts) return RunConfigList(dockerCli, listOpts)
}, },
} }
flags := cmd.Flags() flags := cmd.Flags()
flags.BoolVarP(&listOpts.quiet, "quiet", "q", false, "Only display IDs") flags.BoolVarP(&listOpts.Quiet, "quiet", "q", false, "Only display IDs")
flags.StringVarP(&listOpts.format, "format", "", "", "Pretty-print configs using a Go template") 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.VarP(&listOpts.Filter, "filter", "f", "Filter output based on conditions provided")
return cmd 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() client := dockerCli.Client()
ctx := context.Background() 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 { if err != nil {
return err return err
} }
format := options.format format := options.Format
if len(format) == 0 { if len(format) == 0 {
if len(dockerCli.ConfigFile().ConfigFormat) > 0 && !options.quiet { if len(dockerCli.ConfigFile().ConfigFormat) > 0 && !options.Quiet {
format = dockerCli.ConfigFile().ConfigFormat format = dockerCli.ConfigFile().ConfigFormat
} else { } else {
format = formatter.TableFormatKey format = formatter.TableFormatKey
@ -64,7 +66,7 @@ func runConfigList(dockerCli command.Cli, options listOptions) error {
configCtx := formatter.Context{ configCtx := formatter.Context{
Output: dockerCli.Out(), Output: dockerCli.Out(),
Format: NewFormat(format, options.quiet), Format: NewFormat(format, options.Quiet),
} }
return FormatWrite(configCtx, configs) return FormatWrite(configCtx, configs)
} }

View File

@ -11,8 +11,9 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
type removeOptions struct { // RemoveOptions contains options for the docker config rm command.
names []string type RemoveOptions struct {
Names []string
} }
func newConfigRemoveCommand(dockerCli command.Cli) *cobra.Command { func newConfigRemoveCommand(dockerCli command.Cli) *cobra.Command {
@ -22,21 +23,22 @@ func newConfigRemoveCommand(dockerCli command.Cli) *cobra.Command {
Short: "Remove one or more configs", Short: "Remove one or more configs",
Args: cli.RequiresMinArgs(1), Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
opts := removeOptions{ opts := RemoveOptions{
names: args, 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() client := dockerCli.Client()
ctx := context.Background() ctx := context.Background()
var errs []string var errs []string
for _, name := range opts.names { for _, name := range opts.Names {
if err := client.ConfigRemove(ctx, name); err != nil { if err := client.ConfigRemove(ctx, name); err != nil {
errs = append(errs, err.Error()) errs = append(errs, err.Error())
continue continue