formatter package heavy refactoring

- make it possible to extract the formatter implementation from the
  "common" code, that way, the formatter package stays small
- extract some formatter into their own packages

This is essentially moving the "formatter" implementation of each type
in their respective packages. The *main* reason to do that, is to be
able to depend on `cli/command/formatter` without depending of the
implementation detail of the formatter. As of now, depending on
`cli/command/formatter` means we depend on `docker/docker/api/types`,
`docker/licensing`, … — that should not be the case. `formatter`
should hold the common code (or helpers) to easily create formatter,
not all formatter implementations.

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
Vincent Demeester
2018-10-23 17:05:44 +02:00
parent ea836abed5
commit 69fdd2a4ad
75 changed files with 750 additions and 711 deletions

View File

@ -0,0 +1,120 @@
package network
import (
"fmt"
"strings"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/stringid"
)
const (
defaultNetworkTableFormat = "table {{.ID}}\t{{.Name}}\t{{.Driver}}\t{{.Scope}}"
networkIDHeader = "NETWORK ID"
ipv6Header = "IPV6"
internalHeader = "INTERNAL"
)
// NewFormat returns a Format for rendering using a network Context
func NewFormat(source string, quiet bool) formatter.Format {
switch source {
case formatter.TableFormatKey:
if quiet {
return formatter.DefaultQuietFormat
}
return defaultNetworkTableFormat
case formatter.RawFormatKey:
if quiet {
return `network_id: {{.ID}}`
}
return `network_id: {{.ID}}\nname: {{.Name}}\ndriver: {{.Driver}}\nscope: {{.Scope}}\n`
}
return formatter.Format(source)
}
// FormatWrite writes the context
func FormatWrite(ctx formatter.Context, networks []types.NetworkResource) error {
render := func(format func(subContext formatter.SubContext) error) error {
for _, network := range networks {
networkCtx := &networkContext{trunc: ctx.Trunc, n: network}
if err := format(networkCtx); err != nil {
return err
}
}
return nil
}
networkCtx := networkContext{}
networkCtx.Header = formatter.SubHeaderContext{
"ID": networkIDHeader,
"Name": formatter.NameHeader,
"Driver": formatter.DriverHeader,
"Scope": formatter.ScopeHeader,
"IPv6": ipv6Header,
"Internal": internalHeader,
"Labels": formatter.LabelsHeader,
"CreatedAt": formatter.CreatedAtHeader,
}
return ctx.Write(&networkCtx, render)
}
type networkContext struct {
formatter.HeaderContext
trunc bool
n types.NetworkResource
}
func (c *networkContext) MarshalJSON() ([]byte, error) {
return formatter.MarshalJSON(c)
}
func (c *networkContext) ID() string {
if c.trunc {
return stringid.TruncateID(c.n.ID)
}
return c.n.ID
}
func (c *networkContext) Name() string {
return c.n.Name
}
func (c *networkContext) Driver() string {
return c.n.Driver
}
func (c *networkContext) Scope() string {
return c.n.Scope
}
func (c *networkContext) IPv6() string {
return fmt.Sprintf("%v", c.n.EnableIPv6)
}
func (c *networkContext) Internal() string {
return fmt.Sprintf("%v", c.n.Internal)
}
func (c *networkContext) Labels() string {
if c.n.Labels == nil {
return ""
}
var joinLabels []string
for k, v := range c.n.Labels {
joinLabels = append(joinLabels, fmt.Sprintf("%s=%s", k, v))
}
return strings.Join(joinLabels, ",")
}
func (c *networkContext) Label(name string) string {
if c.n.Labels == nil {
return ""
}
return c.n.Labels[name]
}
func (c *networkContext) CreatedAt() string {
return c.n.Created.String()
}

View File

@ -0,0 +1,215 @@
package network
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"testing"
"time"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/internal/test"
"github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/stringid"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
)
func TestNetworkContext(t *testing.T) {
networkID := stringid.GenerateRandomID()
var ctx networkContext
cases := []struct {
networkCtx networkContext
expValue string
call func() string
}{
{networkContext{
n: types.NetworkResource{ID: networkID},
trunc: false,
}, networkID, ctx.ID},
{networkContext{
n: types.NetworkResource{ID: networkID},
trunc: true,
}, stringid.TruncateID(networkID), ctx.ID},
{networkContext{
n: types.NetworkResource{Name: "network_name"},
}, "network_name", ctx.Name},
{networkContext{
n: types.NetworkResource{Driver: "driver_name"},
}, "driver_name", ctx.Driver},
{networkContext{
n: types.NetworkResource{EnableIPv6: true},
}, "true", ctx.IPv6},
{networkContext{
n: types.NetworkResource{EnableIPv6: false},
}, "false", ctx.IPv6},
{networkContext{
n: types.NetworkResource{Internal: true},
}, "true", ctx.Internal},
{networkContext{
n: types.NetworkResource{Internal: false},
}, "false", ctx.Internal},
{networkContext{
n: types.NetworkResource{},
}, "", ctx.Labels},
{networkContext{
n: types.NetworkResource{Labels: map[string]string{"label1": "value1", "label2": "value2"}},
}, "label1=value1,label2=value2", ctx.Labels},
}
for _, c := range cases {
ctx = c.networkCtx
v := c.call()
if strings.Contains(v, ",") {
test.CompareMultipleValues(t, v, c.expValue)
} else if v != c.expValue {
t.Fatalf("Expected %s, was %s\n", c.expValue, v)
}
}
}
func TestNetworkContextWrite(t *testing.T) {
cases := []struct {
context formatter.Context
expected string
}{
// Errors
{
formatter.Context{Format: "{{InvalidFunction}}"},
`Template parsing error: template: :1: function "InvalidFunction" not defined
`,
},
{
formatter.Context{Format: "{{nil}}"},
`Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
`,
},
// Table format
{
formatter.Context{Format: NewFormat("table", false)},
`NETWORK ID NAME DRIVER SCOPE
networkID1 foobar_baz foo local
networkID2 foobar_bar bar local
`,
},
{
formatter.Context{Format: NewFormat("table", true)},
`networkID1
networkID2
`,
},
{
formatter.Context{Format: NewFormat("table {{.Name}}", false)},
`NAME
foobar_baz
foobar_bar
`,
},
{
formatter.Context{Format: NewFormat("table {{.Name}}", true)},
`NAME
foobar_baz
foobar_bar
`,
},
// Raw Format
{
formatter.Context{Format: NewFormat("raw", false)},
`network_id: networkID1
name: foobar_baz
driver: foo
scope: local
network_id: networkID2
name: foobar_bar
driver: bar
scope: local
`,
},
{
formatter.Context{Format: NewFormat("raw", true)},
`network_id: networkID1
network_id: networkID2
`,
},
// Custom Format
{
formatter.Context{Format: NewFormat("{{.Name}}", false)},
`foobar_baz
foobar_bar
`,
},
// Custom Format with CreatedAt
{
formatter.Context{Format: NewFormat("{{.Name}} {{.CreatedAt}}", false)},
`foobar_baz 2016-01-01 00:00:00 +0000 UTC
foobar_bar 2017-01-01 00:00:00 +0000 UTC
`,
},
}
timestamp1, _ := time.Parse("2006-01-02", "2016-01-01")
timestamp2, _ := time.Parse("2006-01-02", "2017-01-01")
for _, testcase := range cases {
networks := []types.NetworkResource{
{ID: "networkID1", Name: "foobar_baz", Driver: "foo", Scope: "local", Created: timestamp1},
{ID: "networkID2", Name: "foobar_bar", Driver: "bar", Scope: "local", Created: timestamp2},
}
out := bytes.NewBufferString("")
testcase.context.Output = out
err := FormatWrite(testcase.context, networks)
if err != nil {
assert.Error(t, err, testcase.expected)
} else {
assert.Check(t, is.Equal(testcase.expected, out.String()))
}
}
}
func TestNetworkContextWriteJSON(t *testing.T) {
networks := []types.NetworkResource{
{ID: "networkID1", Name: "foobar_baz"},
{ID: "networkID2", Name: "foobar_bar"},
}
expectedJSONs := []map[string]interface{}{
{"Driver": "", "ID": "networkID1", "IPv6": "false", "Internal": "false", "Labels": "", "Name": "foobar_baz", "Scope": "", "CreatedAt": "0001-01-01 00:00:00 +0000 UTC"},
{"Driver": "", "ID": "networkID2", "IPv6": "false", "Internal": "false", "Labels": "", "Name": "foobar_bar", "Scope": "", "CreatedAt": "0001-01-01 00:00:00 +0000 UTC"},
}
out := bytes.NewBufferString("")
err := FormatWrite(formatter.Context{Format: "{{json .}}", Output: out}, networks)
if err != nil {
t.Fatal(err)
}
for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
msg := fmt.Sprintf("Output: line %d: %s", i, line)
var m map[string]interface{}
err := json.Unmarshal([]byte(line), &m)
assert.NilError(t, err, msg)
assert.Check(t, is.DeepEqual(expectedJSONs[i], m), msg)
}
}
func TestNetworkContextWriteJSONField(t *testing.T) {
networks := []types.NetworkResource{
{ID: "networkID1", Name: "foobar_baz"},
{ID: "networkID2", Name: "foobar_bar"},
}
out := bytes.NewBufferString("")
err := FormatWrite(formatter.Context{Format: "{{json .ID}}", Output: out}, networks)
if err != nil {
t.Fatal(err)
}
for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
msg := fmt.Sprintf("Output: line %d: %s", i, line)
var s string
err := json.Unmarshal([]byte(line), &s)
assert.NilError(t, err, msg)
assert.Check(t, is.Equal(networks[i].ID, s), msg)
}
}

View File

@ -65,8 +65,8 @@ func runList(dockerCli command.Cli, options listOptions) error {
networksCtx := formatter.Context{
Output: dockerCli.Out(),
Format: formatter.NewNetworkFormat(format, options.quiet),
Format: NewFormat(format, options.quiet),
Trunc: !options.noTrunc,
}
return formatter.NetworkWrite(networksCtx, networkResources)
return FormatWrite(networksCtx, networkResources)
}