github.1git.de/docker/cli@v26.1.3+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  	flagsHelper "github.com/docker/cli/cli/flags"
    11  	"github.com/docker/cli/opts"
    12  	"github.com/docker/docker/api/types"
    13  	"github.com/fvbommel/sortorder"
    14  	"github.com/spf13/cobra"
    15  )
    16  
    17  type listOptions struct {
    18  	quiet  bool
    19  	format string
    20  	filter opts.FilterOpt
    21  }
    22  
    23  func newSecretListCommand(dockerCli command.Cli) *cobra.Command {
    24  	options := listOptions{filter: opts.NewFilterOpt()}
    25  
    26  	cmd := &cobra.Command{
    27  		Use:     "ls [OPTIONS]",
    28  		Aliases: []string{"list"},
    29  		Short:   "List secrets",
    30  		Args:    cli.NoArgs,
    31  		RunE: func(cmd *cobra.Command, args []string) error {
    32  			return runSecretList(cmd.Context(), dockerCli, options)
    33  		},
    34  		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    35  			return completeNames(dockerCli)(cmd, args, toComplete)
    36  		},
    37  	}
    38  
    39  	flags := cmd.Flags()
    40  	flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display IDs")
    41  	flags.StringVar(&options.format, "format", "", flagsHelper.FormatHelp)
    42  	flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided")
    43  
    44  	return cmd
    45  }
    46  
    47  func runSecretList(ctx context.Context, dockerCli command.Cli, options listOptions) error {
    48  	client := dockerCli.Client()
    49  
    50  	secrets, err := client.SecretList(ctx, types.SecretListOptions{Filters: options.filter.Value()})
    51  	if err != nil {
    52  		return err
    53  	}
    54  	format := options.format
    55  	if len(format) == 0 {
    56  		if len(dockerCli.ConfigFile().SecretFormat) > 0 && !options.quiet {
    57  			format = dockerCli.ConfigFile().SecretFormat
    58  		} else {
    59  			format = formatter.TableFormatKey
    60  		}
    61  	}
    62  
    63  	sort.Slice(secrets, func(i, j int) bool {
    64  		return sortorder.NaturalLess(secrets[i].Spec.Name, secrets[j].Spec.Name)
    65  	})
    66  
    67  	secretCtx := formatter.Context{
    68  		Output: dockerCli.Out(),
    69  		Format: NewFormat(format, options.quiet),
    70  	}
    71  	return FormatWrite(secretCtx, secrets)
    72  }