Skip to content

Commit

Permalink
simplify version cmd + add json option (#139)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
  • Loading branch information
wagoodman authored Aug 25, 2020
1 parent be6a7ea commit 651751f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
32 changes: 27 additions & 5 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package cmd

import (
"encoding/json"
"fmt"
"os"

v1 "github.com/anchore/grype-db/pkg/db/v1"
"github.com/anchore/grype/internal"
"github.com/anchore/grype/internal/version"
"github.com/spf13/cobra"
)

var showVerboseVersionInfo bool
var outputFormat string

var versionCmd = &cobra.Command{
Use: "version",
Expand All @@ -18,14 +20,15 @@ var versionCmd = &cobra.Command{
}

func init() {
versionCmd.Flags().BoolVarP(&showVerboseVersionInfo, "verbose", "v", false, "show additional version information")
versionCmd.Flags().StringVarP(&outputFormat, "output", "o", "text", "format to show version information (available=[text, json])")

rootCmd.AddCommand(versionCmd)
}

func printVersion(_ *cobra.Command, _ []string) {
versionInfo := version.FromBuild()
if showVerboseVersionInfo {
switch outputFormat {
case "text":
fmt.Println("Application: ", internal.ApplicationName)
fmt.Println("Version: ", versionInfo.Version)
fmt.Println("BuildDate: ", versionInfo.BuildDate)
Expand All @@ -35,7 +38,26 @@ func printVersion(_ *cobra.Command, _ []string) {
fmt.Println("GoVersion: ", versionInfo.GoVersion)
fmt.Println("Compiler: ", versionInfo.Compiler)
fmt.Println("Supported DB Schema: ", v1.SchemaVersion)
} else {
fmt.Printf("%s %s\n", internal.ApplicationName, versionInfo.Version)
case "json":

enc := json.NewEncoder(os.Stdout)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
err := enc.Encode(&struct {
version.Version
Application string `json:"application"`
SchemaVersion int `json:"supported-db-schema"`
}{
Version: versionInfo,
Application: internal.ApplicationName,
SchemaVersion: v1.SchemaVersion,
})
if err != nil {
fmt.Printf("failed to show version information: %+v\n", err)
os.Exit(1)
}
default:
fmt.Printf("unsupported output format: %s\n", outputFormat)
os.Exit(1)
}
}
14 changes: 7 additions & 7 deletions internal/version/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ var buildDate = valueNotProvided
var platform = fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)

type Version struct {
Version string
GitCommit string
GitTreeState string
BuildDate string
GoVersion string
Compiler string
Platform string
Version string `json:"version"`
GitCommit string `json:"git-commit"`
GitTreeState string `json:"git-tree-state"`
BuildDate string `json:"build-date"`
GoVersion string `json:"go-version"`
Compiler string `json:"compiler"`
Platform string `json:"platform"`
}

func FromBuild() Version {
Expand Down

0 comments on commit 651751f

Please sign in to comment.