Browse code

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>

Yong Tang authored on 2017/03/09 03:29:15
Showing 5 changed files
... ...
@@ -7,9 +7,10 @@ import (
7 7
 )
8 8
 
9 9
 const (
10
-	defaultNodeTableFormat = "table {{.ID}}\t{{.Hostname}}\t{{.Status}}\t{{.Availability}}\t{{.ManagerStatus}}"
10
+	defaultNodeTableFormat = "table {{.ID}} {{if .Self}}*{{else}} {{ end }}\t{{.Hostname}}\t{{.Status}}\t{{.Availability}}\t{{.ManagerStatus}}"
11 11
 
12 12
 	nodeIDHeader        = "ID"
13
+	selfHeader          = ""
13 14
 	hostnameHeader      = "HOSTNAME"
14 15
 	availabilityHeader  = "AVAILABILITY"
15 16
 	managerStatusHeader = "MANAGER STATUS"
... ...
@@ -46,6 +47,7 @@ func NodeWrite(ctx Context, nodes []swarm.Node, info types.Info) error {
46 46
 	nodeCtx := nodeContext{}
47 47
 	nodeCtx.header = nodeHeaderContext{
48 48
 		"ID":            nodeIDHeader,
49
+		"Self":          selfHeader,
49 50
 		"Hostname":      hostnameHeader,
50 51
 		"Status":        statusHeader,
51 52
 		"Availability":  availabilityHeader,
... ...
@@ -67,11 +69,11 @@ func (c *nodeContext) MarshalJSON() ([]byte, error) {
67 67
 }
68 68
 
69 69
 func (c *nodeContext) ID() string {
70
-	nodeID := c.n.ID
71
-	if nodeID == c.info.Swarm.NodeID {
72
-		nodeID = nodeID + " *"
73
-	}
74
-	return nodeID
70
+	return c.n.ID
71
+}
72
+
73
+func (c *nodeContext) Self() bool {
74
+	return c.n.ID == c.info.Swarm.NodeID
75 75
 }
76 76
 
77 77
 func (c *nodeContext) Hostname() string {
... ...
@@ -148,8 +148,8 @@ func TestNodeContextWriteJSON(t *testing.T) {
148 148
 		{ID: "nodeID2", Description: swarm.NodeDescription{Hostname: "foobar_bar"}},
149 149
 	}
150 150
 	expectedJSONs := []map[string]interface{}{
151
-		{"Availability": "", "Hostname": "foobar_baz", "ID": "nodeID1", "ManagerStatus": "", "Status": ""},
152
-		{"Availability": "", "Hostname": "foobar_bar", "ID": "nodeID2", "ManagerStatus": "", "Status": ""},
151
+		{"Availability": "", "Hostname": "foobar_baz", "ID": "nodeID1", "ManagerStatus": "", "Status": "", "Self": false},
152
+		{"Availability": "", "Hostname": "foobar_bar", "ID": "nodeID2", "ManagerStatus": "", "Status": "", "Self": false},
153 153
 	}
154 154
 
155 155
 	out := bytes.NewBufferString("")
... ...
@@ -129,7 +129,7 @@ func TestNodeListDefaultFormat(t *testing.T) {
129 129
 	})
130 130
 	cmd := newListCommand(cli)
131 131
 	assert.NilError(t, cmd.Execute())
132
-	assert.Contains(t, buf.String(), `nodeID1 *: nodeHostname1 Ready/Leader`)
132
+	assert.Contains(t, buf.String(), `nodeID1: nodeHostname1 Ready/Leader`)
133 133
 	assert.Contains(t, buf.String(), `nodeID2: nodeHostname2 Ready/Reachable`)
134 134
 	assert.Contains(t, buf.String(), `nodeID3: nodeHostname3 Ready`)
135 135
 }
... ...
@@ -169,8 +169,8 @@ format. For a list of supported formatting directives, see
169 169
 
170 170
 The property `nodesFormat` specifies the default format for `docker node ls` output.
171 171
 When the `--format` flag is not provided with the `docker node ls` command,
172
-Docker's client uses this property. If this property is not set, the client
173
-falls back to the default table format. For a list of supported formatting
172
+Docker's client uses the value of `nodesFormat`. If the value of `nodesFormat` is not set,
173
+the client uses the default table format. For a list of supported formatting
174 174
 directives, see the [**Formatting** section in the `docker node ls` documentation](node_ls.md)
175 175
 
176 176
 The property `credsStore` specifies an external binary to serve as the default
... ...
@@ -46,9 +46,10 @@ ID                           HOSTNAME        STATUS  AVAILABILITY  MANAGER STATU
46 46
 38ciaotwjuritcdtn9npbnkuz    swarm-worker1   Ready   Active
47 47
 e216jshn25ckzbvmwlnh5jr3g *  swarm-manager1  Ready   Active        Leader
48 48
 ```
49
-> **Note:**
50
-> If the `ID` field of the node is followed by a `*` (e.g., `e216jshn25ckzbvmwlnh5jr3g *`)
51
-> in the above example output, then this node is also the node of the current docker daemon.
49
+> **Note**:
50
+> In the above example output, there is a hidden column of `.Self` that indicates if the
51
+> node is the same node as the current docker daemon. A `*` (e.g., `e216jshn25ckzbvmwlnh5jr3g *`)
52
+> means this node is the current docker daemon.
52 53
 
53 54
 
54 55
 ### Filtering
... ...
@@ -139,6 +140,7 @@ Valid placeholders for the Go template are listed below:
139 139
 Placeholder      | Description
140 140
 -----------------|------------------------------------------------------------------------------------------
141 141
 `.ID`            | Node ID
142
+`.Self`          | Node of the daemon (`true/false`, `true`indicates that the node is the same as current docker daemon)
142 143
 `.Hostname`      | Node hostname
143 144
 `.Status`        | Node status
144 145
 `.Availability`  | Node availability ("active", "pause", or "drain")
... ...
@@ -153,7 +155,7 @@ The following example uses a template without headers and outputs the
153 153
 
154 154
 ```bash
155 155
 $ docker node ls --format "{{.ID}}: {{.Hostname}}"
156
-e216jshn25ckzbvmwlnh5jr3g *: swarm-manager1
156
+e216jshn25ckzbvmwlnh5jr3g: swarm-manager1
157 157
 ``
158 158
 
159 159