podman version --format ... was not working

This patch fixes the podman --version --format command.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
This commit is contained in:
Daniel J Walsh 2020-05-19 11:00:59 -04:00
parent 17171aecf4
commit 7b188f7b5b
No known key found for this signature in database
GPG Key ID: A2DF901DABE2C028
8 changed files with 46 additions and 33 deletions

View File

@ -7,6 +7,7 @@ import (
"strings"
"text/tabwriter"
"github.com/containers/buildah/pkg/formats"
"github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/libpod/define"
@ -51,6 +52,17 @@ func version(cmd *cobra.Command, args []string) error {
if !strings.HasSuffix(versionFormat, "\n") {
versionFormat += "\n"
}
out := formats.StdoutTemplate{Output: versions, Template: versionFormat}
err := out.Out()
if err != nil {
// On Failure, assume user is using older version of podman version --format and check client
versionFormat = strings.Replace(versionFormat, ".Server.", ".", 1)
out = formats.StdoutTemplate{Output: versions.Client, Template: versionFormat}
if err1 := out.Out(); err1 != nil {
return err
}
}
return nil
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
@ -73,7 +85,7 @@ func version(cmd *cobra.Command, args []string) error {
func formatVersion(writer io.Writer, version *define.Version) {
fmt.Fprintf(writer, "Version:\t%s\n", version.Version)
fmt.Fprintf(writer, "RemoteAPI Version:\t%d\n", version.RemoteAPIVersion)
fmt.Fprintf(writer, "API Version:\t%d\n", version.APIVersion)
fmt.Fprintf(writer, "Go Version:\t%s\n", version.GoVersion)
if version.GitCommit != "" {
fmt.Fprintf(writer, "Git Commit:\t%s\n", version.GitCommit)

View File

@ -121,7 +121,7 @@ version:
GitCommit: c3678ce3289f4195f3f16802411e795c6a587c9f-dirty
GoVersion: go1.14.2
OsArch: linux/amd64
RemoteAPIVersion: 1
APIVersion: 1
Version: 2.0.0
```
Run podman info with JSON formatted response:
@ -228,7 +228,7 @@ Run podman info with JSON formatted response:
]
},
"version": {
"RemoteAPIVersion": 1,
"APIVersion": 1,
"Version": "2.0.0",
"GoVersion": "go1.14.2",
"GitCommit": "c3678ce3289f4195f3f16802411e795c6a587c9f-dirty",

View File

@ -25,17 +25,18 @@ Change output format to "json" or a Go template.
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
Version: 2.0.0
API Version: 1
Go Version: go1.14.2
Git Commit: 4520664f63c3a7f9a80227715359e20069d95542
Built: Tue May 19 10:48:59 2020
OS/Arch: linux/amd64
```
Filtering out only the version:
```
$ podman version --format '{{.Client.Version}}'
1.6.3
2.0.0
```
## SEE ALSO

View File

@ -20,7 +20,7 @@ var (
// Version is an output struct for varlink
type Version struct {
RemoteAPIVersion int64
APIVersion int64
Version string
GoVersion string
GitCommit string
@ -42,7 +42,7 @@ func GetVersion() (Version, error) {
}
}
return Version{
RemoteAPIVersion: podmanVersion.RemoteAPIVersion,
APIVersion: podmanVersion.APIVersion,
Version: podmanVersion.Version,
GoVersion: runtime.Version(),
GitCommit: gitCommit,

View File

@ -112,7 +112,7 @@ func Version(ctx context.Context) (*entities.SystemVersionReport, error) {
f, _ := strconv.ParseFloat(component.APIVersion, 64)
b, _ := time.Parse(time.RFC3339, component.BuildTime)
report.Server = &define.Version{
RemoteAPIVersion: int64(f),
APIVersion: int64(f),
Version: component.Version.Version,
GoVersion: component.GoVersion,
GitCommit: component.GitCommit,

View File

@ -28,7 +28,7 @@ func (i *VarlinkAPI) GetVersion(call iopodman.VarlinkCall) error {
versionInfo.GitCommit,
time.Unix(versionInfo.Built, 0).Format(time.RFC3339),
versionInfo.OsArch,
versionInfo.RemoteAPIVersion,
versionInfo.APIVersion,
)
}

View File

@ -23,7 +23,7 @@ function setup() {
is "${lines[0]}" "Version:[ ]\+[1-9][0-9.]\+" "Version line 1"
is "$output" ".*Go Version: \+" "'Go Version' in output"
is "$output" ".*RemoteAPI Version: \+" "API version in output"
is "$output" ".*API Version: \+" "API version in output"
}

View File

@ -6,7 +6,7 @@ package version
// bumped.
const Version = "2.0.0-dev"
// RemoteAPIVersion is the version for the remote
// APIVersion is the version for the remote
// client API. It is used to determine compatibility
// between a remote podman client and its backend
const RemoteAPIVersion = 1
const APIVersion = 1