Переглянути джерело

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 9 роки тому
батько
коміт
295c27388d

+ 39 - 5
api/client/network.go

@@ -8,6 +8,7 @@ import (
 	"net"
 	"net"
 	"strings"
 	"strings"
 	"text/tabwriter"
 	"text/tabwriter"
+	"text/template"
 
 
 	"github.com/docker/docker/api/types"
 	"github.com/docker/docker/api/types"
 	Cli "github.com/docker/docker/cli"
 	Cli "github.com/docker/docker/cli"
@@ -203,14 +204,25 @@ func (cli *DockerCli) CmdNetworkLs(args ...string) error {
 // Usage: docker network inspect [OPTIONS] <NETWORK> [NETWORK...]
 // Usage: docker network inspect [OPTIONS] <NETWORK> [NETWORK...]
 func (cli *DockerCli) CmdNetworkInspect(args ...string) error {
 func (cli *DockerCli) CmdNetworkInspect(args ...string) error {
 	cmd := Cli.Subcmd("network inspect", []string{"NETWORK [NETWORK...]"}, "Displays detailed information on one or more networks", false)
 	cmd := Cli.Subcmd("network inspect", []string{"NETWORK [NETWORK...]"}, "Displays detailed information on one or more networks", false)
+	tmplStr := cmd.String([]string{"f", "-format"}, "", "Format the output using the given go template")
 	cmd.Require(flag.Min, 1)
 	cmd.Require(flag.Min, 1)
-	err := cmd.ParseFlags(args, true)
-	if err != nil {
+
+	if err := cmd.ParseFlags(args, true); err != nil {
 		return err
 		return err
 	}
 	}
 
 
+	var tmpl *template.Template
+	if *tmplStr != "" {
+		var err error
+		tmpl, err = template.New("").Funcs(funcMap).Parse(*tmplStr)
+		if err != nil {
+			return err
+		}
+	}
+
 	status := 0
 	status := 0
-	var networks []*types.NetworkResource
+	var networks []types.NetworkResource
+	buf := new(bytes.Buffer)
 	for _, name := range cmd.Args() {
 	for _, name := range cmd.Args() {
 		obj, _, err := readBody(cli.call("GET", "/networks/"+name, nil, nil))
 		obj, _, err := readBody(cli.call("GET", "/networks/"+name, nil, nil))
 		if err != nil {
 		if err != nil {
@@ -222,12 +234,34 @@ func (cli *DockerCli) CmdNetworkInspect(args ...string) error {
 			status = 1
 			status = 1
 			continue
 			continue
 		}
 		}
-		networkResource := types.NetworkResource{}
+		var networkResource types.NetworkResource
 		if err := json.NewDecoder(bytes.NewReader(obj)).Decode(&networkResource); err != nil {
 		if err := json.NewDecoder(bytes.NewReader(obj)).Decode(&networkResource); err != nil {
 			return err
 			return err
 		}
 		}
 
 
-		networks = append(networks, &networkResource)
+		if tmpl == nil {
+			networks = append(networks, networkResource)
+			continue
+		}
+
+		if err := tmpl.Execute(buf, &networkResource); err != nil {
+			if err := tmpl.Execute(buf, &networkResource); err != nil {
+				fmt.Fprintf(cli.err, "%s\n", err)
+				return Cli.StatusError{StatusCode: 1}
+			}
+		}
+		buf.WriteString("\n")
+	}
+
+	if tmpl != nil {
+		if _, err := io.Copy(cli.out, buf); err != nil {
+			return err
+		}
+		return nil
+	}
+
+	if len(networks) == 0 {
+		io.WriteString(cli.out, "[]")
 	}
 	}
 
 
 	b, err := json.MarshalIndent(networks, "", "    ")
 	b, err := json.MarshalIndent(networks, "", "    ")

+ 7 - 1
contrib/completion/bash/docker

@@ -1133,9 +1133,15 @@ _docker_network_disconnect() {
 }
 }
 
 
 _docker_network_inspect() {
 _docker_network_inspect() {
+	case "$prev" in
+		--format|-f)
+			return
+			;;
+	esac
+
 	case "$cur" in
 	case "$cur" in
 		-*)
 		-*)
-			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
+			COMPREPLY=( $( compgen -W "--format -f --help" -- "$cur" ) )
 			;;
 			;;
 		*)
 		*)
 			__docker_networks
 			__docker_networks

+ 7 - 1
contrib/completion/zsh/_docker

@@ -309,9 +309,10 @@ __docker_network_subcommand() {
                 "($help)*"{-o=,--opt=}"[Set driver specific options]:key=value: " \
                 "($help)*"{-o=,--opt=}"[Set driver specific options]:key=value: " \
                 "($help -)1:Network Name: " && ret=0
                 "($help -)1:Network Name: " && ret=0
             ;;
             ;;
-        (inspect|rm)
+        (inspect)
             _arguments $(__docker_arguments) \
             _arguments $(__docker_arguments) \
                 $opts_help \
                 $opts_help \
+                "($help -f --format)"{-f=,--format=}"[Format the output using the given go template]:template: " \
                 "($help -)*:network:__docker_networks" && ret=0
                 "($help -)*:network:__docker_networks" && ret=0
             ;;
             ;;
         (ls)
         (ls)
@@ -320,6 +321,11 @@ __docker_network_subcommand() {
                 "($help)--no-trunc[Do not truncate the output]" \
                 "($help)--no-trunc[Do not truncate the output]" \
                 "($help -q --quiet)"{-q,--quiet}"[Only display numeric IDs]" && ret=0
                 "($help -q --quiet)"{-q,--quiet}"[Only display numeric IDs]" && ret=0
             ;;
             ;;
+        (rm)
+            _arguments $(__docker_arguments) \
+                $opts_help \
+                "($help -)*:network:__docker_networks" && ret=0
+            ;;
         (help)
         (help)
             _arguments $(__docker_arguments) ":subcommand:__docker_network_commands" && ret=0
             _arguments $(__docker_arguments) ":subcommand:__docker_network_commands" && ret=0
             ;;
             ;;

+ 6 - 1
docs/reference/commandline/network_inspect.md

@@ -14,6 +14,7 @@ parent = "smn_cli"
 
 
     Displays detailed information on a network
     Displays detailed information on a network
 
 
+      -f, --format=       Format the output using the given go template.
       --help=false       Print usage
       --help=false       Print usage
 
 
 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:
 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
 bda12f8922785d1f160be70736f26c1e331ab8aaf8ed8d56728508f2e2fd4727
 bda12f8922785d1f160be70736f26c1e331ab8aaf8ed8d56728508f2e2fd4727
 ```
 ```
 
 
-The `network inspect` command shows the containers, by id, in its results.
+The `network inspect` command shows the containers, by id, in its
+results. You can specify an alternate format to execute a given
+template for each result. Go's
+[text/template](http://golang.org/pkg/text/template/) package describes all the
+details of the format.
 
 
 ```bash
 ```bash
 $ sudo docker network inspect bridge
 $ sudo docker network inspect bridge

+ 11 - 0
integration-cli/docker_cli_network_unix_test.go

@@ -293,6 +293,17 @@ func (s *DockerSuite) TestDockerNetworkDeleteMultiple(c *check.C) {
 	assertNwIsAvailable(c, "testDelMulti2")
 	assertNwIsAvailable(c, "testDelMulti2")
 }
 }
 
 
+func (s *DockerSuite) TestDockerNetworkInspect(c *check.C) {
+	out, _ := dockerCmd(c, "network", "inspect", "host")
+	networkResources := []types.NetworkResource{}
+	err := json.Unmarshal([]byte(out), &networkResources)
+	c.Assert(err, check.IsNil)
+	c.Assert(networkResources, checker.HasLen, 1)
+
+	out, _ = dockerCmd(c, "network", "inspect", "--format='{{ .Name }}'", "host")
+	c.Assert(strings.TrimSpace(out), check.Equals, "host")
+}
+
 func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) {
 func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) {
 	out, _ := dockerCmd(c, "network", "inspect", "host", "none")
 	out, _ := dockerCmd(c, "network", "inspect", "host", "none")
 	networkResources := []types.NetworkResource{}
 	networkResources := []types.NetworkResource{}

+ 8 - 1
man/docker-network-inspect.1.md

@@ -6,6 +6,7 @@ docker-network-inspect - inspect a network
 
 
 # SYNOPSIS
 # SYNOPSIS
 **docker network inspect**
 **docker network inspect**
+[**-f**|**--format**[=*FORMAT*]]
 [**--help**]
 [**--help**]
 NETWORK [NETWORK...]
 NETWORK [NETWORK...]
 
 
@@ -21,7 +22,11 @@ $ sudo docker run -itd --name=container2 busybox
 bda12f8922785d1f160be70736f26c1e331ab8aaf8ed8d56728508f2e2fd4727
 bda12f8922785d1f160be70736f26c1e331ab8aaf8ed8d56728508f2e2fd4727
 ```
 ```
 
 
-The `network inspect` command shows the containers, by id, in its results.
+The `network inspect` command shows the containers, by id, in its
+results. You can specify an alternate format to execute a given
+template for each result. Go's
+[text/template](http://golang.org/pkg/text/template/) package
+describes all the details of the format.
 
 
 ```bash
 ```bash
 $ sudo docker network inspect bridge
 $ sudo docker network inspect bridge
@@ -50,6 +55,8 @@ $ sudo docker network inspect bridge
 
 
 
 
 # OPTIONS
 # OPTIONS
+**-f**, **--format**=""
+  Format the output using the given go template.
 
 
 **--help**
 **--help**
   Print usage statement
   Print usage statement