Merge pull request #31800 from ripcurld0/checkpoint_ls

Use formatter in docker checkpoint ls
This commit is contained in:
Victor Vieux 2017-03-31 15:54:22 -07:00 committed by GitHub
commit 4666c3e589
3 changed files with 112 additions and 13 deletions

View File

@ -1,14 +1,12 @@
package checkpoint package checkpoint
import ( import (
"fmt"
"text/tabwriter"
"golang.org/x/net/context" "golang.org/x/net/context"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/cli" "github.com/docker/docker/cli"
"github.com/docker/docker/cli/command" "github.com/docker/docker/cli/command"
"github.com/docker/docker/cli/command/formatter"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -48,15 +46,9 @@ func runList(dockerCli *command.DockerCli, container string, opts listOptions) e
return err return err
} }
w := tabwriter.NewWriter(dockerCli.Out(), 20, 1, 3, ' ', 0) cpCtx := formatter.Context{
fmt.Fprintf(w, "CHECKPOINT NAME") Output: dockerCli.Out(),
fmt.Fprintf(w, "\n") Format: formatter.NewCheckpointFormat(formatter.TableFormatKey),
for _, checkpoint := range checkpoints {
fmt.Fprintf(w, "%s\t", checkpoint.Name)
fmt.Fprint(w, "\n")
} }
return formatter.CheckpointWrite(cpCtx, checkpoints)
w.Flush()
return nil
} }

View File

@ -0,0 +1,52 @@
package formatter
import "github.com/docker/docker/api/types"
const (
defaultCheckpointFormat = "table {{.Name}}"
checkpointNameHeader = "CHECKPOINT NAME"
)
// NewCheckpointFormat returns a format for use with a checkpoint Context
func NewCheckpointFormat(source string) Format {
switch source {
case TableFormatKey:
return defaultCheckpointFormat
}
return Format(source)
}
// CheckpointWrite writes formatted checkpoints using the Context
func CheckpointWrite(ctx Context, checkpoints []types.Checkpoint) error {
render := func(format func(subContext subContext) error) error {
for _, checkpoint := range checkpoints {
if err := format(&checkpointContext{c: checkpoint}); err != nil {
return err
}
}
return nil
}
return ctx.Write(newCheckpointContext(), render)
}
type checkpointContext struct {
HeaderContext
c types.Checkpoint
}
func newCheckpointContext() *checkpointContext {
cpCtx := checkpointContext{}
cpCtx.header = volumeHeaderContext{
"Name": checkpointNameHeader,
}
return &cpCtx
}
func (c *checkpointContext) MarshalJSON() ([]byte, error) {
return marshalJSON(c)
}
func (c *checkpointContext) Name() string {
return c.c.Name
}

View File

@ -0,0 +1,55 @@
package formatter
import (
"bytes"
"testing"
"github.com/docker/docker/api/types"
"github.com/stretchr/testify/assert"
)
func TestCheckpointContextFormatWrite(t *testing.T) {
cases := []struct {
context Context
expected string
}{
{
Context{Format: NewCheckpointFormat(defaultCheckpointFormat)},
`CHECKPOINT NAME
checkpoint-1
checkpoint-2
checkpoint-3
`,
},
{
Context{Format: NewCheckpointFormat("{{.Name}}")},
`checkpoint-1
checkpoint-2
checkpoint-3
`,
},
{
Context{Format: NewCheckpointFormat("{{.Name}}:")},
`checkpoint-1:
checkpoint-2:
checkpoint-3:
`,
},
}
checkpoints := []types.Checkpoint{
{"checkpoint-1"},
{"checkpoint-2"},
{"checkpoint-3"},
}
for _, testcase := range cases {
out := bytes.NewBufferString("")
testcase.context.Output = out
err := CheckpointWrite(testcase.context, checkpoints)
if err != nil {
assert.Error(t, err, testcase.expected)
} else {
assert.Equal(t, out.String(), testcase.expected)
}
}
}