github.com/hashicorp/packer@v1.14.3/version/version.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package version
     5  
     6  import (
     7  	_ "embed"
     8  	"strings"
     9  
    10  	"github.com/hashicorp/go-version"
    11  	pluginVersion "github.com/hashicorp/packer-plugin-sdk/version"
    12  )
    13  
    14  var (
    15  	// The git commit that was compiled. This will be filled in by the compiler.
    16  	GitCommit   string
    17  	GitDescribe string
    18  
    19  	// Whether cgo is enabled or not; set at build time
    20  	CgoEnabled bool
    21  
    22  	//go:embed VERSION
    23  	rawVersion string
    24  
    25  	// The next version number that will be released. This will be updated after every release
    26  	// Version must conform to the format expected by github.com/hashicorp/go-version
    27  	// for tests to work.
    28  	// A pre-release marker for the version can also be specified (e.g -dev). If this is omitted
    29  	// The main version number that is being run at the moment.
    30  	Version string
    31  	// A pre-release marker for the version. If this is "" (empty string)
    32  	// then it means that it is a final release. Otherwise, this is a pre-release
    33  	// such as "dev" (in development), "beta", "rc1", etc.
    34  	VersionPrerelease string
    35  	// VersionMetadata may be added to give more non-normalised information on a build
    36  	// like a commit SHA for example.
    37  	//
    38  	// Ex: 1.0.0-dev+metadata
    39  	VersionMetadata string
    40  )
    41  
    42  var PackerVersion *pluginVersion.PluginVersion
    43  
    44  func FormattedVersion() string {
    45  	return PackerVersion.FormattedVersion()
    46  }
    47  
    48  // SemVer is an instance of version.Version. This has the secondary
    49  // benefit of verifying during tests and init time that our version is a
    50  // proper semantic version, which should always be the case.
    51  var SemVer *version.Version
    52  
    53  func init() {
    54  	rawVersion = strings.TrimSpace(rawVersion)
    55  
    56  	PackerVersion = pluginVersion.NewRawVersion(rawVersion)
    57  	// A bug in the SDK prevents us from calling SemVer on the PluginVersion
    58  	// derived from the rawVersion, as when doing so, we reset the semVer
    59  	// attribute to only use the core part of the version, thereby dropping any
    60  	// information on pre-release/metadata.
    61  	SemVer, _ = version.NewVersion(rawVersion)
    62  
    63  	Version = PackerVersion.GetVersion()
    64  	VersionPrerelease = PackerVersion.GetVersionPrerelease()
    65  	VersionMetadata = PackerVersion.GetMetadata()
    66  }
    67  
    68  // String returns the complete version string, including prerelease
    69  func String() string {
    70  	return PackerVersion.String()
    71  }