2016-10-19 12:22:02 -04:00
|
|
|
package secret
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2017-07-07 04:22:52 -04:00
|
|
|
"sort"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2016-10-19 12:22:02 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-07-07 04:22:52 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-10-19 12:22:02 -04:00
|
|
|
"github.com/spf13/cobra"
|
2017-07-07 04:22:52 -04:00
|
|
|
"vbom.ml/util/sortorder"
|
2016-10-19 12:22:02 -04:00
|
|
|
)
|
|
|
|
|
2017-07-07 04:22:52 -04:00
|
|
|
type bySecretName []swarm.Secret
|
|
|
|
|
|
|
|
func (r bySecretName) Len() int { return len(r) }
|
|
|
|
func (r bySecretName) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
|
|
|
|
func (r bySecretName) Less(i, j int) bool {
|
|
|
|
return sortorder.NaturalLess(r[i].Spec.Name, r[j].Spec.Name)
|
|
|
|
}
|
|
|
|
|
2016-10-19 12:22:02 -04:00
|
|
|
type listOptions struct {
|
2017-03-05 06:11:04 -05:00
|
|
|
quiet bool
|
|
|
|
format string
|
2016-12-05 11:08:43 -05:00
|
|
|
filter opts.FilterOpt
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
2017-04-01 03:07:22 -04:00
|
|
|
func newSecretListCommand(dockerCli command.Cli) *cobra.Command {
|
2017-05-15 08:45:19 -04:00
|
|
|
options := listOptions{filter: opts.NewFilterOpt()}
|
2016-10-19 12:22:02 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-11-29 10:44:12 -05:00
|
|
|
Use: "ls [OPTIONS]",
|
|
|
|
Aliases: []string{"list"},
|
|
|
|
Short: "List secrets",
|
|
|
|
Args: cli.NoArgs,
|
2016-10-19 12:22:02 -04:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2017-05-15 08:45:19 -04:00
|
|
|
return runSecretList(dockerCli, options)
|
2016-10-19 12:22:02 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display IDs")
|
|
|
|
flags.StringVarP(&options.format, "format", "", "", "Pretty-print secrets using a Go template")
|
|
|
|
flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided")
|
2016-10-19 12:22:02 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
func runSecretList(dockerCli command.Cli, options listOptions) error {
|
2016-10-19 12:22:02 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
secrets, err := client.SecretList(ctx, types.SecretListOptions{Filters: options.filter.Value()})
|
2016-10-19 12:22:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-15 08:45:19 -04:00
|
|
|
format := options.format
|
2017-03-05 06:11:04 -05:00
|
|
|
if len(format) == 0 {
|
2017-05-15 08:45:19 -04:00
|
|
|
if len(dockerCli.ConfigFile().SecretFormat) > 0 && !options.quiet {
|
2017-03-05 06:11:04 -05:00
|
|
|
format = dockerCli.ConfigFile().SecretFormat
|
|
|
|
} else {
|
|
|
|
format = formatter.TableFormatKey
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
}
|
2017-07-07 04:22:52 -04:00
|
|
|
|
|
|
|
sort.Sort(bySecretName(secrets))
|
|
|
|
|
2017-03-05 06:11:04 -05:00
|
|
|
secretCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
2017-05-15 08:45:19 -04:00
|
|
|
Format: formatter.NewSecretFormat(format, options.quiet),
|
2017-03-05 06:11:04 -05:00
|
|
|
}
|
|
|
|
return formatter.SecretWrite(secretCtx, secrets)
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|