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.
sdk_bump
Lucas Bajolet 2 years ago committed by Wilken Rivera
parent 27538f377b
commit 93bec31ec9

@ -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

@ -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

Loading…
Cancel
Save