diff --git a/cli/command/formatter/node.go b/cli/command/formatter/node.go index bd478e57f2..6a6fb43c1a 100644 --- a/cli/command/formatter/node.go +++ b/cli/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/cli/command/formatter/node_test.go b/cli/command/formatter/node_test.go index e3e341fc8b..86f4979d3f 100644 --- a/cli/command/formatter/node_test.go +++ b/cli/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/cli/command/node/list_test.go b/cli/command/node/list_test.go index 13a21d1b52..4b8d906c3a 100644 --- a/cli/command/node/list_test.go +++ b/cli/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`) } diff --git a/docs/reference/commandline/cli.md b/docs/reference/commandline/cli.md index 0d48d19725..a8529e728f 100644 --- a/docs/reference/commandline/cli.md +++ b/docs/reference/commandline/cli.md @@ -169,8 +169,8 @@ format. For a list of supported formatting directives, see The property `nodesFormat` specifies the default format for `docker node ls` output. When the `--format` flag is not provided with the `docker node ls` command, -Docker's client uses this property. If this property is not set, the client -falls back to the default table format. For a list of supported formatting +Docker's client uses the value of `nodesFormat`. If the value of `nodesFormat` is not set, +the client uses the default table format. For a list of supported formatting directives, see the [**Formatting** section in the `docker node ls` documentation](node_ls.md) The property `credsStore` specifies an external binary to serve as the default diff --git a/docs/reference/commandline/node_ls.md b/docs/reference/commandline/node_ls.md index ad2ffcd050..8162e11b58 100644 --- a/docs/reference/commandline/node_ls.md +++ b/docs/reference/commandline/node_ls.md @@ -46,9 +46,10 @@ ID HOSTNAME STATUS AVAILABILITY MANAGER STATU 38ciaotwjuritcdtn9npbnkuz swarm-worker1 Ready Active e216jshn25ckzbvmwlnh5jr3g * swarm-manager1 Ready Active Leader ``` -> **Note:** -> If the `ID` field of the node is followed by a `*` (e.g., `e216jshn25ckzbvmwlnh5jr3g *`) -> in the above example output, then this node is also the node of the current docker daemon. +> **Note**: +> In the above example output, there is a hidden column of `.Self` that indicates if the +> node is the same node as the current docker daemon. A `*` (e.g., `e216jshn25ckzbvmwlnh5jr3g *`) +> means this node is the current docker daemon. ### Filtering @@ -139,6 +140,7 @@ Valid placeholders for the Go template are listed below: Placeholder | Description -----------------|------------------------------------------------------------------------------------------ `.ID` | Node ID +`.Self` | Node of the daemon (`true/false`, `true`indicates that the node is the same as current docker daemon) `.Hostname` | Node hostname `.Status` | Node status `.Availability` | Node availability ("active", "pause", or "drain") @@ -153,7 +155,7 @@ The following example uses a template without headers and outputs the ```bash $ docker node ls --format "{{.ID}}: {{.Hostname}}" -e216jshn25ckzbvmwlnh5jr3g *: swarm-manager1 +e216jshn25ckzbvmwlnh5jr3g: swarm-manager1 ``