Add format to docker stack ls

Signed-off-by: Boaz Shuster <ripcurld.github@gmail.com>
This commit is contained in:
Boaz Shuster 2017-03-05 20:02:03 +02:00 committed by Tibor Vass
parent e7793092a2
commit 5b6bd92862
3 changed files with 150 additions and 46 deletions

View File

@ -0,0 +1,67 @@
package formatter
import (
"strconv"
)
const (
defaultStackTableFormat = "table {{.Name}}\t{{.Services}}"
stackServicesHeader = "SERVICES"
)
// Stack contains deployed stack information.
type Stack struct {
// Name is the name of the stack
Name string
// Services is the number of the services
Services int
}
// NewStackFormat returns a format for use with a stack Context
func NewStackFormat(source string) Format {
switch source {
case TableFormatKey:
return defaultStackTableFormat
}
return Format(source)
}
// StackWrite writes formatted stacks using the Context
func StackWrite(ctx Context, stacks []*Stack) error {
render := func(format func(subContext subContext) error) error {
for _, stack := range stacks {
if err := format(&stackContext{s: stack}); err != nil {
return err
}
}
return nil
}
return ctx.Write(newStackContext(), render)
}
type stackContext struct {
HeaderContext
s *Stack
}
func newStackContext() *stackContext {
stackCtx := stackContext{}
stackCtx.header = map[string]string{
"Name": nameHeader,
"Services": stackServicesHeader,
}
return &stackCtx
}
func (s *stackContext) MarshalJSON() ([]byte, error) {
return marshalJSON(s)
}
func (s *stackContext) Name() string {
return s.s.Name
}
func (s *stackContext) Services() string {
return strconv.Itoa(s.s.Services)
}

View File

@ -0,0 +1,64 @@
package formatter
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
func TestStackContextWrite(t *testing.T) {
cases := []struct {
context Context
expected string
}{
// Errors
{
Context{Format: "{{InvalidFunction}}"},
`Template parsing error: template: :1: function "InvalidFunction" not defined
`,
},
{
Context{Format: "{{nil}}"},
`Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
`,
},
// Table format
{
Context{Format: NewStackFormat("table")},
`NAME SERVICES
baz 2
bar 1
`,
},
{
Context{Format: NewStackFormat("table {{.Name}}")},
`NAME
baz
bar
`,
},
// Custom Format
{
Context{Format: NewStackFormat("{{.Name}}")},
`baz
bar
`,
},
}
stacks := []*Stack{
{Name: "baz", Services: 2},
{Name: "bar", Services: 1},
}
for _, testcase := range cases {
out := bytes.NewBufferString("")
testcase.context.Output = out
err := StackWrite(testcase.context, stacks)
if err != nil {
assert.Error(t, err, testcase.expected)
} else {
assert.Equal(t, out.String(), testcase.expected)
}
}
}

View File

@ -1,14 +1,11 @@
package stack
import (
"fmt"
"io"
"sort"
"strconv"
"text/tabwriter"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/cli/compose/convert"
"github.com/docker/cli/client"
"github.com/docker/docker/api/types"
@ -17,11 +14,8 @@ import (
"golang.org/x/net/context"
)
const (
listItemFmt = "%s\t%s\n"
)
type listOptions struct {
format string
}
func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
@ -37,6 +31,8 @@ func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
},
}
flags := cmd.Flags()
flags.StringVar(&opts.format, "format", "", "Pretty-print stacks using a Go template")
return cmd
}
@ -48,55 +44,32 @@ func runList(dockerCli *command.DockerCli, opts listOptions) error {
if err != nil {
return err
}
out := dockerCli.Out()
printTable(out, stacks)
return nil
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)
}
type byName []*stack
type byName []*formatter.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 }
func printTable(out io.Writer, stacks []*stack) {
writer := tabwriter.NewWriter(out, 0, 4, 2, ' ', 0)
// Ignore flushing errors
defer writer.Flush()
sort.Sort(byName(stacks))
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
Name string
// Services is the number of the services
Services int
}
func getStacks(
ctx context.Context,
apiclient client.APIClient,
) ([]*stack, error) {
func getStacks(ctx context.Context, apiclient client.APIClient) ([]*formatter.Stack, error) {
services, err := apiclient.ServiceList(
ctx,
types.ServiceListOptions{Filters: getAllStacksFilter()})
if err != nil {
return nil, err
}
m := make(map[string]*stack, 0)
m := make(map[string]*formatter.Stack, 0)
for _, service := range services {
labels := service.Spec.Labels
name, ok := labels[convert.LabelNamespace]
@ -106,7 +79,7 @@ func getStacks(
}
ztack, ok := m[name]
if !ok {
m[name] = &stack{
m[name] = &formatter.Stack{
Name: name,
Services: 1,
}
@ -114,7 +87,7 @@ func getStacks(
ztack.Services++
}
}
var stacks []*stack
var stacks []*formatter.Stack
for _, stack := range m {
stacks = append(stacks, stack)
}