diff --git a/.goreleaser.yml b/.goreleaser.yml index e6131acfa..81220eed2 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -1,7 +1,6 @@ version: 2 before: hooks: - - go mod tidy - ./scripts/completions.sh - go run ./scripts manpages @@ -16,6 +15,9 @@ builds: - -w - -X {{ .ModulePath }}/internal/version.version={{ .Version }} - -X {{ .ModulePath }}/internal/version.versionPrerelease={{- if .IsSnapshot -}}dev+{{ .ShortCommit }}{{- end -}} + - -X {{ .ModulePath }}/internal/version.commit={{ .FullCommit }} + - -X {{ .ModulePath }}/internal/version.commitDate={{ .CommitDate }} + - -X {{ .ModulePath }}/internal/version.modified={{ .IsGitDirty }} env: - CGO_ENABLED=0 @@ -110,6 +112,9 @@ kos: - -w - -X {{ .ModulePath }}/internal/version.version={{ .Version }} - -X {{ .ModulePath }}/internal/version.versionPrerelease={{- if .IsSnapshot -}}dev+{{ .ShortCommit }}{{- end -}} + - -X {{ .ModulePath }}/internal/version.commit={{ .FullCommit }} + - -X {{ .ModulePath }}/internal/version.commitDate={{ .CommitDate }} + - -X {{ .ModulePath }}/internal/version.modified={{ .IsGitDirty }} - -X {{ .ModulePath }}/internal/state/config.defaultConfigPathOverride=/config.toml snapshot: diff --git a/internal/cmd/version/version.go b/internal/cmd/version/version.go index 6899565ed..98f8322e7 100644 --- a/internal/cmd/version/version.go +++ b/internal/cmd/version/version.go @@ -3,7 +3,6 @@ package version import ( "fmt" "runtime" - "runtime/debug" "text/tabwriter" "github.com/spf13/cobra" @@ -35,25 +34,14 @@ func runVersion(cmd *cobra.Command, _ []string) error { fmt.Fprintf(tw, "go version:\t%s (%s)\n", runtime.Version(), runtime.Compiler) fmt.Fprintf(tw, "platform:\t%s/%s\n", runtime.GOOS, runtime.GOARCH) - if info, ok := debug.ReadBuildInfo(); ok { - rev := getSettingsValue(info.Settings, "vcs.revision", "unknown") - if modified := getSettingsValue(info.Settings, "vcs.modified", "false"); modified == "true" { - rev += " (modified)" - } - - fmt.Fprintf(tw, "revision:\t%s\n", rev) - fmt.Fprintf(tw, "revision date:\t%s\n", getSettingsValue(info.Settings, "vcs.time", "unknown")) + rev := version.Commit + if version.Modified { + rev += " (modified)" } + + fmt.Fprintf(tw, "revision:\t%s\n", rev) + fmt.Fprintf(tw, "revision date:\t%s\n", version.CommitDate) return tw.Flush() } return nil } - -func getSettingsValue(settings []debug.BuildSetting, key, def string) string { - for _, setting := range settings { - if setting.Key == key { - return setting.Value - } - } - return def -} diff --git a/internal/version/version.go b/internal/version/version.go index 43ae7d36d..6edc914f3 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -1,5 +1,7 @@ package version +import "runtime/debug" + var ( // version is a semver version (https://semver.org). version = "1.62.0" // x-releaser-pleaser-version @@ -16,4 +18,56 @@ var ( } return version }() + + // Can be set by goreleaser because debug.ReadBuildInfo() is not available for goreleaser builds + commit = "" + + // Commit is the latest full commit hash during build time + Commit = func() string { + if commit != "" { + return commit + } + return getSettingsValue("vcs.revision", "unknown") + }() + + // Can be set by goreleaser because debug.ReadBuildInfo() is not available for goreleaser builds + commitDate = "" + + // CommitDate is the timestamp of the latest commit during build time in RFC3339 + CommitDate = func() string { + if commitDate != "" { + return commitDate + } + return getSettingsValue("vcs.time", "unknown") + }() + + // Can be set by goreleaser because debug.ReadBuildInfo() is not available for goreleaser builds + modified = "" + + // Modified specifies whether the git worktree was dirty during build time + Modified = func() bool { + if modified != "" { + return modified == "true" + } + return getSettingsValue("vcs.modified", "false") == "true" + }() + + // used for getSettingsValue + info, ok = debug.ReadBuildInfo() ) + +// getSettingsValue is a helper for getting values from debug.ReadBuildInfo() +// This is only a fallback for builds that do not use goreleaser, since goreleaser +// usually injects the above variables using ldflags. debug.ReadBuildInfo() will be +// used for example when installing using 'go install'. +func getSettingsValue(key, def string) string { + if !ok { + return def + } + for _, setting := range info.Settings { + if setting.Key == key { + return setting.Value + } + } + return def +}