2016-06-23 01:00:21 -04:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
2017-02-16 08:30:39 -05:00
|
|
|
"sort"
|
2016-06-23 01:00:21 -04:00
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2017-03-05 13:02:03 -05:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/compose/convert"
|
2016-06-23 01:00:21 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-05-08 13:51:30 -04:00
|
|
|
"github.com/docker/docker/client"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-06-23 01:00:21 -04:00
|
|
|
"github.com/spf13/cobra"
|
2017-02-16 08:30:39 -05:00
|
|
|
"golang.org/x/net/context"
|
2017-07-10 05:43:57 -04:00
|
|
|
"vbom.ml/util/sortorder"
|
2016-06-23 01:00:21 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type listOptions struct {
|
2017-03-05 13:02:03 -05:00
|
|
|
format string
|
2016-06-23 01:00:21 -04:00
|
|
|
}
|
|
|
|
|
2017-06-20 14:00:01 -04:00
|
|
|
func newListCommand(dockerCli command.Cli) *cobra.Command {
|
2016-06-23 01:00:21 -04:00
|
|
|
opts := listOptions{}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "ls",
|
|
|
|
Aliases: []string{"list"},
|
|
|
|
Short: "List stacks",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runList(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-03-05 13:02:03 -05:00
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.StringVar(&opts.format, "format", "", "Pretty-print stacks using a Go template")
|
2016-06-23 01:00:21 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-06-20 14:00:01 -04:00
|
|
|
func runList(dockerCli command.Cli, opts listOptions) error {
|
2016-06-23 01:00:21 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
stacks, err := getStacks(ctx, client)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-05 13:02:03 -05:00
|
|
|
format := opts.format
|
|
|
|
if len(format) == 0 {
|
|
|
|
format = formatter.TableFormatKey
|
|
|
|
}
|
|
|
|
stackCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
|
|
|
Format: formatter.NewStackFormat(format),
|
|
|
|
}
|
|
|
|
sort.Sort(byName(stacks))
|
|
|
|
return formatter.StackWrite(stackCtx, stacks)
|
2016-06-23 01:00:21 -04:00
|
|
|
}
|
|
|
|
|
2017-03-05 13:02:03 -05:00
|
|
|
type byName []*formatter.Stack
|
2017-02-16 08:30:39 -05:00
|
|
|
|
|
|
|
func (n byName) Len() int { return len(n) }
|
|
|
|
func (n byName) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
|
2017-07-10 05:43:57 -04:00
|
|
|
func (n byName) Less(i, j int) bool { return sortorder.NaturalLess(n[i].Name, n[j].Name) }
|
2017-02-16 08:30:39 -05:00
|
|
|
|
2017-03-05 13:02:03 -05:00
|
|
|
func getStacks(ctx context.Context, apiclient client.APIClient) ([]*formatter.Stack, error) {
|
2016-06-23 01:00:21 -04:00
|
|
|
services, err := apiclient.ServiceList(
|
|
|
|
ctx,
|
2016-11-30 17:38:40 -05:00
|
|
|
types.ServiceListOptions{Filters: getAllStacksFilter()})
|
2016-06-23 01:00:21 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-09 17:42:16 -04:00
|
|
|
m := make(map[string]*formatter.Stack)
|
2016-06-23 01:00:21 -04:00
|
|
|
for _, service := range services {
|
|
|
|
labels := service.Spec.Labels
|
2016-12-05 16:14:08 -05:00
|
|
|
name, ok := labels[convert.LabelNamespace]
|
2016-06-23 01:00:21 -04:00
|
|
|
if !ok {
|
2017-03-09 13:23:45 -05:00
|
|
|
return nil, errors.Errorf("cannot get label %s for service %s",
|
2016-12-05 16:14:08 -05:00
|
|
|
convert.LabelNamespace, service.ID)
|
2016-06-23 01:00:21 -04:00
|
|
|
}
|
|
|
|
ztack, ok := m[name]
|
|
|
|
if !ok {
|
2017-03-05 13:02:03 -05:00
|
|
|
m[name] = &formatter.Stack{
|
2016-06-23 01:00:21 -04:00
|
|
|
Name: name,
|
|
|
|
Services: 1,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ztack.Services++
|
|
|
|
}
|
|
|
|
}
|
2017-03-05 13:02:03 -05:00
|
|
|
var stacks []*formatter.Stack
|
2016-06-23 01:00:21 -04:00
|
|
|
for _, stack := range m {
|
|
|
|
stacks = append(stacks, stack)
|
|
|
|
}
|
|
|
|
return stacks, nil
|
|
|
|
}
|