Skip to content

Commit

Permalink
support for json flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Shu Kutsuzawa committed Feb 16, 2021
1 parent 6b81b78 commit c09af64
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ jobs:
with:
version: latest
args: build --snapshot --skip-post-hooks
env:
GOVERSION: ${{ steps.go-version.outputs.content }}

test:
runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -94,7 +96,7 @@ jobs:
-
name: Run tests
run: make test
-
-
name: Upload code coverage report
uses: codecov/codecov-action@v1
with:
Expand Down
32 changes: 26 additions & 6 deletions internal/cmd/version_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,26 @@ import (

type VersionOutput struct {
Version string `json:"version"`

BuildGoVersion string `json:"go_version,omitempty"`
BuildGoOS string `json:"go_os,omitempty"`
BuildGoArch string `json:"go_arch,omitempty"`
}

type VersionCommand struct {
Ui cli.Ui
Version string
Ui cli.Ui
Version string
BuildInfo *BuildInfo

jsonOutput bool
}

type BuildInfo struct {
GoVersion string
GoOS string
GoArch string
}

func (c *VersionCommand) flags() *flag.FlagSet {
fs := defaultFlagSet("version")

Expand All @@ -37,10 +48,14 @@ func (c *VersionCommand) Run(args []string) int {
return 1
}

output := VersionOutput{
Version: c.Version,
BuildGoVersion: c.BuildInfo.GoVersion,
BuildGoOS: c.BuildInfo.GoOS,
BuildGoArch: c.BuildInfo.GoArch,
}

if c.jsonOutput {
output := VersionOutput{
Version: c.Version,
}
jsonOutput, err := json.MarshalIndent(output, "", " ")
if err != nil {
c.Ui.Error(fmt.Sprintf("\nError marshalling JSON: %s", err))
Expand All @@ -50,7 +65,12 @@ func (c *VersionCommand) Run(args []string) int {
return 0
}

c.Ui.Output(string(c.Version))
ver := string(c.Version)
if output.BuildGoVersion != "" && output.BuildGoOS != "" && output.BuildGoArch != "" {
ver = fmt.Sprintf("%s\ngo%s %s/%s", c.Version, output.BuildGoVersion, output.BuildGoOS, output.BuildGoArch)
}

c.Ui.Output(ver)
return 0
}

Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func main() {
return &cmd.VersionCommand{
Ui: ui,
Version: VersionString(),
BuildInfo: &cmd.BuildInfo{
GoVersion: buildGoVersion,
GoOS: buildGoOS,
GoArch: buildGoArch,
},
}, nil
},
}
Expand Down
9 changes: 1 addition & 8 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@ func init() {
// VersionString returns the complete version string, including prerelease
func VersionString() string {
if prerelease != "" {
return buildInfo(fmt.Sprintf("%s-%s", version, prerelease))
}

return buildInfo(version)
}
func buildInfo(version string) string {
if buildGoVersion != "" && buildGoOS != "" && buildGoArch != "" {
return fmt.Sprintf("%s\ngo%s %s/%s", version, buildGoVersion, buildGoOS, buildGoArch)
return fmt.Sprintf("%s-%s", version, prerelease)
}
return version
}

0 comments on commit c09af64

Please sign in to comment.