Skip to content

Commit f60369d

Browse files
author
Ryan Zhang
committed
Export cli/command/config
Signed-off-by: Ryan Zhang <ryan.zhang@docker.com>
1 parent 79e1cab commit f60369d

4 files changed

Lines changed: 63 additions & 55 deletions

File tree

cli/command/config/create.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,45 @@ import (
1515
"github.com/spf13/cobra"
1616
)
1717

18-
type createOptions struct {
19-
name string
20-
templateDriver string
21-
file string
22-
labels opts.ListOpts
18+
// CreateOptions specifies some options that are used when creating a config.
19+
type CreateOptions struct {
20+
Name string
21+
TemplateDriver string
22+
File string
23+
Labels opts.ListOpts
2324
}
2425

2526
func newConfigCreateCommand(dockerCli command.Cli) *cobra.Command {
26-
createOpts := createOptions{
27-
labels: opts.NewListOpts(opts.ValidateEnv),
27+
createOpts := CreateOptions{
28+
Labels: opts.NewListOpts(opts.ValidateEnv),
2829
}
2930

3031
cmd := &cobra.Command{
3132
Use: "create [OPTIONS] CONFIG file|-",
3233
Short: "Create a config from a file or STDIN",
3334
Args: cli.ExactArgs(2),
3435
RunE: func(cmd *cobra.Command, args []string) error {
35-
createOpts.name = args[0]
36-
createOpts.file = args[1]
37-
return runConfigCreate(dockerCli, createOpts)
36+
createOpts.Name = args[0]
37+
createOpts.File = args[1]
38+
return RunConfigCreate(dockerCli, createOpts)
3839
},
3940
}
4041
flags := cmd.Flags()
41-
flags.VarP(&createOpts.labels, "label", "l", "Config labels")
42-
flags.StringVar(&createOpts.templateDriver, "template-driver", "", "Template driver")
42+
flags.VarP(&createOpts.Labels, "label", "l", "Config labels")
43+
flags.StringVar(&createOpts.TemplateDriver, "template-driver", "", "Template driver")
4344
flags.SetAnnotation("driver", "version", []string{"1.37"})
4445

4546
return cmd
4647
}
4748

48-
func runConfigCreate(dockerCli command.Cli, options createOptions) error {
49+
// RunConfigCreate creates a config with the given options.
50+
func RunConfigCreate(dockerCli command.Cli, options CreateOptions) error {
4951
client := dockerCli.Client()
5052
ctx := context.Background()
5153

5254
var in io.Reader = dockerCli.In()
53-
if options.file != "-" {
54-
file, err := system.OpenSequential(options.file)
55+
if options.File != "-" {
56+
file, err := system.OpenSequential(options.File)
5557
if err != nil {
5658
return err
5759
}
@@ -61,19 +63,19 @@ func runConfigCreate(dockerCli command.Cli, options createOptions) error {
6163

6264
configData, err := ioutil.ReadAll(in)
6365
if err != nil {
64-
return errors.Errorf("Error reading content from %q: %v", options.file, err)
66+
return errors.Errorf("Error reading content from %q: %v", options.File, err)
6567
}
6668

6769
spec := swarm.ConfigSpec{
6870
Annotations: swarm.Annotations{
69-
Name: options.name,
70-
Labels: opts.ConvertKVStringsToMap(options.labels.GetAll()),
71+
Name: options.Name,
72+
Labels: opts.ConvertKVStringsToMap(options.Labels.GetAll()),
7173
},
7274
Data: configData,
7375
}
74-
if options.templateDriver != "" {
76+
if options.TemplateDriver != "" {
7577
spec.Templating = &swarm.Driver{
76-
Name: options.templateDriver,
78+
Name: options.TemplateDriver,
7779
}
7880
}
7981
r, err := client.ConfigCreate(ctx, spec)

cli/command/config/inspect.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,43 @@ import (
1111
"github.com/spf13/cobra"
1212
)
1313

14-
type inspectOptions struct {
15-
names []string
16-
format string
17-
pretty bool
14+
// InspectOptions contains options for the docker config inspect command.
15+
type InspectOptions struct {
16+
Names []string
17+
Format string
18+
Pretty bool
1819
}
1920

2021
func newConfigInspectCommand(dockerCli command.Cli) *cobra.Command {
21-
opts := inspectOptions{}
22+
opts := InspectOptions{}
2223
cmd := &cobra.Command{
2324
Use: "inspect [OPTIONS] CONFIG [CONFIG...]",
2425
Short: "Display detailed information on one or more configs",
2526
Args: cli.RequiresMinArgs(1),
2627
RunE: func(cmd *cobra.Command, args []string) error {
27-
opts.names = args
28-
return runConfigInspect(dockerCli, opts)
28+
opts.Names = args
29+
return RunConfigInspect(dockerCli, opts)
2930
},
3031
}
3132

32-
cmd.Flags().StringVarP(&opts.format, "format", "f", "", "Format the output using the given Go template")
33-
cmd.Flags().BoolVar(&opts.pretty, "pretty", false, "Print the information in a human friendly format")
33+
cmd.Flags().StringVarP(&opts.Format, "format", "f", "", "Format the output using the given Go template")
34+
cmd.Flags().BoolVar(&opts.Pretty, "pretty", false, "Print the information in a human friendly format")
3435
return cmd
3536
}
3637

37-
func runConfigInspect(dockerCli command.Cli, opts inspectOptions) error {
38+
// RunConfigInspect inspects the given Swarm config.
39+
func RunConfigInspect(dockerCli command.Cli, opts InspectOptions) error {
3840
client := dockerCli.Client()
3941
ctx := context.Background()
4042

41-
if opts.pretty {
42-
opts.format = "pretty"
43+
if opts.Pretty {
44+
opts.Format = "pretty"
4345
}
4446

4547
getRef := func(id string) (interface{}, []byte, error) {
4648
return client.ConfigInspectWithRaw(ctx, id)
4749
}
48-
f := opts.format
50+
f := opts.Format
4951

5052
// check if the user is trying to apply a template to the pretty format, which
5153
// is not supported
@@ -58,7 +60,7 @@ func runConfigInspect(dockerCli command.Cli, opts inspectOptions) error {
5860
Format: NewFormat(f, false),
5961
}
6062

61-
if err := InspectFormatWrite(configCtx, opts.names, getRef); err != nil {
63+
if err := InspectFormatWrite(configCtx, opts.Names, getRef); err != nil {
6264
return cli.StatusError{StatusCode: 1, Status: err.Error()}
6365
}
6466
return nil

cli/command/config/ls.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,47 @@ import (
1313
"vbom.ml/util/sortorder"
1414
)
1515

16-
type listOptions struct {
17-
quiet bool
18-
format string
19-
filter opts.FilterOpt
16+
// ListOptions contains options for the docker config ls command.
17+
type ListOptions struct {
18+
Quiet bool
19+
Format string
20+
Filter opts.FilterOpt
2021
}
2122

2223
func newConfigListCommand(dockerCli command.Cli) *cobra.Command {
23-
listOpts := listOptions{filter: opts.NewFilterOpt()}
24+
listOpts := ListOptions{Filter: opts.NewFilterOpt()}
2425

2526
cmd := &cobra.Command{
2627
Use: "ls [OPTIONS]",
2728
Aliases: []string{"list"},
2829
Short: "List configs",
2930
Args: cli.NoArgs,
3031
RunE: func(cmd *cobra.Command, args []string) error {
31-
return runConfigList(dockerCli, listOpts)
32+
return RunConfigList(dockerCli, listOpts)
3233
},
3334
}
3435

3536
flags := cmd.Flags()
36-
flags.BoolVarP(&listOpts.quiet, "quiet", "q", false, "Only display IDs")
37-
flags.StringVarP(&listOpts.format, "format", "", "", "Pretty-print configs using a Go template")
38-
flags.VarP(&listOpts.filter, "filter", "f", "Filter output based on conditions provided")
37+
flags.BoolVarP(&listOpts.Quiet, "quiet", "q", false, "Only display IDs")
38+
flags.StringVarP(&listOpts.Format, "format", "", "", "Pretty-print configs using a Go template")
39+
flags.VarP(&listOpts.Filter, "filter", "f", "Filter output based on conditions provided")
3940

4041
return cmd
4142
}
4243

43-
func runConfigList(dockerCli command.Cli, options listOptions) error {
44+
// RunConfigList lists Swarm configs.
45+
func RunConfigList(dockerCli command.Cli, options ListOptions) error {
4446
client := dockerCli.Client()
4547
ctx := context.Background()
4648

47-
configs, err := client.ConfigList(ctx, types.ConfigListOptions{Filters: options.filter.Value()})
49+
configs, err := client.ConfigList(ctx, types.ConfigListOptions{Filters: options.Filter.Value()})
4850
if err != nil {
4951
return err
5052
}
5153

52-
format := options.format
54+
format := options.Format
5355
if len(format) == 0 {
54-
if len(dockerCli.ConfigFile().ConfigFormat) > 0 && !options.quiet {
56+
if len(dockerCli.ConfigFile().ConfigFormat) > 0 && !options.Quiet {
5557
format = dockerCli.ConfigFile().ConfigFormat
5658
} else {
5759
format = formatter.TableFormatKey
@@ -64,7 +66,7 @@ func runConfigList(dockerCli command.Cli, options listOptions) error {
6466

6567
configCtx := formatter.Context{
6668
Output: dockerCli.Out(),
67-
Format: NewFormat(format, options.quiet),
69+
Format: NewFormat(format, options.Quiet),
6870
}
6971
return FormatWrite(configCtx, configs)
7072
}

cli/command/config/remove.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import (
1111
"github.com/spf13/cobra"
1212
)
1313

14-
type removeOptions struct {
15-
names []string
14+
// RemoveOptions contains options for the docker config rm command.
15+
type RemoveOptions struct {
16+
Names []string
1617
}
1718

1819
func newConfigRemoveCommand(dockerCli command.Cli) *cobra.Command {
@@ -22,21 +23,22 @@ func newConfigRemoveCommand(dockerCli command.Cli) *cobra.Command {
2223
Short: "Remove one or more configs",
2324
Args: cli.RequiresMinArgs(1),
2425
RunE: func(cmd *cobra.Command, args []string) error {
25-
opts := removeOptions{
26-
names: args,
26+
opts := RemoveOptions{
27+
Names: args,
2728
}
28-
return runConfigRemove(dockerCli, opts)
29+
return RunConfigRemove(dockerCli, opts)
2930
},
3031
}
3132
}
3233

33-
func runConfigRemove(dockerCli command.Cli, opts removeOptions) error {
34+
// RunConfigRemove removes the given Swarm configs.
35+
func RunConfigRemove(dockerCli command.Cli, opts RemoveOptions) error {
3436
client := dockerCli.Client()
3537
ctx := context.Background()
3638

3739
var errs []string
3840

39-
for _, name := range opts.names {
41+
for _, name := range opts.Names {
4042
if err := client.ConfigRemove(ctx, name); err != nil {
4143
errs = append(errs, err.Error())
4244
continue

0 commit comments

Comments
 (0)