2017-05-08 13:36:04 -04:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2017-07-07 04:22:52 -04:00
|
|
|
"sort"
|
|
|
|
|
2017-05-08 13:36:04 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-03-30 09:27:25 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2017-05-08 13:36:04 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2021-03-09 18:45:56 -05:00
|
|
|
flagsHelper "github.com/docker/cli/cli/flags"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2017-05-08 13:36:04 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2020-08-28 08:35:09 -04:00
|
|
|
"github.com/fvbommel/sortorder"
|
2017-05-08 13:36:04 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2019-03-05 17:26:42 -05:00
|
|
|
// ListOptions contains options for the docker config ls command.
|
|
|
|
type ListOptions struct {
|
|
|
|
Quiet bool
|
|
|
|
Format string
|
|
|
|
Filter opts.FilterOpt
|
2017-05-08 13:36:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func newConfigListCommand(dockerCli command.Cli) *cobra.Command {
|
2019-03-05 17:26:42 -05:00
|
|
|
listOpts := ListOptions{Filter: opts.NewFilterOpt()}
|
2017-05-08 13:36:04 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "ls [OPTIONS]",
|
|
|
|
Aliases: []string{"list"},
|
|
|
|
Short: "List configs",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2023-09-09 18:27:44 -04:00
|
|
|
return RunConfigList(cmd.Context(), dockerCli, listOpts)
|
2017-05-08 13:36:04 -04:00
|
|
|
},
|
2022-03-30 09:27:25 -04:00
|
|
|
ValidArgsFunction: completion.NoComplete,
|
2017-05-08 13:36:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2019-03-05 17:26:42 -05:00
|
|
|
flags.BoolVarP(&listOpts.Quiet, "quiet", "q", false, "Only display IDs")
|
2024-01-29 05:16:14 -05:00
|
|
|
flags.StringVar(&listOpts.Format, "format", "", flagsHelper.FormatHelp)
|
2019-03-05 17:26:42 -05:00
|
|
|
flags.VarP(&listOpts.Filter, "filter", "f", "Filter output based on conditions provided")
|
2017-05-08 13:36:04 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2019-03-05 17:26:42 -05:00
|
|
|
// RunConfigList lists Swarm configs.
|
2023-09-09 18:27:44 -04:00
|
|
|
func RunConfigList(ctx context.Context, dockerCli command.Cli, options ListOptions) error {
|
2017-05-08 13:36:04 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
|
2019-03-05 17:26:42 -05:00
|
|
|
configs, err := client.ConfigList(ctx, types.ConfigListOptions{Filters: options.Filter.Value()})
|
2017-05-08 13:36:04 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-05 17:26:42 -05:00
|
|
|
format := options.Format
|
2017-05-08 13:36:04 -04:00
|
|
|
if len(format) == 0 {
|
2019-03-05 17:26:42 -05:00
|
|
|
if len(dockerCli.ConfigFile().ConfigFormat) > 0 && !options.Quiet {
|
2017-05-08 13:36:04 -04:00
|
|
|
format = dockerCli.ConfigFile().ConfigFormat
|
|
|
|
} else {
|
|
|
|
format = formatter.TableFormatKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 15:49:10 -04:00
|
|
|
sort.Slice(configs, func(i, j int) bool {
|
|
|
|
return sortorder.NaturalLess(configs[i].Spec.Name, configs[j].Spec.Name)
|
|
|
|
})
|
2017-07-07 04:22:52 -04:00
|
|
|
|
2017-05-08 13:36:04 -04:00
|
|
|
configCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
2019-03-05 17:26:42 -05:00
|
|
|
Format: NewFormat(format, options.Quiet),
|
2017-05-08 13:36:04 -04:00
|
|
|
}
|
2018-10-23 11:05:44 -04:00
|
|
|
return FormatWrite(configCtx, configs)
|
2017-05-08 13:36:04 -04:00
|
|
|
}
|