github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/command/secret/ls.go (about) 1 package secret 2 3 import ( 4 "context" 5 "sort" 6 7 "github.com/docker/cli/cli" 8 "github.com/docker/cli/cli/command" 9 "github.com/docker/cli/cli/command/formatter" 10 "github.com/docker/cli/opts" 11 "github.com/docker/docker/api/types" 12 "github.com/fvbommel/sortorder" 13 "github.com/spf13/cobra" 14 ) 15 16 type listOptions struct { 17 quiet bool 18 format string 19 filter opts.FilterOpt 20 } 21 22 func newSecretListCommand(dockerCli command.Cli) *cobra.Command { 23 options := listOptions{filter: opts.NewFilterOpt()} 24 25 cmd := &cobra.Command{ 26 Use: "ls [OPTIONS]", 27 Aliases: []string{"list"}, 28 Short: "List secrets", 29 Args: cli.NoArgs, 30 RunE: func(cmd *cobra.Command, args []string) error { 31 return runSecretList(dockerCli, options) 32 }, 33 } 34 35 flags := cmd.Flags() 36 flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display IDs") 37 flags.StringVarP(&options.format, "format", "", "", "Pretty-print secrets using a Go template") 38 flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided") 39 40 return cmd 41 } 42 43 func runSecretList(dockerCli command.Cli, options listOptions) error { 44 client := dockerCli.Client() 45 ctx := context.Background() 46 47 secrets, err := client.SecretList(ctx, types.SecretListOptions{Filters: options.filter.Value()}) 48 if err != nil { 49 return err 50 } 51 format := options.format 52 if len(format) == 0 { 53 if len(dockerCli.ConfigFile().SecretFormat) > 0 && !options.quiet { 54 format = dockerCli.ConfigFile().SecretFormat 55 } else { 56 format = formatter.TableFormatKey 57 } 58 } 59 60 sort.Slice(secrets, func(i, j int) bool { 61 return sortorder.NaturalLess(secrets[i].Spec.Name, secrets[j].Spec.Name) 62 }) 63 64 secretCtx := formatter.Context{ 65 Output: dockerCli.Out(), 66 Format: NewFormat(format, options.quiet), 67 } 68 return FormatWrite(secretCtx, secrets) 69 }