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 <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang 2017-03-08 10:29:15 -08:00
parent 4fc1d6782c
commit b807d24e56
3 changed files with 11 additions and 9 deletions

View File

@ -7,9 +7,10 @@ import (
) )
const ( 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" nodeIDHeader = "ID"
selfHeader = ""
hostnameHeader = "HOSTNAME" hostnameHeader = "HOSTNAME"
availabilityHeader = "AVAILABILITY" availabilityHeader = "AVAILABILITY"
managerStatusHeader = "MANAGER STATUS" managerStatusHeader = "MANAGER STATUS"
@ -46,6 +47,7 @@ func NodeWrite(ctx Context, nodes []swarm.Node, info types.Info) error {
nodeCtx := nodeContext{} nodeCtx := nodeContext{}
nodeCtx.header = nodeHeaderContext{ nodeCtx.header = nodeHeaderContext{
"ID": nodeIDHeader, "ID": nodeIDHeader,
"Self": selfHeader,
"Hostname": hostnameHeader, "Hostname": hostnameHeader,
"Status": statusHeader, "Status": statusHeader,
"Availability": availabilityHeader, "Availability": availabilityHeader,
@ -67,11 +69,11 @@ func (c *nodeContext) MarshalJSON() ([]byte, error) {
} }
func (c *nodeContext) ID() string { func (c *nodeContext) ID() string {
nodeID := c.n.ID return c.n.ID
if nodeID == c.info.Swarm.NodeID {
nodeID = nodeID + " *"
} }
return nodeID
func (c *nodeContext) Self() bool {
return c.n.ID == c.info.Swarm.NodeID
} }
func (c *nodeContext) Hostname() string { func (c *nodeContext) Hostname() string {

View File

@ -148,8 +148,8 @@ func TestNodeContextWriteJSON(t *testing.T) {
{ID: "nodeID2", Description: swarm.NodeDescription{Hostname: "foobar_bar"}}, {ID: "nodeID2", Description: swarm.NodeDescription{Hostname: "foobar_bar"}},
} }
expectedJSONs := []map[string]interface{}{ expectedJSONs := []map[string]interface{}{
{"Availability": "", "Hostname": "foobar_baz", "ID": "nodeID1", "ManagerStatus": "", "Status": ""}, {"Availability": "", "Hostname": "foobar_baz", "ID": "nodeID1", "ManagerStatus": "", "Status": "", "Self": false},
{"Availability": "", "Hostname": "foobar_bar", "ID": "nodeID2", "ManagerStatus": "", "Status": ""}, {"Availability": "", "Hostname": "foobar_bar", "ID": "nodeID2", "ManagerStatus": "", "Status": "", "Self": false},
} }
out := bytes.NewBufferString("") out := bytes.NewBufferString("")

View File

@ -129,7 +129,7 @@ func TestNodeListDefaultFormat(t *testing.T) {
}) })
cmd := newListCommand(cli) cmd := newListCommand(cli)
assert.NilError(t, cmd.Execute()) 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(), `nodeID2: nodeHostname2 Ready/Reachable`)
assert.Contains(t, buf.String(), `nodeID3: nodeHostname3 Ready`) assert.Contains(t, buf.String(), `nodeID3: nodeHostname3 Ready`)
} }