Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add goversion, goos, and goarch to version command #407

Merged
merged 3 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ jobs:
ARTIFACTORY_TOKEN: ${{ secrets.ARTIFACTORY_TOKEN }}
ARTIFACTORY_USER: ${{ secrets.ARTIFACTORY_USER }}
CIRCLE_TOKEN: ${{ secrets.CIRCLE_TOKEN }}
GOVERSION: ${{ steps.go-version.outputs.content }}
-
name: Publish released artifacts
run: hc-releases publish -product=terraform-ls
Expand Down
4 changes: 2 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ builds:
- -trimpath
- -tags=preloadschema
ldflags:
- '-s -w -X "main.version={{ .RawVersion }}" -X "main.prerelease={{ if .IsSnapshot }}snapshot.{{ .ShortCommit }}{{ else }}{{ .Prerelease }}{{ end }}"'
- '-s -w -X "main.version={{ .RawVersion }}" -X "main.prerelease={{ if .IsSnapshot }}snapshot.{{ .ShortCommit }}{{ else }}{{ .Prerelease }}{{ end }}" -X "main.buildGoVersion={{ .Env.GOVERSION }}" -X "main.buildGoOS={{ .Os }}" -X "main.buildGoArch={{ .Arch }}"'
goarch:
- '386'
- amd64
Expand All @@ -39,7 +39,7 @@ builds:
- -trimpath
- -tags=preloadschema
ldflags:
- '-s -w -X "main.version={{ .RawVersion }}" -X "main.prerelease={{ if .IsSnapshot }}snapshot.{{ .ShortCommit }}{{ else }}{{ .Prerelease }}{{ end }}"'
- '-s -w -X "main.version={{ .RawVersion }}" -X "main.prerelease={{ if .IsSnapshot }}snapshot.{{ .ShortCommit }}{{ else }}{{ .Prerelease }}{{ end }}" -X "main.buildGoVersion={{ .Env.GOVERSION }}" -X "main.buildGoOS={{ .Os }}" -X "main.buildGoArch={{ .Arch }}"'
cappyzawa marked this conversation as resolved.
Show resolved Hide resolved
goarch:
- '386'
- amd64
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"`
cappyzawa marked this conversation as resolved.
Show resolved Hide resolved
}

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)
cappyzawa marked this conversation as resolved.
Show resolved Hide resolved
}

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{
cappyzawa marked this conversation as resolved.
Show resolved Hide resolved
GoVersion: buildGoVersion,
GoOS: buildGoOS,
GoArch: buildGoArch,
},
}, nil
},
}
Expand Down
6 changes: 6 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ var version = "0.0.0"
// such as "dev" (in development), "beta", "rc1", etc.
var prerelease = "dev"

var (
buildGoVersion = ""
buildGoOS = ""
buildGoArch = ""
)

func init() {
// Verify that the version is proper semantic version, which should always be the case.
_, err := goversion.NewVersion(version)
Expand Down