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"
)
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)