2016-06-23 01:00:21 -04:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-02-16 08:30:39 -05:00
|
|
|
"sort"
|
2016-06-23 01:00:21 -04:00
|
|
|
"strconv"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/compose/convert"
|
|
|
|
"github.com/docker/cli/client"
|
2016-06-23 01:00:21 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
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"
|
2016-06-23 01:00:21 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
listItemFmt = "%s\t%s\n"
|
|
|
|
)
|
|
|
|
|
|
|
|
type listOptions struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
|
|
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)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runList(dockerCli *command.DockerCli, opts listOptions) error {
|
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
stacks, err := getStacks(ctx, client)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
out := dockerCli.Out()
|
|
|
|
printTable(out, stacks)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-16 08:30:39 -05:00
|
|
|
type byName []*stack
|
|
|
|
|
|
|
|
func (n byName) Len() int { return len(n) }
|
|
|
|
func (n byName) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
|
|
|
|
func (n byName) Less(i, j int) bool { return n[i].Name < n[j].Name }
|
|
|
|
|
2016-06-23 01:00:21 -04:00
|
|
|
func printTable(out io.Writer, stacks []*stack) {
|
|
|
|
writer := tabwriter.NewWriter(out, 0, 4, 2, ' ', 0)
|
|
|
|
|
|
|
|
// Ignore flushing errors
|
|
|
|
defer writer.Flush()
|
|
|
|
|
2017-02-16 08:30:39 -05:00
|
|
|
sort.Sort(byName(stacks))
|
|
|
|
|
2016-06-23 01:00:21 -04:00
|
|
|
fmt.Fprintf(writer, listItemFmt, "NAME", "SERVICES")
|
|
|
|
for _, stack := range stacks {
|
|
|
|
fmt.Fprintf(
|
|
|
|
writer,
|
|
|
|
listItemFmt,
|
|
|
|
stack.Name,
|
|
|
|
strconv.Itoa(stack.Services),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type stack struct {
|
|
|
|
// Name is the name of the stack
|
2016-11-17 01:17:40 -05:00
|
|
|
Name string
|
2016-06-23 01:00:21 -04:00
|
|
|
// Services is the number of the services
|
|
|
|
Services int
|
|
|
|
}
|
|
|
|
|
|
|
|
func getStacks(
|
|
|
|
ctx context.Context,
|
|
|
|
apiclient client.APIClient,
|
|
|
|
) ([]*stack, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
m := make(map[string]*stack, 0)
|
|
|
|
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 {
|
|
|
|
m[name] = &stack{
|
|
|
|
Name: name,
|
|
|
|
Services: 1,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ztack.Services++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var stacks []*stack
|
|
|
|
for _, stack := range m {
|
|
|
|
stacks = append(stacks, stack)
|
|
|
|
}
|
|
|
|
return stacks, nil
|
|
|
|
}
|