Merge pull request #171 from anchore/version-output-json

Add support for JSON version output
This commit is contained in:
Sam Dacanay 2020-09-14 13:51:59 -07:00 committed by GitHub
commit 80ee18d20b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 19 deletions

View File

@ -1,14 +1,19 @@
package cmd package cmd
import ( import (
"encoding/json"
"fmt" "fmt"
"os"
"github.com/anchore/syft/internal" "github.com/anchore/syft/internal"
"github.com/anchore/syft/internal/version" "github.com/anchore/syft/internal/version"
"github.com/anchore/syft/syft/presenter"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var showVerboseVersionInfo bool var showVerboseVersionInfo bool
var outputFormat string
var versionCmd = &cobra.Command{ var versionCmd = &cobra.Command{
Use: "version", Use: "version",
@ -18,12 +23,15 @@ var versionCmd = &cobra.Command{
func init() { func init() {
versionCmd.Flags().BoolVarP(&showVerboseVersionInfo, "verbose", "v", false, "show additional version information") versionCmd.Flags().BoolVarP(&showVerboseVersionInfo, "verbose", "v", false, "show additional version information")
versionCmd.Flags().StringVarP(&outputFormat, "output", "o", string(presenter.TextPresenter), "format to show version information (available=[text, json])")
rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(versionCmd)
} }
func printVersion(_ *cobra.Command, _ []string) { func printVersion(_ *cobra.Command, _ []string) {
versionInfo := version.FromBuild() versionInfo := version.FromBuild()
switch outputFormat {
case "text":
if showVerboseVersionInfo { if showVerboseVersionInfo {
fmt.Println("Application: ", internal.ApplicationName) fmt.Println("Application: ", internal.ApplicationName)
fmt.Println("Version: ", versionInfo.Version) fmt.Println("Version: ", versionInfo.Version)
@ -36,4 +44,23 @@ func printVersion(_ *cobra.Command, _ []string) {
} else { } else {
fmt.Printf("%s %s\n", internal.ApplicationName, versionInfo.Version) 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"`
}{
Version: versionInfo,
Application: internal.ApplicationName,
})
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)
}
} }

View File

@ -16,13 +16,13 @@ var platform = fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
// Version defines the application version details (generally from build information) // Version defines the application version details (generally from build information)
type Version struct { type Version struct {
Version string // application semantic version Version string `json:"version"` // application semantic version
GitCommit string // git SHA at build-time GitCommit string `json:"gitCommit"` // git SHA at build-time
GitTreeState string // indication of git tree (either "clean" or "dirty") at build-time GitTreeState string `json:"gitTreeState"` // indication of git tree (either "clean" or "dirty") at build-time
BuildDate string // date of the build BuildDate string `json:"buildDate"` // date of the build
GoVersion string // go runtime version at build-time GoVersion string `json:"goVersion"` // go runtime version at build-time
Compiler string // compiler used at build-time Compiler string `json:"compiler"` // compiler used at build-time
Platform string // GOOS and GOARCH at build-time Platform string `json:"platform"` // GOOS and GOARCH at build-time
} }
// FromBuild provides all version details // FromBuild provides all version details