2016-09-08 13:11:39 -04:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2017-07-10 05:43:57 -04:00
|
|
|
"sort"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-07-10 05:43:57 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
2017-07-10 05:43:57 -04:00
|
|
|
"vbom.ml/util/sortorder"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
2017-07-10 05:43:57 -04:00
|
|
|
type byHostname []swarm.Node
|
|
|
|
|
|
|
|
func (n byHostname) Len() int { return len(n) }
|
|
|
|
func (n byHostname) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
|
|
|
|
func (n byHostname) Less(i, j int) bool {
|
|
|
|
return sortorder.NaturalLess(n[i].Description.Hostname, n[j].Description.Hostname)
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
type listOptions struct {
|
|
|
|
quiet bool
|
2017-01-24 16:17:40 -05:00
|
|
|
format string
|
2016-09-08 13:11:39 -04:00
|
|
|
filter opts.FilterOpt
|
|
|
|
}
|
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
func newListCommand(dockerCli command.Cli) *cobra.Command {
|
2017-05-15 08:45:19 -04: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 {
|
2017-05-15 08:45:19 -04:00
|
|
|
return runList(dockerCli, options)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
flags := cmd.Flags()
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display IDs")
|
|
|
|
flags.StringVar(&options.format, "format", "", "Pretty-print nodes using a Go template")
|
|
|
|
flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
func runList(dockerCli command.Cli, options listOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
nodes, err := client.NodeList(
|
|
|
|
ctx,
|
2017-05-15 08:45:19 -04:00
|
|
|
types.NodeListOptions{Filters: options.filter.Value()})
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-24 16:17:40 -05:00
|
|
|
info := types.Info{}
|
2017-05-15 08:45:19 -04:00
|
|
|
if len(nodes) > 0 && !options.quiet {
|
2016-10-08 22:29:58 -04:00
|
|
|
// only non-empty nodes and not quiet, should we call /info api
|
2017-01-24 16:17:40 -05:00
|
|
|
info, err = client.Info(ctx)
|
2016-10-08 22:29:58 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-10-08 22:29:58 -04:00
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
format := options.format
|
2017-01-24 16:17:40 -05:00
|
|
|
if len(format) == 0 {
|
|
|
|
format = formatter.TableFormatKey
|
2017-05-15 08:45:19 -04:00
|
|
|
if len(dockerCli.ConfigFile().NodesFormat) > 0 && !options.quiet {
|
2017-01-24 16:17:40 -05:00
|
|
|
format = dockerCli.ConfigFile().NodesFormat
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 16:17:40 -05:00
|
|
|
nodesCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
2017-05-15 08:45:19 -04:00
|
|
|
Format: formatter.NewNodeFormat(format, options.quiet),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-07-10 05:43:57 -04:00
|
|
|
sort.Sort(byHostname(nodes))
|
2017-01-24 16:17:40 -05:00
|
|
|
return formatter.NodeWrite(nodesCtx, nodes, info)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|