Browse code

Make network ls output order

Fixes: #20328
We sort network ls output with incresing order,
it may make output more easy to consume for users.

Signed-off-by: Kai Qiang Wu(Kennan) <wkqwu@cn.ibm.com>

Kai Qiang Wu(Kennan) authored on 2016/02/17 12:11:37
Showing 2 changed files
... ...
@@ -3,6 +3,7 @@ package client
3 3
 import (
4 4
 	"fmt"
5 5
 	"net"
6
+	"sort"
6 7
 	"strings"
7 8
 	"text/tabwriter"
8 9
 
... ...
@@ -192,7 +193,7 @@ func (cli *DockerCli) CmdNetworkLs(args ...string) error {
192 192
 	if !*quiet {
193 193
 		fmt.Fprintln(wr, "NETWORK ID\tNAME\tDRIVER")
194 194
 	}
195
-
195
+	sort.Sort(byNetworkName(networkResources))
196 196
 	for _, networkResource := range networkResources {
197 197
 		ID := networkResource.ID
198 198
 		netName := networkResource.Name
... ...
@@ -214,6 +215,12 @@ func (cli *DockerCli) CmdNetworkLs(args ...string) error {
214 214
 	return nil
215 215
 }
216 216
 
217
+type byNetworkName []types.NetworkResource
218
+
219
+func (r byNetworkName) Len() int           { return len(r) }
220
+func (r byNetworkName) Swap(i, j int)      { r[i], r[j] = r[j], r[i] }
221
+func (r byNetworkName) Less(i, j int) bool { return r[i].Name < r[j].Name }
222
+
217 223
 // CmdNetworkInspect inspects the network object for more details
218 224
 //
219 225
 // Usage: docker network inspect [OPTIONS] <NETWORK> [NETWORK...]
... ...
@@ -10,7 +10,6 @@ import (
10 10
 	"net/http"
11 11
 	"net/http/httptest"
12 12
 	"os"
13
-	"sort"
14 13
 	"strings"
15 14
 	"time"
16 15
 
... ...
@@ -259,9 +258,6 @@ func assertNwList(c *check.C, out string, expectNws []string) {
259 259
 		// wrap all network name in nwList
260 260
 		nwList = append(nwList, netFields[1])
261 261
 	}
262
-	// first need to sort out and expected
263
-	sort.StringSlice(nwList).Sort()
264
-	sort.StringSlice(expectNws).Sort()
265 262
 
266 263
 	// network ls should contains all expected networks
267 264
 	c.Assert(nwList, checker.DeepEquals, expectNws)
... ...
@@ -321,11 +317,11 @@ func (s *DockerNetworkSuite) TestDockerNetworkLsFilter(c *check.C) {
321 321
 	// filter with partial ID and partial name
322 322
 	// only show 'bridge' and 'dev' network
323 323
 	out, _ = dockerCmd(c, "network", "ls", "-f", "id="+networkID[0:5], "-f", "name=dge")
324
-	assertNwList(c, out, []string{"dev", "bridge"})
324
+	assertNwList(c, out, []string{"bridge", "dev"})
325 325
 
326 326
 	// only show built-in network (bridge, none, host)
327 327
 	out, _ = dockerCmd(c, "network", "ls", "-f", "type=builtin")
328
-	assertNwList(c, out, []string{"bridge", "none", "host"})
328
+	assertNwList(c, out, []string{"bridge", "host", "none"})
329 329
 
330 330
 	// only show custom networks (dev)
331 331
 	out, _ = dockerCmd(c, "network", "ls", "-f", "type=custom")
... ...
@@ -334,7 +330,7 @@ func (s *DockerNetworkSuite) TestDockerNetworkLsFilter(c *check.C) {
334 334
 	// show all networks with filter
335 335
 	// it should be equivalent of ls without option
336 336
 	out, _ = dockerCmd(c, "network", "ls", "-f", "type=custom", "-f", "type=builtin")
337
-	assertNwList(c, out, []string{"dev", "bridge", "host", "none"})
337
+	assertNwList(c, out, []string{"bridge", "dev", "host", "none"})
338 338
 }
339 339
 
340 340
 func (s *DockerNetworkSuite) TestDockerNetworkCreateDelete(c *check.C) {