From 43c20d02006fb83d05414a2f868b1c0a4dd4346b Mon Sep 17 00:00:00 2001
From: Tomas Tomecek <ttomecek@redhat.com>
Date: Fri, 23 Nov 2018 22:25:45 +0100
Subject: [PATCH] implement --format for version command

Signed-off-by: Tomas Tomecek <ttomecek@redhat.com>
---
 cmd/podman/version.go    | 33 ++++++++++++++++++++++++++++-----
 completions/bash/podman  | 15 ++++++++++-----
 docs/podman-version.1.md | 23 +++++++++++++++++++++++
 3 files changed, 61 insertions(+), 10 deletions(-)

diff --git a/cmd/podman/version.go b/cmd/podman/version.go
index d80f24a14c..d81deb6960 100644
--- a/cmd/podman/version.go
+++ b/cmd/podman/version.go
@@ -4,6 +4,7 @@ import (
 	"fmt"
 	"time"
 
+	"github.com/containers/libpod/cmd/podman/formats"
 	"github.com/containers/libpod/libpod"
 	"github.com/pkg/errors"
 	"github.com/urfave/cli"
@@ -15,6 +16,19 @@ func versionCmd(c *cli.Context) error {
 	if err != nil {
 		errors.Wrapf(err, "unable to determine version")
 	}
+
+	versionOutputFormat := c.String("format")
+	if versionOutputFormat != "" {
+		var out formats.Writer
+		switch versionOutputFormat {
+		case formats.JSONString:
+			out = formats.JSONStruct{Output: output}
+		default:
+			out = formats.StdoutTemplate{Output: output, Template: versionOutputFormat}
+		}
+		formats.Writer(out).Out()
+		return nil
+	}
 	fmt.Println("Version:      ", output.Version)
 	fmt.Println("Go Version:   ", output.GoVersion)
 	if output.GitCommit != "" {
@@ -30,8 +44,17 @@ func versionCmd(c *cli.Context) error {
 }
 
 // Cli command to print out the full version of podman
-var versionCommand = cli.Command{
-	Name:   "version",
-	Usage:  "Display the PODMAN Version Information",
-	Action: versionCmd,
-}
+var (
+	versionCommand = cli.Command{
+		Name:   "version",
+		Usage:  "Display the Podman Version Information",
+		Action: versionCmd,
+		Flags:  versionFlags,
+	}
+	versionFlags = []cli.Flag{
+		cli.StringFlag{
+			Name:  "format",
+			Usage: "Change the output format to JSON or a Go template",
+		},
+	}
+)
diff --git a/completions/bash/podman b/completions/bash/podman
index 222511a3c8..8eaa1e6a9e 100644
--- a/completions/bash/podman
+++ b/completions/bash/podman
@@ -1906,11 +1906,16 @@ _podman_top() {
 }
 
 _podman_version() {
-     local options_with_args="
-     "
-     local boolean_options="
-     "
-     _complete_ "$options_with_args" "$boolean_options"
+    local boolean_options="
+     --help
+     -h
+    "
+    local options_with_args="
+     --format
+    "
+    local all_options="$options_with_args $boolean_options"
+
+    _complete_ "$options_with_args" "$boolean_options"
 }
 
 _podman_save() {
diff --git a/docs/podman-version.1.md b/docs/podman-version.1.md
index 0c9b9ceed5..749a33afdc 100644
--- a/docs/podman-version.1.md
+++ b/docs/podman-version.1.md
@@ -16,8 +16,31 @@ OS, and Architecture.
 
 Print usage statement
 
+**--format**
+
+Change output format to "json" or a Go template.
+
+## Example
+
+A sample output of the `version` command:
+```
+$ podman version
+Version:       0.11.1
+Go Version:    go1.11
+Git Commit:    "8967a1d691ed44896b81ad48c863033f23c65eb0-dirty"
+Built:         Thu Nov  8 22:35:40 2018
+OS/Arch:       linux/amd64
+```
+
+Filtering out only the version:
+```
+$ podman version --format '{{.Version}}'
+0.11.2
+```
+
 ## SEE ALSO
 podman(1), crio(8)
 
 ## HISTORY
+November 2018, Added --format flag by Tomas Tomecek <ttomecek@redhat.com>
 July 2017, Originally compiled by Urvashi Mohnani <umohnani@redhat.com>