-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathlist.go
More file actions
59 lines (51 loc) · 1.43 KB
/
list.go
File metadata and controls
59 lines (51 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package stack
import (
"context"
"sort"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/formatter"
flagsHelper "github.com/docker/cli/cli/flags"
"github.com/fvbommel/sortorder"
"github.com/spf13/cobra"
)
// listOptions holds docker stack ls options
type listOptions struct {
format string
}
func newListCommand(dockerCLI command.Cli) *cobra.Command {
opts := listOptions{}
cmd := &cobra.Command{
Use: "ls [OPTIONS]",
Aliases: []string{"list"},
Short: "List stacks",
Args: cli.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runList(cmd.Context(), dockerCLI, opts)
},
ValidArgsFunction: cobra.NoFileCompletions,
DisableFlagsInUseLine: true,
}
flags := cmd.Flags()
flags.StringVar(&opts.format, "format", "", flagsHelper.FormatHelp)
return cmd
}
// runList performs a stack list against the specified swarm cluster
func runList(ctx context.Context, dockerCLI command.Cli, opts listOptions) error {
stacks, err := getStacks(ctx, dockerCLI.Client())
if err != nil {
return err
}
format := formatter.Format(opts.format)
if format == "" || format == formatter.TableFormatKey {
format = stackTableFormat
}
stackCtx := formatter.Context{
Output: dockerCLI.Out(),
Format: format,
}
sort.Slice(stacks, func(i, j int) bool {
return sortorder.NaturalLess(stacks[i].Name, stacks[j].Name)
})
return stackWrite(stackCtx, stacks)
}