2016-10-19 12:22:02 -04:00
|
|
|
package secret
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/cli/command"
|
2016-11-03 17:01:54 -04:00
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
runconfigopts "github.com/docker/docker/runconfig/opts"
|
2016-10-19 12:22:02 -04:00
|
|
|
"github.com/spf13/cobra"
|
2016-11-09 19:59:01 -05:00
|
|
|
"golang.org/x/net/context"
|
2016-10-19 12:22:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type createOptions struct {
|
2016-11-03 17:01:54 -04:00
|
|
|
name string
|
|
|
|
labels opts.ListOpts
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func newSecretCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-11-03 17:01:54 -04:00
|
|
|
createOpts := createOptions{
|
|
|
|
labels: opts.NewListOpts(runconfigopts.ValidateEnv),
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-11-18 01:28:21 -05:00
|
|
|
Use: "create [OPTIONS] SECRET [SECRET...]",
|
2016-10-19 12:22:02 -04:00
|
|
|
Short: "Create a secret using stdin as content",
|
2016-11-03 17:01:54 -04:00
|
|
|
Args: cli.RequiresMinArgs(1),
|
2016-10-19 12:22:02 -04:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2016-11-03 17:01:54 -04:00
|
|
|
createOpts.name = args[0]
|
|
|
|
return runSecretCreate(dockerCli, createOpts)
|
2016-10-19 12:22:02 -04:00
|
|
|
},
|
|
|
|
}
|
2016-11-03 17:01:54 -04:00
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.VarP(&createOpts.labels, "label", "l", "Secret labels")
|
|
|
|
|
|
|
|
return cmd
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
2016-11-03 17:01:54 -04:00
|
|
|
func runSecretCreate(dockerCli *command.DockerCli, options createOptions) error {
|
2016-10-19 12:22:02 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
secretData, err := ioutil.ReadAll(os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error reading content from STDIN: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
spec := swarm.SecretSpec{
|
|
|
|
Annotations: swarm.Annotations{
|
2016-11-03 17:01:54 -04:00
|
|
|
Name: options.name,
|
|
|
|
Labels: runconfigopts.ConvertKVStringsToMap(options.labels.GetAll()),
|
2016-10-19 12:22:02 -04:00
|
|
|
},
|
|
|
|
Data: secretData,
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := client.SecretCreate(ctx, spec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(dockerCli.Out(), r.ID)
|
|
|
|
return nil
|
|
|
|
}
|