diff --git a/cmd/version.go b/cmd/version.go index 5bde0c673..b64dae77e 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -1,6 +1,9 @@ package cmd import ( + "encoding/json" + "fmt" + "github.com/qri-io/ioes" "github.com/qri-io/qri/version" "github.com/spf13/cobra" @@ -8,6 +11,8 @@ import ( // NewVersionCommand creates a new `qri version` cobra command that prints the current qri version func NewVersionCommand(_ Factory, ioStreams ioes.IOStreams) *cobra.Command { + var format string + cmd := &cobra.Command{ Use: "version", Short: "print the version number", @@ -19,9 +24,22 @@ For updates & further information check https://github.com/qri-io/qri/releases`, }, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - printInfo(ioStreams.Out, version.Summary()) + switch format { + case "json": + data, err := json.Marshal(version.Map()) + if err != nil { + return err + } + printInfo(ioStreams.Out, string(data)) + case "pretty": + printInfo(ioStreams.Out, version.Summary()) + default: + return fmt.Errorf("unrecognized output format: %q", format) + } return nil }, } + + cmd.Flags().StringVar(&format, "format", "pretty", "output format. One of (pretty|json)") return cmd } diff --git a/version/version.go b/version/version.go index f38fba7d3..c18a6abda 100644 --- a/version/version.go +++ b/version/version.go @@ -19,10 +19,23 @@ var ( GolangVersion = "n/a" ) +// Map returns a summary of build info as a string map +func Map() map[string]string { + return map[string]string{ + "version": Version, + "gitCommit": GitCommit, + "gitBranch": GitBranch, + "gitState": GitState, + "gitSummary": GitSummary, + "buildDate": BuildDate, + "golangVersion": GolangVersion, + } +} + // Summary prints a summary of all build info. func Summary() string { return fmt.Sprintf( - "version:\t%s\n\nbuild date:\t%s\ngit summary:\t%s\ngit branch:\t%s\ngit commit:\t%s\ngit state:\t%s\ngolang version:\t%s", + "version:\t%s\nbuild date:\t%s\ngit summary:\t%s\ngit branch:\t%s\ngit commit:\t%s\ngit state:\t%s\ngolang version:\t%s", Version, BuildDate, GitSummary,