Browse Source

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

Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
Akihiro Suda 9 years ago
parent
commit
dc38c9a047

+ 32 - 3
api/client/system/info.go

@@ -11,32 +11,50 @@ import (
 	"github.com/docker/docker/cli"
 	"github.com/docker/docker/cli"
 	"github.com/docker/docker/pkg/ioutils"
 	"github.com/docker/docker/pkg/ioutils"
 	"github.com/docker/docker/utils"
 	"github.com/docker/docker/utils"
+	"github.com/docker/docker/utils/templates"
+	"github.com/docker/engine-api/types"
 	"github.com/docker/engine-api/types/swarm"
 	"github.com/docker/engine-api/types/swarm"
 	"github.com/docker/go-units"
 	"github.com/docker/go-units"
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
 )
 )
 
 
+type infoOptions struct {
+	format string
+}
+
 // NewInfoCommand creates a new cobra.Command for `docker info`
 // NewInfoCommand creates a new cobra.Command for `docker info`
 func NewInfoCommand(dockerCli *client.DockerCli) *cobra.Command {
 func NewInfoCommand(dockerCli *client.DockerCli) *cobra.Command {
+	var opts infoOptions
+
 	cmd := &cobra.Command{
 	cmd := &cobra.Command{
 		Use:   "info",
 		Use:   "info",
 		Short: "Display system-wide information",
 		Short: "Display system-wide information",
 		Args:  cli.NoArgs,
 		Args:  cli.NoArgs,
 		RunE: func(cmd *cobra.Command, args []string) error {
 		RunE: func(cmd *cobra.Command, args []string) error {
-			return runInfo(dockerCli)
+			return runInfo(dockerCli, &opts)
 		},
 		},
 	}
 	}
-	return cmd
 
 
+	flags := cmd.Flags()
+
+	flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given go template")
+
+	return cmd
 }
 }
 
 
-func runInfo(dockerCli *client.DockerCli) error {
+func runInfo(dockerCli *client.DockerCli, opts *infoOptions) error {
 	ctx := context.Background()
 	ctx := context.Background()
 	info, err := dockerCli.Client().Info(ctx)
 	info, err := dockerCli.Client().Info(ctx)
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
+	if opts.format == "" {
+		return prettyPrintInfo(dockerCli, info)
+	}
+	return formatInfo(dockerCli, info, opts.format)
+}
 
 
+func prettyPrintInfo(dockerCli *client.DockerCli, info types.Info) error {
 	fmt.Fprintf(dockerCli.Out(), "Containers: %d\n", info.Containers)
 	fmt.Fprintf(dockerCli.Out(), "Containers: %d\n", info.Containers)
 	fmt.Fprintf(dockerCli.Out(), " Running: %d\n", info.ContainersRunning)
 	fmt.Fprintf(dockerCli.Out(), " Running: %d\n", info.ContainersRunning)
 	fmt.Fprintf(dockerCli.Out(), " Paused: %d\n", info.ContainersPaused)
 	fmt.Fprintf(dockerCli.Out(), " Paused: %d\n", info.ContainersPaused)
@@ -223,3 +241,14 @@ func runInfo(dockerCli *client.DockerCli) error {
 
 
 	return nil
 	return nil
 }
 }
+
+func formatInfo(dockerCli *client.DockerCli, info types.Info, format string) error {
+	tmpl, err := templates.Parse(format)
+	if err != nil {
+		return cli.StatusError{StatusCode: 64,
+			Status: "Template parsing error: " + err.Error()}
+	}
+	err = tmpl.Execute(dockerCli.Out(), info)
+	dockerCli.Out().Write([]byte{'\n'})
+	return err
+}

+ 7 - 1
contrib/completion/bash/docker

@@ -1290,9 +1290,15 @@ _docker_import() {
 }
 }
 
 
 _docker_info() {
 _docker_info() {
+	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" ) )
 			;;
 			;;
 	esac
 	esac
 }
 }

+ 4 - 0
contrib/completion/fish/docker.fish

@@ -200,6 +200,8 @@ complete -c docker -A -f -n '__fish_seen_subcommand_from import' -l help -d 'Pri
 
 
 # info
 # info
 complete -c docker -f -n '__fish_docker_no_subcommand' -a info -d 'Display system-wide information'
 complete -c docker -f -n '__fish_docker_no_subcommand' -a info -d 'Display system-wide information'
+complete -c docker -A -f -n '__fish_seen_subcommand_from info' -s f -l format  -d 'Format the output using the given go template'
+complete -c docker -A -f -n '__fish_seen_subcommand_from info' -l help -d 'Print usage'
 
 
 # inspect
 # inspect
 complete -c docker -f -n '__fish_docker_no_subcommand' -a inspect -d 'Return low-level information on a container or image'
 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
 
 
 # version
 # version
 complete -c docker -f -n '__fish_docker_no_subcommand' -a version -d 'Show the Docker version information'
 complete -c docker -f -n '__fish_docker_no_subcommand' -a version -d 'Show the Docker version information'
+complete -c docker -A -f -n '__fish_seen_subcommand_from version' -s f -l format  -d 'Format the output using the given go template'
+complete -c docker -A -f -n '__fish_seen_subcommand_from version' -l help -d 'Print usage'
 
 
 # wait
 # wait
 complete -c docker -f -n '__fish_docker_no_subcommand' -a wait -d 'Block until a container stops, then print its exit code'
 complete -c docker -f -n '__fish_docker_no_subcommand' -a wait -d 'Block until a container stops, then print its exit code'

+ 2 - 1
contrib/completion/zsh/_docker

@@ -1709,7 +1709,8 @@ __docker_subcommand() {
             ;;
             ;;
         (info|version)
         (info|version)
             _arguments $(__docker_arguments) \
             _arguments $(__docker_arguments) \
-                $opts_help && ret=0
+                $opts_help \
+		"($help -f --format)"{-f=,--format=}"[Format the output using the given go template]:template: " && ret=0
             ;;
             ;;
         (inspect)
         (inspect)
             local state
             local state

+ 11 - 1
docs/reference/commandline/info.md

@@ -16,7 +16,8 @@ Usage:  docker info
 Display system-wide information
 Display system-wide information
 
 
 Options:
 Options:
-      --help   Print usage
+  -f, --format string   Format the output using the given go template
+      --help            Print usage
 ```
 ```
 
 
 This command displays system wide information regarding the Docker installation.
 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
 The number of images shown is the number of unique images. The same image tagged
 The number of images shown is the number of unique images. The same image tagged
 under different names is counted only once.
 under different names is counted only once.
 
 
+If a format is specified, the given template will be executed instead of the
+default format. Go's [text/template](http://golang.org/pkg/text/template/) package
+describes all the details of the format.
+
 Depending on the storage driver in use, additional information can be shown, such
 Depending on the storage driver in use, additional information can be shown, such
 as pool name, data file, metadata file, data space used, total data space, metadata
 as pool name, data file, metadata file, data space used, total data space, metadata
 space used, and total metadata space.
 space used, and total metadata space.
@@ -144,3 +149,8 @@ information about the devicemapper storage driver is shown:
     Insecure registries:
     Insecure registries:
      myinsecurehost:5000
      myinsecurehost:5000
      127.0.0.0/8
      127.0.0.0/8
+
+You can also specify the output format:
+
+    $ docker info --format '{{json .}}'
+	{"ID":"I54V:OLXT:HVMM:TPKO:JPHQ:CQCD:JNLC:O3BZ:4ZVJ:43XJ:PFHZ:6N2S","Containers":14, ...}

+ 12 - 0
integration-cli/docker_cli_info_test.go

@@ -1,6 +1,7 @@
 package main
 package main
 
 
 import (
 import (
+	"encoding/json"
 	"fmt"
 	"fmt"
 	"net"
 	"net"
 	"strings"
 	"strings"
@@ -49,6 +50,17 @@ func (s *DockerSuite) TestInfoEnsureSucceeds(c *check.C) {
 	}
 	}
 }
 }
 
 
+// TestInfoFormat tests `docker info --format`
+func (s *DockerSuite) TestInfoFormat(c *check.C) {
+	out, status := dockerCmd(c, "info", "--format", "{{json .}}")
+	c.Assert(status, checker.Equals, 0)
+	var m map[string]interface{}
+	err := json.Unmarshal([]byte(out), &m)
+	c.Assert(err, checker.IsNil)
+	_, _, err = dockerCmdWithError("info", "--format", "{{.badString}}")
+	c.Assert(err, checker.NotNil)
+}
+
 // TestInfoDiscoveryBackend verifies that a daemon run with `--cluster-advertise` and
 // TestInfoDiscoveryBackend verifies that a daemon run with `--cluster-advertise` and
 // `--cluster-store` properly show the backend's endpoint in info output.
 // `--cluster-store` properly show the backend's endpoint in info output.
 func (s *DockerSuite) TestInfoDiscoveryBackend(c *check.C) {
 func (s *DockerSuite) TestInfoDiscoveryBackend(c *check.C) {

+ 13 - 1
man/docker-info.1.md

@@ -7,7 +7,7 @@ docker-info - Display system-wide information
 # SYNOPSIS
 # SYNOPSIS
 **docker info**
 **docker info**
 [**--help**]
 [**--help**]
-
+[**-f**|**--format**[=*FORMAT*]]
 
 
 # DESCRIPTION
 # DESCRIPTION
 This command displays system wide information regarding the Docker installation.
 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
 The number of images shown is the number of unique images. The same image tagged
 The number of images shown is the number of unique images. The same image tagged
 under different names is counted only once.
 under different names is counted only once.
 
 
+If a format is specified, the given template will be executed instead of the
+default format. Go's **text/template** package
+describes all the details of the format.
+
 Depending on the storage driver in use, additional information can be shown, such
 Depending on the storage driver in use, additional information can be shown, such
 as pool name, data file, metadata file, data space used, total data space, metadata
 as pool name, data file, metadata file, data space used, total data space, metadata
 space used, and total metadata space.
 space used, and total metadata space.
@@ -28,6 +32,9 @@ available on the volume where `/var/lib/docker` is mounted.
 **--help**
 **--help**
   Print usage statement
   Print usage statement
 
 
+**-f**, **--format**=""
+  Format the output using the given go template
+
 # EXAMPLES
 # EXAMPLES
 
 
 ## Display Docker system information
 ## Display Docker system information
@@ -140,6 +147,11 @@ information about the devicemapper storage driver is shown:
      myinsecurehost:5000
      myinsecurehost:5000
      127.0.0.0/8
      127.0.0.0/8
 
 
+You can also specify the output format:
+
+    $ docker info --format '{{json .}}'
+	{"ID":"I54V:OLXT:HVMM:TPKO:JPHQ:CQCD:JNLC:O3BZ:4ZVJ:43XJ:PFHZ:6N2S","Containers":14, ...}
+
 # HISTORY
 # HISTORY
 April 2014, Originally compiled by William Henry (whenry at redhat dot com)
 April 2014, Originally compiled by William Henry (whenry at redhat dot com)
 based on docker.com source material and internal work.
 based on docker.com source material and internal work.