github.com/ThomasObenaus/nomad@v0.11.1/version/version.go (about)

     1  package version
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  )
     7  
     8  var (
     9  	// The git commit that was compiled. This will be filled in by the compiler.
    10  	GitCommit   string
    11  	GitDescribe string
    12  
    13  	// The main version number that is being run at the moment.
    14  	Version = "0.11.1"
    15  
    16  	// A pre-release marker for the version. If this is "" (empty string)
    17  	// then it means that it is a final release. Otherwise, this is a pre-release
    18  	// such as "dev" (in development), "beta", "rc1", etc.
    19  	VersionPrerelease = ""
    20  
    21  	// VersionMetadata is metadata further describing the build type.
    22  	VersionMetadata = ""
    23  )
    24  
    25  // VersionInfo
    26  type VersionInfo struct {
    27  	Revision          string
    28  	Version           string
    29  	VersionPrerelease string
    30  	VersionMetadata   string
    31  }
    32  
    33  func GetVersion() *VersionInfo {
    34  	ver := Version
    35  	rel := VersionPrerelease
    36  	md := VersionMetadata
    37  	if GitDescribe != "" {
    38  		ver = GitDescribe
    39  	}
    40  	if GitDescribe == "" && rel == "" && VersionPrerelease != "" {
    41  		rel = "dev"
    42  	}
    43  
    44  	return &VersionInfo{
    45  		Revision:          GitCommit,
    46  		Version:           ver,
    47  		VersionPrerelease: rel,
    48  		VersionMetadata:   md,
    49  	}
    50  }
    51  
    52  func (c *VersionInfo) VersionNumber() string {
    53  	version := fmt.Sprintf("%s", c.Version)
    54  
    55  	if c.VersionPrerelease != "" {
    56  		version = fmt.Sprintf("%s-%s", version, c.VersionPrerelease)
    57  	}
    58  
    59  	if c.VersionMetadata != "" {
    60  		version = fmt.Sprintf("%s+%s", version, c.VersionMetadata)
    61  	}
    62  
    63  	return version
    64  }
    65  
    66  func (c *VersionInfo) FullVersionNumber(rev bool) string {
    67  	var versionString bytes.Buffer
    68  
    69  	fmt.Fprintf(&versionString, "Nomad v%s", c.Version)
    70  	if c.VersionPrerelease != "" {
    71  		fmt.Fprintf(&versionString, "-%s", c.VersionPrerelease)
    72  	}
    73  
    74  	if c.VersionMetadata != "" {
    75  		fmt.Fprintf(&versionString, "+%s", c.VersionMetadata)
    76  	}
    77  
    78  	if rev && c.Revision != "" {
    79  		fmt.Fprintf(&versionString, " (%s)", c.Revision)
    80  	}
    81  
    82  	return versionString.String()
    83  }