github.com/tonnydourado/packer@v0.6.1-0.20140701134019-5d0cd9676a37/packer/version.go (about)

     1  package packer
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  )
     7  
     8  // The git commit that is being compiled. This will be filled in by the
     9  // compiler for source builds.
    10  var GitCommit string
    11  
    12  // The version of packer.
    13  const Version = "0.6.1"
    14  
    15  // Any pre-release marker for the version. If this is "" (empty string),
    16  // then it means that it is a final release. Otherwise, this is the
    17  // pre-release marker.
    18  const VersionPrerelease = "dev"
    19  
    20  type versionCommand byte
    21  
    22  func (versionCommand) Help() string {
    23  	return `usage: packer version
    24  
    25  Outputs the version of Packer that is running. There are no additional
    26  command-line flags for this command.`
    27  }
    28  
    29  func (versionCommand) Run(env Environment, args []string) int {
    30  	env.Ui().Machine("version", Version)
    31  	env.Ui().Machine("version-prelease", VersionPrerelease)
    32  	env.Ui().Machine("version-commit", GitCommit)
    33  
    34  	env.Ui().Say(VersionString())
    35  	return 0
    36  }
    37  
    38  func (versionCommand) Synopsis() string {
    39  	return "print Packer version"
    40  }
    41  
    42  // VersionString returns the Packer version in human-readable
    43  // form complete with pre-release and git commit info if it is
    44  // available.
    45  func VersionString() string {
    46  	var versionString bytes.Buffer
    47  	fmt.Fprintf(&versionString, "Packer v%s", Version)
    48  	if VersionPrerelease != "" {
    49  		fmt.Fprintf(&versionString, ".%s", VersionPrerelease)
    50  
    51  		if GitCommit != "" {
    52  			fmt.Fprintf(&versionString, " (%s)", GitCommit)
    53  		}
    54  	}
    55  
    56  	return versionString.String()
    57  }