From 6b81b783bb203f4466c8251695f719b5d299503e Mon Sep 17 00:00:00 2001 From: Shu Kutsuzawa Date: Tue, 16 Feb 2021 19:42:14 +0900 Subject: [PATCH 1/3] add goversion, goos and goarch for build --- .github/workflows/release.yml | 1 + .goreleaser.yml | 4 ++-- version.go | 15 ++++++++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a655e469..b8c3fa49 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 diff --git a/.goreleaser.yml b/.goreleaser.yml index ac5f0a2a..964c1b1f 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -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 @@ -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 }}"' goarch: - '386' - amd64 diff --git a/version.go b/version.go index 347c4173..d33e8b68 100644 --- a/version.go +++ b/version.go @@ -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) @@ -25,7 +31,14 @@ func init() { // VersionString returns the complete version string, including prerelease func VersionString() string { if prerelease != "" { - return fmt.Sprintf("%s-%s", version, 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 version } From c09af642bae9ec1b7804696caffda2c5930bfbf8 Mon Sep 17 00:00:00 2001 From: Shu Kutsuzawa Date: Tue, 16 Feb 2021 22:10:15 +0900 Subject: [PATCH 2/3] support for json flag --- .github/workflows/ci.yml | 4 +++- internal/cmd/version_command.go | 32 ++++++++++++++++++++++++++------ main.go | 5 +++++ version.go | 9 +-------- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bfeaf69f..7011fd80 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 }} @@ -94,7 +96,7 @@ jobs: - name: Run tests run: make test - - + - name: Upload code coverage report uses: codecov/codecov-action@v1 with: diff --git a/internal/cmd/version_command.go b/internal/cmd/version_command.go index 60145aa8..03b730df 100644 --- a/internal/cmd/version_command.go +++ b/internal/cmd/version_command.go @@ -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") @@ -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)) @@ -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 } diff --git a/main.go b/main.go index 430365d5..803317e7 100644 --- a/main.go +++ b/main.go @@ -47,6 +47,11 @@ func main() { return &cmd.VersionCommand{ Ui: ui, Version: VersionString(), + BuildInfo: &cmd.BuildInfo{ + GoVersion: buildGoVersion, + GoOS: buildGoOS, + GoArch: buildGoArch, + }, }, nil }, } diff --git a/version.go b/version.go index d33e8b68..da7eee90 100644 --- a/version.go +++ b/version.go @@ -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 } From 27d868b077a2e6c020a205445722084b2c9457f3 Mon Sep 17 00:00:00 2001 From: Shu Kutsuzawa Date: Wed, 17 Feb 2021 01:36:56 +0900 Subject: [PATCH 3/3] fix based on https://github.com/hashicorp/terraform-ls/pull/407#pullrequestreview-591248388 --- .github/workflows/ci.yml | 2 -- .github/workflows/release.yml | 1 - .goreleaser.yml | 4 ++-- internal/cmd/version_command.go | 34 ++++++++++++++++----------------- main.go | 5 ----- version.go | 6 ------ 6 files changed, 18 insertions(+), 34 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7011fd80..41c6c6ed 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -45,8 +45,6 @@ jobs: with: version: latest args: build --snapshot --skip-post-hooks - env: - GOVERSION: ${{ steps.go-version.outputs.content }} test: runs-on: ${{ matrix.os }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b8c3fa49..a655e469 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -76,7 +76,6 @@ 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 diff --git a/.goreleaser.yml b/.goreleaser.yml index 964c1b1f..ac5f0a2a 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -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 }}" -X "main.buildGoVersion={{ .Env.GOVERSION }}" -X "main.buildGoOS={{ .Os }}" -X "main.buildGoArch={{ .Arch }}"' + - '-s -w -X "main.version={{ .RawVersion }}" -X "main.prerelease={{ if .IsSnapshot }}snapshot.{{ .ShortCommit }}{{ else }}{{ .Prerelease }}{{ end }}"' goarch: - '386' - amd64 @@ -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 }}" -X "main.buildGoVersion={{ .Env.GOVERSION }}" -X "main.buildGoOS={{ .Os }}" -X "main.buildGoArch={{ .Arch }}"' + - '-s -w -X "main.version={{ .RawVersion }}" -X "main.prerelease={{ if .IsSnapshot }}snapshot.{{ .ShortCommit }}{{ else }}{{ .Prerelease }}{{ end }}"' goarch: - '386' - amd64 diff --git a/internal/cmd/version_command.go b/internal/cmd/version_command.go index 03b730df..0ccc304a 100644 --- a/internal/cmd/version_command.go +++ b/internal/cmd/version_command.go @@ -4,6 +4,7 @@ import ( "encoding/json" "flag" "fmt" + "runtime" "strings" "github.com/mitchellh/cli" @@ -12,23 +13,21 @@ 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"` + *BuildInfo } type VersionCommand struct { - Ui cli.Ui - Version string - BuildInfo *BuildInfo + Ui cli.Ui + Version string jsonOutput bool } type BuildInfo struct { - GoVersion string - GoOS string - GoArch string + 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 { @@ -49,10 +48,13 @@ func (c *VersionCommand) Run(args []string) int { } output := VersionOutput{ - Version: c.Version, - BuildGoVersion: c.BuildInfo.GoVersion, - BuildGoOS: c.BuildInfo.GoOS, - BuildGoArch: c.BuildInfo.GoArch, + Version: c.Version, + BuildInfo: &BuildInfo{ + GoVersion: runtime.Version(), + GoOS: runtime.GOOS, + GoArch: runtime.GOARCH, + Compiler: runtime.Compiler, + }, } if c.jsonOutput { @@ -65,11 +67,7 @@ func (c *VersionCommand) Run(args []string) int { return 0 } - 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) - } - + 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 } diff --git a/main.go b/main.go index 803317e7..430365d5 100644 --- a/main.go +++ b/main.go @@ -47,11 +47,6 @@ func main() { return &cmd.VersionCommand{ Ui: ui, Version: VersionString(), - BuildInfo: &cmd.BuildInfo{ - GoVersion: buildGoVersion, - GoOS: buildGoOS, - GoArch: buildGoArch, - }, }, nil }, } diff --git a/version.go b/version.go index da7eee90..347c4173 100644 --- a/version.go +++ b/version.go @@ -14,12 +14,6 @@ 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)