github.com/cspotcode/docker-cli@v20.10.0-rc1.0.20201201121459-3faad7acc5b8+incompatible/cli/command/stack/list.go (about)

     1  package stack
     2  
     3  import (
     4  	"sort"
     5  
     6  	"github.com/docker/cli/cli"
     7  	"github.com/docker/cli/cli/command"
     8  	"github.com/docker/cli/cli/command/stack/formatter"
     9  	"github.com/docker/cli/cli/command/stack/kubernetes"
    10  	"github.com/docker/cli/cli/command/stack/options"
    11  	"github.com/docker/cli/cli/command/stack/swarm"
    12  	"github.com/fvbommel/sortorder"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  func newListCommand(dockerCli command.Cli, common *commonOptions) *cobra.Command {
    17  	opts := options.List{}
    18  
    19  	cmd := &cobra.Command{
    20  		Use:     "ls [OPTIONS]",
    21  		Aliases: []string{"list"},
    22  		Short:   "List stacks",
    23  		Args:    cli.NoArgs,
    24  		RunE: func(cmd *cobra.Command, args []string) error {
    25  			return RunList(cmd, dockerCli, opts, common.orchestrator)
    26  		},
    27  	}
    28  
    29  	flags := cmd.Flags()
    30  	flags.StringVar(&opts.Format, "format", "", "Pretty-print stacks using a Go template")
    31  	flags.StringSliceVar(&opts.Namespaces, "namespace", []string{}, "Kubernetes namespaces to use")
    32  	flags.SetAnnotation("namespace", "kubernetes", nil)
    33  	flags.BoolVarP(&opts.AllNamespaces, "all-namespaces", "", false, "List stacks from all Kubernetes namespaces")
    34  	flags.SetAnnotation("all-namespaces", "kubernetes", nil)
    35  	return cmd
    36  }
    37  
    38  // RunList performs a stack list against the specified orchestrator
    39  func RunList(cmd *cobra.Command, dockerCli command.Cli, opts options.List, orchestrator command.Orchestrator) error {
    40  	stacks := []*formatter.Stack{}
    41  	if orchestrator.HasSwarm() {
    42  		ss, err := swarm.GetStacks(dockerCli)
    43  		if err != nil {
    44  			return err
    45  		}
    46  		stacks = append(stacks, ss...)
    47  	}
    48  	if orchestrator.HasKubernetes() {
    49  		kubeCli, err := kubernetes.WrapCli(dockerCli, kubernetes.NewOptions(cmd.Flags(), orchestrator))
    50  		if err != nil {
    51  			return err
    52  		}
    53  		ss, err := kubernetes.GetStacks(kubeCli, opts)
    54  		if err != nil {
    55  			return err
    56  		}
    57  		stacks = append(stacks, ss...)
    58  	}
    59  	return format(dockerCli, opts, orchestrator, stacks)
    60  }
    61  
    62  func format(dockerCli command.Cli, opts options.List, orchestrator command.Orchestrator, stacks []*formatter.Stack) error {
    63  	format := formatter.Format(opts.Format)
    64  	if format == "" || format == formatter.TableFormatKey {
    65  		format = formatter.SwarmStackTableFormat
    66  		if orchestrator.HasKubernetes() {
    67  			format = formatter.KubernetesStackTableFormat
    68  		}
    69  	}
    70  	stackCtx := formatter.Context{
    71  		Output: dockerCli.Out(),
    72  		Format: format,
    73  	}
    74  	sort.Slice(stacks, func(i, j int) bool {
    75  		return sortorder.NaturalLess(stacks[i].Name, stacks[j].Name) ||
    76  			!sortorder.NaturalLess(stacks[j].Name, stacks[i].Name) &&
    77  				sortorder.NaturalLess(stacks[j].Namespace, stacks[i].Namespace)
    78  	})
    79  	return formatter.StackWrite(stackCtx, stacks)
    80  }