From 93bec31ec95205f8f8f11795843a68a089d49a28 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Mon, 15 Apr 2024 15:41:26 -0400 Subject: [PATCH] version: embed version/VERSION as version string Since we have both version/version.go and version/VERSION to specify version strings, both are a bit redundant. As version/VERSION is supposed to be the source of truth now, we are using it to derive the version informaiton we used to rely on in Packer and its subcommands. Note: doing this prevents us from changing the version/prerelease through ldflags though as we derive Version/VersionPrerelease from the rawVersion variable. --- hcl2template/parser.go | 2 +- version/version.go | 38 +++++++++++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/hcl2template/parser.go b/hcl2template/parser.go index c372a7733..a1d23f640 100644 --- a/hcl2template/parser.go +++ b/hcl2template/parser.go @@ -157,7 +157,7 @@ func (p *Parser) Parse(filename string, varFiles []string, argVars map[string]st // Before we go further, we'll check to make sure this version can read // all files, so we can produce a version-related error message rather than // potentially-confusing downstream errors. - versionDiags := cfg.CheckCoreVersionRequirements(p.CorePackerVersion) + versionDiags := cfg.CheckCoreVersionRequirements(p.CorePackerVersion.Core()) diags = append(diags, versionDiags...) if versionDiags.HasErrors() { return cfg, diags diff --git a/version/version.go b/version/version.go index a7eec6fd4..418d7ea4b 100644 --- a/version/version.go +++ b/version/version.go @@ -4,6 +4,10 @@ package version import ( + _ "embed" + "fmt" + "strings" + "github.com/hashicorp/go-version" pluginVersion "github.com/hashicorp/packer-plugin-sdk/version" ) @@ -16,21 +20,24 @@ var ( // Whether cgo is enabled or not; set at build time CgoEnabled bool + //go:embed VERSION + rawVersion string + // The next version number that will be released. This will be updated after every release // Version must conform to the format expected by github.com/hashicorp/go-version // for tests to work. // A pre-release marker for the version can also be specified (e.g -dev). If this is omitted // The main version number that is being run at the moment. - - Version = "1.11.0" - + Version string // A pre-release marker for the version. If this is "" (empty string) // then it means that it is a final release. Otherwise, this is a pre-release // such as "dev" (in development), "beta", "rc1", etc. - - VersionPrerelease = "dev" - - VersionMetadata = "" + VersionPrerelease string + // VersionMetadata may be added to give more non-normalised information on a build + // like a commit SHA for example. + // + // Ex: 1.0.0-dev+metadata + VersionMetadata string ) var PackerVersion *pluginVersion.PluginVersion @@ -45,8 +52,21 @@ func FormattedVersion() string { var SemVer *version.Version func init() { - PackerVersion = pluginVersion.InitializePluginVersion(Version, VersionPrerelease) - SemVer = PackerVersion.SemVer() + var err error + + // Note: we use strings.TrimSpace on the version read from version/VERSION + // as it could have trailing whitespaces that must not be part of the + // version string, otherwise version.NewSemver will reject it. + SemVer, err = version.NewSemver(strings.TrimSpace(rawVersion)) + if err != nil { + panic(fmt.Sprintf("Invalid semver version specified in 'version/VERSION' (%q): %s", rawVersion, err)) + } + + Version = SemVer.Core().String() + VersionPrerelease = SemVer.Prerelease() + VersionMetadata = SemVer.Metadata() + + PackerVersion = pluginVersion.InitializePluginVersion(SemVer.Core().String(), SemVer.Prerelease()) } // String returns the complete version string, including prerelease