2017-12-04 06:30:39 -05:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
2018-04-25 08:24:46 -04:00
|
|
|
"sort"
|
|
|
|
|
2017-12-04 06:30:39 -05:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2018-10-23 11:05:44 -04:00
|
|
|
"github.com/docker/cli/cli/command/stack/formatter"
|
2017-12-04 06:30:39 -05:00
|
|
|
"github.com/docker/cli/cli/command/stack/kubernetes"
|
|
|
|
"github.com/docker/cli/cli/command/stack/options"
|
|
|
|
"github.com/docker/cli/cli/command/stack/swarm"
|
|
|
|
"github.com/spf13/cobra"
|
2018-04-25 08:24:46 -04:00
|
|
|
"vbom.ml/util/sortorder"
|
2017-12-04 06:30:39 -05:00
|
|
|
)
|
|
|
|
|
2018-06-20 08:48:50 -04:00
|
|
|
func newListCommand(dockerCli command.Cli, common *commonOptions) *cobra.Command {
|
2017-12-04 06:30:39 -05:00
|
|
|
opts := options.List{}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2018-06-22 02:16:27 -04:00
|
|
|
Use: "ls [OPTIONS]",
|
2017-12-04 06:30:39 -05:00
|
|
|
Aliases: []string{"list"},
|
|
|
|
Short: "List stacks",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2019-01-14 12:00:14 -05:00
|
|
|
return RunList(cmd, dockerCli, opts, common.orchestrator)
|
2017-12-04 06:30:39 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.StringVar(&opts.Format, "format", "", "Pretty-print stacks using a Go template")
|
2018-04-26 05:13:14 -04:00
|
|
|
flags.StringSliceVar(&opts.Namespaces, "namespace", []string{}, "Kubernetes namespaces to use")
|
|
|
|
flags.SetAnnotation("namespace", "kubernetes", nil)
|
|
|
|
flags.BoolVarP(&opts.AllNamespaces, "all-namespaces", "", false, "List stacks from all Kubernetes namespaces")
|
2018-04-09 09:13:16 -04:00
|
|
|
flags.SetAnnotation("all-namespaces", "kubernetes", nil)
|
2017-12-04 06:30:39 -05:00
|
|
|
return cmd
|
|
|
|
}
|
2018-04-25 08:24:46 -04:00
|
|
|
|
2019-01-14 12:00:14 -05:00
|
|
|
// RunList performs a stack list against the specified orchestrator
|
|
|
|
func RunList(cmd *cobra.Command, dockerCli command.Cli, opts options.List, orchestrator command.Orchestrator) error {
|
2018-04-25 08:24:46 -04:00
|
|
|
stacks := []*formatter.Stack{}
|
2018-06-20 08:48:50 -04:00
|
|
|
if orchestrator.HasSwarm() {
|
2018-04-25 08:24:46 -04:00
|
|
|
ss, err := swarm.GetStacks(dockerCli)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stacks = append(stacks, ss...)
|
|
|
|
}
|
2018-06-20 08:48:50 -04:00
|
|
|
if orchestrator.HasKubernetes() {
|
|
|
|
kubeCli, err := kubernetes.WrapCli(dockerCli, kubernetes.NewOptions(cmd.Flags(), orchestrator))
|
2018-05-17 12:03:41 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ss, err := kubernetes.GetStacks(kubeCli, opts)
|
2018-04-25 08:24:46 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stacks = append(stacks, ss...)
|
|
|
|
}
|
2018-06-20 08:48:50 -04:00
|
|
|
return format(dockerCli, opts, orchestrator, stacks)
|
2018-04-26 05:13:14 -04:00
|
|
|
}
|
|
|
|
|
2018-06-20 08:48:50 -04:00
|
|
|
func format(dockerCli command.Cli, opts options.List, orchestrator command.Orchestrator, stacks []*formatter.Stack) error {
|
2018-04-25 08:24:46 -04:00
|
|
|
format := opts.Format
|
|
|
|
if format == "" || format == formatter.TableFormatKey {
|
|
|
|
format = formatter.SwarmStackTableFormat
|
2018-06-20 08:48:50 -04:00
|
|
|
if orchestrator.HasKubernetes() {
|
2018-04-25 08:24:46 -04:00
|
|
|
format = formatter.KubernetesStackTableFormat
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stackCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
|
|
|
Format: formatter.Format(format),
|
|
|
|
}
|
|
|
|
sort.Slice(stacks, func(i, j int) bool {
|
2018-04-26 05:13:14 -04:00
|
|
|
return sortorder.NaturalLess(stacks[i].Name, stacks[j].Name) ||
|
|
|
|
!sortorder.NaturalLess(stacks[j].Name, stacks[i].Name) &&
|
|
|
|
sortorder.NaturalLess(stacks[j].Namespace, stacks[i].Namespace)
|
2018-04-25 08:24:46 -04:00
|
|
|
})
|
|
|
|
return formatter.StackWrite(stackCtx, stacks)
|
|
|
|
}
|