unify version information

Instead of having two different places where we keep the current version
which must be manually kept in sync, let's use the same one that the
release process uses (version/VERSION).

Local builds will remain tagged with -dev by default, and we'll
disable this behavior with a linker flag at release time.
This commit is contained in:
CJ Horton 2023-04-03 20:38:30 -07:00
parent 7d1e918a38
commit 49e308e57b
2 changed files with 56 additions and 10 deletions

View File

@ -8,26 +8,45 @@
package version
import (
_ "embed"
"fmt"
"strings"
version "github.com/hashicorp/go-version"
)
// The main version number that is being run at the moment.
var Version = "1.6.0"
// rawVersion is the current version as a string, as read from the VERSION
// file. This must be a valid semantic version.
//
//go:embed VERSION
var rawVersion 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.
var Prerelease = "dev"
// dev determines whether the -dev prerelease marker will
// be included in version info. It is expected to be set to "no" using
// linker flags when building binaries for release.
var dev string = "yes"
// SemVer is an instance of version.Version. This has the secondary
// benefit of verifying during tests and init time that our version is a
// proper semantic version, which should always be the case.
// The main version number that is being run at the moment, populated from the raw version.
var Version string
// A pre-release marker for the version, populated using a combination of the raw version
// and the dev flag.
var Prerelease string
// SemVer is an instance of version.Version representing the main version
// without any prerelease information.
var SemVer *version.Version
func init() {
SemVer = version.Must(version.NewVersion(Version))
semVerFull := version.Must(version.NewVersion(strings.TrimSpace(rawVersion)))
SemVer = semVerFull.Core()
Version = SemVer.String()
if dev == "no" {
Prerelease = semVerFull.Prerelease()
} else {
Prerelease = "dev"
}
}
// Header is the header name used to send the current terraform version

27
version/version_test.go Normal file
View File

@ -0,0 +1,27 @@
package version
import (
"regexp"
"strings"
"testing"
)
// Smoke test to validate that the version file can be read correctly and all exported
// variables include the expected information.
func TestVersion(t *testing.T) {
if match, _ := regexp.MatchString("[^\\d+\\.]", Version); match != false {
t.Fatalf("Version should contain only the main version")
}
if match, _ := regexp.MatchString("[^a-z\\d]", Prerelease); match != false {
t.Fatalf("Prerelease should contain only letters and numbers")
}
if SemVer.Prerelease() != "" {
t.Fatalf("SemVer should not include prerelease information")
}
if !strings.Contains(String(), Prerelease) {
t.Fatalf("Full version string should include prerelease information")
}
}