Browse code

Add format flag to network inspect

…for consistency as docker inspect and docker volume inspect supports it too

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2015/12/03 06:32:10
Showing 6 changed files
... ...
@@ -8,6 +8,7 @@ import (
8 8
 	"net"
9 9
 	"strings"
10 10
 	"text/tabwriter"
11
+	"text/template"
11 12
 
12 13
 	"github.com/docker/docker/api/types"
13 14
 	Cli "github.com/docker/docker/cli"
... ...
@@ -203,14 +204,25 @@ func (cli *DockerCli) CmdNetworkLs(args ...string) error {
203 203
 // Usage: docker network inspect [OPTIONS] <NETWORK> [NETWORK...]
204 204
 func (cli *DockerCli) CmdNetworkInspect(args ...string) error {
205 205
 	cmd := Cli.Subcmd("network inspect", []string{"NETWORK [NETWORK...]"}, "Displays detailed information on one or more networks", false)
206
+	tmplStr := cmd.String([]string{"f", "-format"}, "", "Format the output using the given go template")
206 207
 	cmd.Require(flag.Min, 1)
207
-	err := cmd.ParseFlags(args, true)
208
-	if err != nil {
208
+
209
+	if err := cmd.ParseFlags(args, true); err != nil {
209 210
 		return err
210 211
 	}
211 212
 
213
+	var tmpl *template.Template
214
+	if *tmplStr != "" {
215
+		var err error
216
+		tmpl, err = template.New("").Funcs(funcMap).Parse(*tmplStr)
217
+		if err != nil {
218
+			return err
219
+		}
220
+	}
221
+
212 222
 	status := 0
213
-	var networks []*types.NetworkResource
223
+	var networks []types.NetworkResource
224
+	buf := new(bytes.Buffer)
214 225
 	for _, name := range cmd.Args() {
215 226
 		obj, _, err := readBody(cli.call("GET", "/networks/"+name, nil, nil))
216 227
 		if err != nil {
... ...
@@ -222,12 +234,34 @@ func (cli *DockerCli) CmdNetworkInspect(args ...string) error {
222 222
 			status = 1
223 223
 			continue
224 224
 		}
225
-		networkResource := types.NetworkResource{}
225
+		var networkResource types.NetworkResource
226 226
 		if err := json.NewDecoder(bytes.NewReader(obj)).Decode(&networkResource); err != nil {
227 227
 			return err
228 228
 		}
229 229
 
230
-		networks = append(networks, &networkResource)
230
+		if tmpl == nil {
231
+			networks = append(networks, networkResource)
232
+			continue
233
+		}
234
+
235
+		if err := tmpl.Execute(buf, &networkResource); err != nil {
236
+			if err := tmpl.Execute(buf, &networkResource); err != nil {
237
+				fmt.Fprintf(cli.err, "%s\n", err)
238
+				return Cli.StatusError{StatusCode: 1}
239
+			}
240
+		}
241
+		buf.WriteString("\n")
242
+	}
243
+
244
+	if tmpl != nil {
245
+		if _, err := io.Copy(cli.out, buf); err != nil {
246
+			return err
247
+		}
248
+		return nil
249
+	}
250
+
251
+	if len(networks) == 0 {
252
+		io.WriteString(cli.out, "[]")
231 253
 	}
232 254
 
233 255
 	b, err := json.MarshalIndent(networks, "", "    ")
... ...
@@ -1133,9 +1133,15 @@ _docker_network_disconnect() {
1133 1133
 }
1134 1134
 
1135 1135
 _docker_network_inspect() {
1136
+	case "$prev" in
1137
+		--format|-f)
1138
+			return
1139
+			;;
1140
+	esac
1141
+
1136 1142
 	case "$cur" in
1137 1143
 		-*)
1138
-			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
1144
+			COMPREPLY=( $( compgen -W "--format -f --help" -- "$cur" ) )
1139 1145
 			;;
1140 1146
 		*)
1141 1147
 			__docker_networks
... ...
@@ -309,9 +309,10 @@ __docker_network_subcommand() {
309 309
                 "($help)*"{-o=,--opt=}"[Set driver specific options]:key=value: " \
310 310
                 "($help -)1:Network Name: " && ret=0
311 311
             ;;
312
-        (inspect|rm)
312
+        (inspect)
313 313
             _arguments $(__docker_arguments) \
314 314
                 $opts_help \
315
+                "($help -f --format)"{-f=,--format=}"[Format the output using the given go template]:template: " \
315 316
                 "($help -)*:network:__docker_networks" && ret=0
316 317
             ;;
317 318
         (ls)
... ...
@@ -320,6 +321,11 @@ __docker_network_subcommand() {
320 320
                 "($help)--no-trunc[Do not truncate the output]" \
321 321
                 "($help -q --quiet)"{-q,--quiet}"[Only display numeric IDs]" && ret=0
322 322
             ;;
323
+        (rm)
324
+            _arguments $(__docker_arguments) \
325
+                $opts_help \
326
+                "($help -)*:network:__docker_networks" && ret=0
327
+            ;;
323 328
         (help)
324 329
             _arguments $(__docker_arguments) ":subcommand:__docker_network_commands" && ret=0
325 330
             ;;
... ...
@@ -14,6 +14,7 @@ parent = "smn_cli"
14 14
 
15 15
     Displays detailed information on a network
16 16
 
17
+      -f, --format=       Format the output using the given go template.
17 18
       --help=false       Print usage
18 19
 
19 20
 Returns information about one or more networks. By default, this command renders all results in a JSON object. For example, if you connect two containers to a network:
... ...
@@ -26,7 +27,11 @@ $ sudo docker run -itd --name=container2 busybox
26 26
 bda12f8922785d1f160be70736f26c1e331ab8aaf8ed8d56728508f2e2fd4727
27 27
 ```
28 28
 
29
-The `network inspect` command shows the containers, by id, in its results.
29
+The `network inspect` command shows the containers, by id, in its
30
+results. You can specify an alternate format to execute a given
31
+template for each result. Go's
32
+[text/template](http://golang.org/pkg/text/template/) package describes all the
33
+details of the format.
30 34
 
31 35
 ```bash
32 36
 $ sudo docker network inspect bridge
... ...
@@ -293,6 +293,17 @@ func (s *DockerSuite) TestDockerNetworkDeleteMultiple(c *check.C) {
293 293
 	assertNwIsAvailable(c, "testDelMulti2")
294 294
 }
295 295
 
296
+func (s *DockerSuite) TestDockerNetworkInspect(c *check.C) {
297
+	out, _ := dockerCmd(c, "network", "inspect", "host")
298
+	networkResources := []types.NetworkResource{}
299
+	err := json.Unmarshal([]byte(out), &networkResources)
300
+	c.Assert(err, check.IsNil)
301
+	c.Assert(networkResources, checker.HasLen, 1)
302
+
303
+	out, _ = dockerCmd(c, "network", "inspect", "--format='{{ .Name }}'", "host")
304
+	c.Assert(strings.TrimSpace(out), check.Equals, "host")
305
+}
306
+
296 307
 func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) {
297 308
 	out, _ := dockerCmd(c, "network", "inspect", "host", "none")
298 309
 	networkResources := []types.NetworkResource{}
... ...
@@ -6,6 +6,7 @@ docker-network-inspect - inspect a network
6 6
 
7 7
 # SYNOPSIS
8 8
 **docker network inspect**
9
+[**-f**|**--format**[=*FORMAT*]]
9 10
 [**--help**]
10 11
 NETWORK [NETWORK...]
11 12
 
... ...
@@ -21,7 +22,11 @@ $ sudo docker run -itd --name=container2 busybox
21 21
 bda12f8922785d1f160be70736f26c1e331ab8aaf8ed8d56728508f2e2fd4727
22 22
 ```
23 23
 
24
-The `network inspect` command shows the containers, by id, in its results.
24
+The `network inspect` command shows the containers, by id, in its
25
+results. You can specify an alternate format to execute a given
26
+template for each result. Go's
27
+[text/template](http://golang.org/pkg/text/template/) package
28
+describes all the details of the format.
25 29
 
26 30
 ```bash
27 31
 $ sudo docker network inspect bridge
... ...
@@ -50,6 +55,8 @@ $ sudo docker network inspect bridge
50 50
 
51 51
 
52 52
 # OPTIONS
53
+**-f**, **--format**=""
54
+  Format the output using the given go template.
53 55
 
54 56
 **--help**
55 57
   Print usage statement