Browse code

add `--format` flag to `docker info`

Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>

Akihiro Suda authored on 2016/06/21 18:15:17
Showing 7 changed files
... ...
@@ -11,32 +11,50 @@ import (
11 11
 	"github.com/docker/docker/cli"
12 12
 	"github.com/docker/docker/pkg/ioutils"
13 13
 	"github.com/docker/docker/utils"
14
+	"github.com/docker/docker/utils/templates"
15
+	"github.com/docker/engine-api/types"
14 16
 	"github.com/docker/engine-api/types/swarm"
15 17
 	"github.com/docker/go-units"
16 18
 	"github.com/spf13/cobra"
17 19
 )
18 20
 
21
+type infoOptions struct {
22
+	format string
23
+}
24
+
19 25
 // NewInfoCommand creates a new cobra.Command for `docker info`
20 26
 func NewInfoCommand(dockerCli *client.DockerCli) *cobra.Command {
27
+	var opts infoOptions
28
+
21 29
 	cmd := &cobra.Command{
22 30
 		Use:   "info",
23 31
 		Short: "Display system-wide information",
24 32
 		Args:  cli.NoArgs,
25 33
 		RunE: func(cmd *cobra.Command, args []string) error {
26
-			return runInfo(dockerCli)
34
+			return runInfo(dockerCli, &opts)
27 35
 		},
28 36
 	}
29
-	return cmd
30 37
 
38
+	flags := cmd.Flags()
39
+
40
+	flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given go template")
41
+
42
+	return cmd
31 43
 }
32 44
 
33
-func runInfo(dockerCli *client.DockerCli) error {
45
+func runInfo(dockerCli *client.DockerCli, opts *infoOptions) error {
34 46
 	ctx := context.Background()
35 47
 	info, err := dockerCli.Client().Info(ctx)
36 48
 	if err != nil {
37 49
 		return err
38 50
 	}
51
+	if opts.format == "" {
52
+		return prettyPrintInfo(dockerCli, info)
53
+	}
54
+	return formatInfo(dockerCli, info, opts.format)
55
+}
39 56
 
57
+func prettyPrintInfo(dockerCli *client.DockerCli, info types.Info) error {
40 58
 	fmt.Fprintf(dockerCli.Out(), "Containers: %d\n", info.Containers)
41 59
 	fmt.Fprintf(dockerCli.Out(), " Running: %d\n", info.ContainersRunning)
42 60
 	fmt.Fprintf(dockerCli.Out(), " Paused: %d\n", info.ContainersPaused)
... ...
@@ -223,3 +241,14 @@ func runInfo(dockerCli *client.DockerCli) error {
223 223
 
224 224
 	return nil
225 225
 }
226
+
227
+func formatInfo(dockerCli *client.DockerCli, info types.Info, format string) error {
228
+	tmpl, err := templates.Parse(format)
229
+	if err != nil {
230
+		return cli.StatusError{StatusCode: 64,
231
+			Status: "Template parsing error: " + err.Error()}
232
+	}
233
+	err = tmpl.Execute(dockerCli.Out(), info)
234
+	dockerCli.Out().Write([]byte{'\n'})
235
+	return err
236
+}
... ...
@@ -1290,9 +1290,15 @@ _docker_import() {
1290 1290
 }
1291 1291
 
1292 1292
 _docker_info() {
1293
+	case "$prev" in
1294
+		--format|-f)
1295
+			return
1296
+			;;
1297
+	esac
1298
+    
1293 1299
 	case "$cur" in
1294 1300
 		-*)
1295
-			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
1301
+			COMPREPLY=( $( compgen -W "--format -f --help" -- "$cur" ) )
1296 1302
 			;;
1297 1303
 	esac
1298 1304
 }
... ...
@@ -200,6 +200,8 @@ complete -c docker -A -f -n '__fish_seen_subcommand_from import' -l help -d 'Pri
200 200
 
201 201
 # info
202 202
 complete -c docker -f -n '__fish_docker_no_subcommand' -a info -d 'Display system-wide information'
203
+complete -c docker -A -f -n '__fish_seen_subcommand_from info' -s f -l format  -d 'Format the output using the given go template'
204
+complete -c docker -A -f -n '__fish_seen_subcommand_from info' -l help -d 'Print usage'
203 205
 
204 206
 # inspect
205 207
 complete -c docker -f -n '__fish_docker_no_subcommand' -a inspect -d 'Return low-level information on a container or image'
... ...
@@ -393,6 +395,8 @@ complete -c docker -A -f -n '__fish_seen_subcommand_from unpause' -a '(__fish_pr
393 393
 
394 394
 # version
395 395
 complete -c docker -f -n '__fish_docker_no_subcommand' -a version -d 'Show the Docker version information'
396
+complete -c docker -A -f -n '__fish_seen_subcommand_from version' -s f -l format  -d 'Format the output using the given go template'
397
+complete -c docker -A -f -n '__fish_seen_subcommand_from version' -l help -d 'Print usage'
396 398
 
397 399
 # wait
398 400
 complete -c docker -f -n '__fish_docker_no_subcommand' -a wait -d 'Block until a container stops, then print its exit code'
... ...
@@ -1709,7 +1709,8 @@ __docker_subcommand() {
1709 1709
             ;;
1710 1710
         (info|version)
1711 1711
             _arguments $(__docker_arguments) \
1712
-                $opts_help && ret=0
1712
+                $opts_help \
1713
+		"($help -f --format)"{-f=,--format=}"[Format the output using the given go template]:template: " && ret=0
1713 1714
             ;;
1714 1715
         (inspect)
1715 1716
             local state
... ...
@@ -16,7 +16,8 @@ Usage:  docker info
16 16
 Display system-wide information
17 17
 
18 18
 Options:
19
-      --help   Print usage
19
+  -f, --format string   Format the output using the given go template
20
+      --help            Print usage
20 21
 ```
21 22
 
22 23
 This command displays system wide information regarding the Docker installation.
... ...
@@ -24,6 +25,10 @@ Information displayed includes the kernel version, number of containers and imag
24 24
 The number of images shown is the number of unique images. The same image tagged
25 25
 under different names is counted only once.
26 26
 
27
+If a format is specified, the given template will be executed instead of the
28
+default format. Go's [text/template](http://golang.org/pkg/text/template/) package
29
+describes all the details of the format.
30
+
27 31
 Depending on the storage driver in use, additional information can be shown, such
28 32
 as pool name, data file, metadata file, data space used, total data space, metadata
29 33
 space used, and total metadata space.
... ...
@@ -144,3 +149,8 @@ information about the devicemapper storage driver is shown:
144 144
     Insecure registries:
145 145
      myinsecurehost:5000
146 146
      127.0.0.0/8
147
+
148
+You can also specify the output format:
149
+
150
+    $ docker info --format '{{json .}}'
151
+	{"ID":"I54V:OLXT:HVMM:TPKO:JPHQ:CQCD:JNLC:O3BZ:4ZVJ:43XJ:PFHZ:6N2S","Containers":14, ...}
... ...
@@ -1,6 +1,7 @@
1 1
 package main
2 2
 
3 3
 import (
4
+	"encoding/json"
4 5
 	"fmt"
5 6
 	"net"
6 7
 	"strings"
... ...
@@ -49,6 +50,17 @@ func (s *DockerSuite) TestInfoEnsureSucceeds(c *check.C) {
49 49
 	}
50 50
 }
51 51
 
52
+// TestInfoFormat tests `docker info --format`
53
+func (s *DockerSuite) TestInfoFormat(c *check.C) {
54
+	out, status := dockerCmd(c, "info", "--format", "{{json .}}")
55
+	c.Assert(status, checker.Equals, 0)
56
+	var m map[string]interface{}
57
+	err := json.Unmarshal([]byte(out), &m)
58
+	c.Assert(err, checker.IsNil)
59
+	_, _, err = dockerCmdWithError("info", "--format", "{{.badString}}")
60
+	c.Assert(err, checker.NotNil)
61
+}
62
+
52 63
 // TestInfoDiscoveryBackend verifies that a daemon run with `--cluster-advertise` and
53 64
 // `--cluster-store` properly show the backend's endpoint in info output.
54 65
 func (s *DockerSuite) TestInfoDiscoveryBackend(c *check.C) {
... ...
@@ -7,7 +7,7 @@ docker-info - Display system-wide information
7 7
 # SYNOPSIS
8 8
 **docker info**
9 9
 [**--help**]
10
-
10
+[**-f**|**--format**[=*FORMAT*]]
11 11
 
12 12
 # DESCRIPTION
13 13
 This command displays system wide information regarding the Docker installation.
... ...
@@ -15,6 +15,10 @@ Information displayed includes the kernel version, number of containers and imag
15 15
 The number of images shown is the number of unique images. The same image tagged
16 16
 under different names is counted only once.
17 17
 
18
+If a format is specified, the given template will be executed instead of the
19
+default format. Go's **text/template** package
20
+describes all the details of the format.
21
+
18 22
 Depending on the storage driver in use, additional information can be shown, such
19 23
 as pool name, data file, metadata file, data space used, total data space, metadata
20 24
 space used, and total metadata space.
... ...
@@ -28,6 +32,9 @@ available on the volume where `/var/lib/docker` is mounted.
28 28
 **--help**
29 29
   Print usage statement
30 30
 
31
+**-f**, **--format**=""
32
+  Format the output using the given go template
33
+
31 34
 # EXAMPLES
32 35
 
33 36
 ## Display Docker system information
... ...
@@ -140,6 +147,11 @@ information about the devicemapper storage driver is shown:
140 140
      myinsecurehost:5000
141 141
      127.0.0.0/8
142 142
 
143
+You can also specify the output format:
144
+
145
+    $ docker info --format '{{json .}}'
146
+	{"ID":"I54V:OLXT:HVMM:TPKO:JPHQ:CQCD:JNLC:O3BZ:4ZVJ:43XJ:PFHZ:6N2S","Containers":14, ...}
147
+
143 148
 # HISTORY
144 149
 April 2014, Originally compiled by William Henry (whenry at redhat dot com)
145 150
 based on docker.com source material and internal work.