From b807d24e56d6affcad722c5238c6bda1639c9455 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Wed, 8 Mar 2017 10:29:15 -0800 Subject: [PATCH] Add hidden placeholder of `.Self` for `docker node ls --format` This commit adds a hidden placeholder of `.Self` for `docker node ls --format` so that if the node is the same as the current docker daemon, then a `*` is outputed. Signed-off-by: Yong Tang --- command/formatter/node.go | 14 ++++++++------ command/formatter/node_test.go | 4 ++-- command/node/list_test.go | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/command/formatter/node.go b/command/formatter/node.go index bd478e57f2..6a6fb43c1a 100644 --- a/command/formatter/node.go +++ b/command/formatter/node.go @@ -7,9 +7,10 @@ import ( ) const ( - defaultNodeTableFormat = "table {{.ID}}\t{{.Hostname}}\t{{.Status}}\t{{.Availability}}\t{{.ManagerStatus}}" + defaultNodeTableFormat = "table {{.ID}} {{if .Self}}*{{else}} {{ end }}\t{{.Hostname}}\t{{.Status}}\t{{.Availability}}\t{{.ManagerStatus}}" nodeIDHeader = "ID" + selfHeader = "" hostnameHeader = "HOSTNAME" availabilityHeader = "AVAILABILITY" managerStatusHeader = "MANAGER STATUS" @@ -46,6 +47,7 @@ func NodeWrite(ctx Context, nodes []swarm.Node, info types.Info) error { nodeCtx := nodeContext{} nodeCtx.header = nodeHeaderContext{ "ID": nodeIDHeader, + "Self": selfHeader, "Hostname": hostnameHeader, "Status": statusHeader, "Availability": availabilityHeader, @@ -67,11 +69,11 @@ func (c *nodeContext) MarshalJSON() ([]byte, error) { } func (c *nodeContext) ID() string { - nodeID := c.n.ID - if nodeID == c.info.Swarm.NodeID { - nodeID = nodeID + " *" - } - return nodeID + return c.n.ID +} + +func (c *nodeContext) Self() bool { + return c.n.ID == c.info.Swarm.NodeID } func (c *nodeContext) Hostname() string { diff --git a/command/formatter/node_test.go b/command/formatter/node_test.go index e3e341fc8b..86f4979d3f 100644 --- a/command/formatter/node_test.go +++ b/command/formatter/node_test.go @@ -148,8 +148,8 @@ func TestNodeContextWriteJSON(t *testing.T) { {ID: "nodeID2", Description: swarm.NodeDescription{Hostname: "foobar_bar"}}, } expectedJSONs := []map[string]interface{}{ - {"Availability": "", "Hostname": "foobar_baz", "ID": "nodeID1", "ManagerStatus": "", "Status": ""}, - {"Availability": "", "Hostname": "foobar_bar", "ID": "nodeID2", "ManagerStatus": "", "Status": ""}, + {"Availability": "", "Hostname": "foobar_baz", "ID": "nodeID1", "ManagerStatus": "", "Status": "", "Self": false}, + {"Availability": "", "Hostname": "foobar_bar", "ID": "nodeID2", "ManagerStatus": "", "Status": "", "Self": false}, } out := bytes.NewBufferString("") diff --git a/command/node/list_test.go b/command/node/list_test.go index 13a21d1b52..4b8d906c3a 100644 --- a/command/node/list_test.go +++ b/command/node/list_test.go @@ -129,7 +129,7 @@ func TestNodeListDefaultFormat(t *testing.T) { }) cmd := newListCommand(cli) assert.NilError(t, cmd.Execute()) - assert.Contains(t, buf.String(), `nodeID1 *: nodeHostname1 Ready/Leader`) + assert.Contains(t, buf.String(), `nodeID1: nodeHostname1 Ready/Leader`) assert.Contains(t, buf.String(), `nodeID2: nodeHostname2 Ready/Reachable`) assert.Contains(t, buf.String(), `nodeID3: nodeHostname3 Ready`) }