diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 509d584a4b3..0377ccf7bac 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -24,7 +24,7 @@ jobs: - name: Install tools if: matrix.os == 'ubuntu-latest' run: | - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.23.8 + curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.24.0 cd $(mktemp -d) go mod init tmp go get mvdan.cc/gofumpt/gofumports @@ -61,6 +61,11 @@ jobs: sudo snap install goreleaser --classic sudo snap install snapcraft --classic goreleaser release --skip-publish --snapshot + # verify that version information is embedded correctly + ./dist/chezmoi-cgo_linux_amd64/chezmoi --version | tee /dev/stderr | grep -q "chezmoi version v" + ./dist/chezmoi-cgo_linux_amd64/chezmoi --version | tee /dev/stderr | grep -q "chezmoi version v" + ./dist/chezmoi-nocgo_linux_386/chezmoi --version | tee /dev/stderr | grep -q "chezmoi version v" + ./dist/chezmoi-nocgo-snap_linux_386/chezmoi --version | tee /dev/stderr | grep -q "chezmoi version v" release: if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') needs: diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 4b7ad5e6375..3c1a3973525 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -7,7 +7,6 @@ before: builds: - id: chezmoi-cgo binary: chezmoi - ldflags: "-s -w -X github.com/twpayne/chezmoi/cmd.VersionStr={{ .Version }} -X github.com/twpayne/chezmoi/cmd.Commit={{ .Commit }} -X github.com/twpayne/chezmoi/cmd.Date={{ .Date }}" env: - CGO_ENABLED=1 goos: @@ -18,7 +17,6 @@ builds: binary: chezmoi env: - CGO_ENABLED=0 - ldflags: "-s -w -X github.com/twpayne/chezmoi/cmd.VersionStr={{ .Version }} -X github.com/twpayne/chezmoi/cmd.Commit={{ .Commit }} -X github.com/twpayne/chezmoi/cmd.Date={{ .Date }}" goos: - linux - darwin @@ -43,7 +41,6 @@ builds: binary: chezmoi flags: - -tags=snap - ldflags: "-s -w -X github.com/twpayne/chezmoi/cmd.VersionStr={{ .Version }} -X github.com/twpayne/chezmoi/cmd.Commit={{ .Commit }} -X github.com/twpayne/chezmoi/cmd.Date={{ .Date }}" env: - CGO_ENABLED=1 goos: @@ -54,7 +51,6 @@ builds: binary: chezmoi flags: - -tags=snap - ldflags: "-s -w -X github.com/twpayne/chezmoi/cmd.VersionStr={{ .Version }} -X github.com/twpayne/chezmoi/cmd.Commit={{ .Commit }} -X github.com/twpayne/chezmoi/cmd.Date={{ .Date }}" env: - CGO_ENABLED=0 goos: diff --git a/Makefile b/Makefile index bebb5ef6b30..d8615ef382f 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ generate: .PHONY: install-tools install-tools: - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- v1.23.8 + curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- v1.24.0 ( cd $$(mktemp -d) && go mod init tmp && go get mvdan.cc/gofumpt/gofumports ) .PHONY: lint diff --git a/cmd/doctor.go b/cmd/doctor.go index 0cc82da36c5..720980bc4ae 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -430,7 +430,7 @@ func (c *doctorSuspiciousFilesCheck) Skip() bool { } func (doctorVersionCheck) Check() (bool, error) { - if VersionStr == devVersionStr || Commit == unknownStr || Date == unknownStr { + if VersionStr == "" || Commit == "" || Date == "" { return false, nil } return true, nil diff --git a/cmd/root.go b/cmd/root.go index 553fdabffac..370fd85de0f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -18,12 +18,11 @@ var config = newConfig() // Version information. var ( - devVersionStr = "dev" - unknownStr = "unknown" - VersionStr = devVersionStr - Commit = unknownStr - Date = unknownStr - Version *semver.Version + VersionStr string + Commit string + Date string + BuiltBy string + Version *semver.Version ) var rootCmd = &cobra.Command{ @@ -35,23 +34,6 @@ var rootCmd = &cobra.Command{ } func init() { - if VersionStr != devVersionStr { - var err error - Version, err = semver.NewVersion(strings.TrimPrefix(VersionStr, "v")) - if err != nil { - printErrorAndExit(err) - } - } - - versionComponents := []string{VersionStr} - if Commit != unknownStr { - versionComponents = append(versionComponents, "commit "+Commit) - } - if Date != unknownStr { - versionComponents = append(versionComponents, "built at "+Date) - } - rootCmd.Version = strings.Join(versionComponents, ", ") - homeDir, err := os.UserHomeDir() if err != nil { printErrorAndExit(err) @@ -114,6 +96,28 @@ func init() { // Execute executes the root command. func Execute() { + var versionComponents []string + if VersionStr != "" { + var err error + Version, err = semver.NewVersion(strings.TrimPrefix(VersionStr, "v")) + if err != nil { + printErrorAndExit(err) + } + versionComponents = append(versionComponents, VersionStr) + } else { + versionComponents = append(versionComponents, "dev") + } + if Commit != "" { + versionComponents = append(versionComponents, "commit "+Commit) + } + if Date != "" { + versionComponents = append(versionComponents, "built at "+Date) + } + if BuiltBy != "" { + versionComponents = append(versionComponents, "built by "+BuiltBy) + } + rootCmd.Version = strings.Join(versionComponents, ", ") + if err := rootCmd.Execute(); err != nil { printErrorAndExit(err) } diff --git a/cmd/upgrade.go b/cmd/upgrade.go index e1c01baa506..94a98f10cac 100644 --- a/cmd/upgrade.go +++ b/cmd/upgrade.go @@ -122,7 +122,7 @@ func (c *Config) runUpgradeCmd(cmd *cobra.Command, args []string) error { // If the upgrade is not forced and we're not a dev version, stop if we're // already the latest version. - if !c.upgrade.force && VersionStr != devVersionStr { + if !c.upgrade.force && VersionStr != "" { if !Version.LessThan(*releaseVersion) { fmt.Fprintf(c.Stdout, "chezmoi: already at the latest version (%s)\n", Version) return nil diff --git a/main.go b/main.go index ca8623284d5..ca17fcfb7d5 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,17 @@ package main import "github.com/twpayne/chezmoi/cmd" +var ( + version = "" + commit = "" + date = "" + builtBy = "" +) + func main() { + cmd.VersionStr = version + cmd.Commit = commit + cmd.Date = date + cmd.BuiltBy = builtBy cmd.Execute() }