Skip to content

Commit

Permalink
feat(vesrion): add json format output for version command
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Nov 30, 2020
1 parent ed4b87e commit ad4dcc7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
20 changes: 19 additions & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package cmd

import (
"encoding/json"
"fmt"

"github.com/qri-io/ioes"
"github.com/qri-io/qri/version"
"github.com/spf13/cobra"
)

// 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",
Expand All @@ -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
}
15 changes: 14 additions & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit ad4dcc7

Please sign in to comment.