2016-10-19 12:22:02 -04:00
|
|
|
package secret
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/cli/command"
|
2017-03-05 06:11:04 -05:00
|
|
|
"github.com/docker/docker/cli/command/formatter"
|
2016-12-05 11:08:43 -05:00
|
|
|
"github.com/docker/docker/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 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 {
|
2016-12-05 11:08:43 -05:00
|
|
|
opts := 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 {
|
|
|
|
return runSecretList(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display IDs")
|
2017-03-05 06:11:04 -05:00
|
|
|
flags.StringVarP(&opts.format, "format", "", "", "Pretty-print secrets using a Go template")
|
2016-12-05 11:08:43 -05:00
|
|
|
flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
|
2016-10-19 12:22:02 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-04-01 03:07:22 -04:00
|
|
|
func runSecretList(dockerCli command.Cli, opts listOptions) error {
|
2016-10-19 12:22:02 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2016-12-05 11:08:43 -05:00
|
|
|
secrets, err := client.SecretList(ctx, types.SecretListOptions{Filters: opts.filter.Value()})
|
2016-10-19 12:22:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-05 06:11:04 -05:00
|
|
|
format := opts.format
|
|
|
|
if len(format) == 0 {
|
|
|
|
if len(dockerCli.ConfigFile().SecretFormat) > 0 && !opts.quiet {
|
|
|
|
format = dockerCli.ConfigFile().SecretFormat
|
|
|
|
} else {
|
|
|
|
format = formatter.TableFormatKey
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
}
|
2017-03-05 06:11:04 -05:00
|
|
|
secretCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
|
|
|
Format: formatter.NewSecretFormat(format, opts.quiet),
|
|
|
|
}
|
|
|
|
return formatter.SecretWrite(secretCtx, secrets)
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|