Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions cli/command/config/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,45 @@ 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{
Use: "create [OPTIONS] CONFIG file|-",
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
}
Expand All @@ -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)
Expand Down
30 changes: 16 additions & 14 deletions cli/command/config/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
30 changes: 16 additions & 14 deletions cli/command/config/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,47 @@ 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]",
Aliases: []string{"list"},
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
Expand All @@ -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)
}
16 changes: 9 additions & 7 deletions cli/command/config/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down