2016-09-08 13:11:39 -04:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2018-05-03 18:02:44 -07:00
|
|
|
"context"
|
2017-07-10 12:43:57 +03:00
|
|
|
"sort"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-03-30 15:27:25 +02:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2021-03-10 00:45:56 +01:00
|
|
|
flagsHelper "github.com/docker/cli/cli/flags"
|
2017-05-15 14:45:19 +02:00
|
|
|
"github.com/docker/cli/opts"
|
2025-05-19 17:03:39 +02:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2023-07-14 23:42:40 +02:00
|
|
|
"github.com/docker/docker/api/types/system"
|
2020-08-28 14:35:09 +02:00
|
|
|
"github.com/fvbommel/sortorder"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
2025-02-20 12:44:43 +01:00
|
|
|
"github.com/spf13/pflag"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type listOptions struct {
|
|
|
|
quiet bool
|
2017-01-24 13:17:40 -08:00
|
|
|
format string
|
2016-09-08 13:11:39 -04:00
|
|
|
filter opts.FilterOpt
|
|
|
|
}
|
|
|
|
|
2016-12-25 22:23:35 +01:00
|
|
|
func newListCommand(dockerCli command.Cli) *cobra.Command {
|
2017-05-15 14:45:19 +02:00
|
|
|
options := listOptions{filter: opts.NewFilterOpt()}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "ls [OPTIONS]",
|
|
|
|
Aliases: []string{"list"},
|
|
|
|
Short: "List nodes in the swarm",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2023-09-09 22:27:44 +00:00
|
|
|
return runList(cmd.Context(), dockerCli, options)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2022-03-30 15:27:25 +02:00
|
|
|
ValidArgsFunction: completion.NoComplete,
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
flags := cmd.Flags()
|
2017-05-15 14:45:19 +02:00
|
|
|
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display IDs")
|
2021-03-10 00:45:56 +01:00
|
|
|
flags.StringVar(&options.format, "format", "", flagsHelper.FormatHelp)
|
2017-05-15 14:45:19 +02:00
|
|
|
flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2025-02-20 12:44:43 +01:00
|
|
|
flags.VisitAll(func(flag *pflag.Flag) {
|
|
|
|
// Set a default completion function if none was set. We don't look
|
|
|
|
// up if it does already have one set, because Cobra does this for
|
|
|
|
// us, and returns an error (which we ignore for this reason).
|
|
|
|
_ = cmd.RegisterFlagCompletionFunc(flag.Name, completion.NoComplete)
|
|
|
|
})
|
2016-09-08 13:11:39 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-09-09 22:27:44 +00:00
|
|
|
func runList(ctx context.Context, dockerCli command.Cli, options listOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
|
|
|
|
nodes, err := client.NodeList(
|
|
|
|
ctx,
|
2025-05-19 17:03:39 +02:00
|
|
|
swarm.NodeListOptions{Filters: options.filter.Value()})
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-14 23:42:40 +02:00
|
|
|
info := system.Info{}
|
2017-05-15 14:45:19 +02:00
|
|
|
if len(nodes) > 0 && !options.quiet {
|
2016-10-09 10:29:58 +08:00
|
|
|
// only non-empty nodes and not quiet, should we call /info api
|
2017-01-24 13:17:40 -08:00
|
|
|
info, err = client.Info(ctx)
|
2016-10-09 10:29:58 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-10-09 10:29:58 +08:00
|
|
|
|
2017-05-15 14:45:19 +02:00
|
|
|
format := options.format
|
2017-01-24 13:17:40 -08:00
|
|
|
if len(format) == 0 {
|
|
|
|
format = formatter.TableFormatKey
|
2017-05-15 14:45:19 +02:00
|
|
|
if len(dockerCli.ConfigFile().NodesFormat) > 0 && !options.quiet {
|
2017-01-24 13:17:40 -08:00
|
|
|
format = dockerCli.ConfigFile().NodesFormat
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 13:17:40 -08:00
|
|
|
nodesCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
2018-10-23 17:05:44 +02:00
|
|
|
Format: NewFormat(format, options.quiet),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2018-07-06 15:49:10 -04:00
|
|
|
sort.Slice(nodes, func(i, j int) bool {
|
|
|
|
return sortorder.NaturalLess(nodes[i].Description.Hostname, nodes[j].Description.Hostname)
|
|
|
|
})
|
2018-10-23 17:05:44 +02:00
|
|
|
return FormatWrite(nodesCtx, nodes, info)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|