Skip to content

Commit

Permalink
always read the version from the file
Browse files Browse the repository at this point in the history
  • Loading branch information
radditude committed Apr 4, 2023
1 parent b8985e5 commit 891133f
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,36 @@
package version

import (
"fmt"
_ "embed"
"os"
"strings"

version "github.com/hashicorp/go-version"
)

// The main version number that is being run at the moment.
var Version = "1.5.0"
var 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.
var Prerelease = "dev"
var Prerelease string

// 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.
var SemVer *version.Version

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

func init() {
rawVersion = strings.TrimSpace(rawVersion)
version.Must(version.NewVersion(rawVersion))
Version, Prerelease = extractPrerelease(rawVersion)
SemVer = version.Must(version.NewVersion(Version))
}

Expand All @@ -33,8 +44,20 @@ const Header = "Terraform-Version"

// String returns the complete version string, including prerelease
func String() string {
if Prerelease != "" {
return fmt.Sprintf("%s-%s", Version, Prerelease)
return SemVer.String()
}

func extractPrerelease(rawVersion string) (string, string) {
parts := strings.Split(rawVersion, "-")

var prerelease string
if os.Getenv("TFDEV") == "1" {
prerelease = "dev"
} else if len(parts) > 1 {
prerelease = parts[1]
} else {
prerelease = ""
}
return Version

return parts[0], prerelease
}

0 comments on commit 891133f

Please sign in to comment.