1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Use formatter in docker checkpoint ls

Signed-off-by: Boaz Shuster <ripcurld.github@gmail.com>
This commit is contained in:
Boaz Shuster 2017-03-13 23:19:46 +02:00
parent 47cd4bf526
commit f9d2636787
3 changed files with 112 additions and 13 deletions

View file

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

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)
}
}
}