Skip to content

Commit

Permalink
Add goversion, goos, and goarch to version command (hashicorp#407)
Browse files Browse the repository at this point in the history
* add goversion, goos and goarch for build

* support for json flag

* fix based on hashicorp#407 (review)
  • Loading branch information
cappyzawa authored Mar 5, 2021
1 parent 87dba07 commit 32aef11
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ jobs:
-
name: Run tests
run: make test
-
-
name: Upload code coverage report
uses: codecov/codecov-action@v1
with:
Expand Down
26 changes: 22 additions & 4 deletions internal/cmd/version_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"encoding/json"
"flag"
"fmt"
"runtime"
"strings"

"github.com/mitchellh/cli"
)

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

*BuildInfo
}

type VersionCommand struct {
Expand All @@ -20,6 +23,13 @@ type VersionCommand struct {
jsonOutput bool
}

type BuildInfo struct {
GoVersion string `json:"go,omitempty"`
GoOS string `json:"os,omitempty"`
GoArch string `json:"arch,omitempty"`
Compiler string `json:"compiler,omitempty"`
}

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

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

output := VersionOutput{
Version: c.Version,
BuildInfo: &BuildInfo{
GoVersion: runtime.Version(),
GoOS: runtime.GOOS,
GoArch: runtime.GOARCH,
Compiler: runtime.Compiler,
},
}

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 +67,8 @@ func (c *VersionCommand) Run(args []string) int {
return 0
}

c.Ui.Output(string(c.Version))
ver := fmt.Sprintf("%s\nplatform: %s/%s\ngo: %s\ncompiler: %s", c.Version, output.GoOS, output.GoArch, output.GoVersion, output.Compiler)
c.Ui.Output(ver)
return 0
}

Expand Down

0 comments on commit 32aef11

Please sign in to comment.