github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/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  // This should be check to a callback to check for the latest version.
    13  //
    14  // The global nature of this variable is dirty, but a version checker
    15  // really shouldn't change anyways.
    16  var VersionChecker VersionCheckFunc
    17  
    18  // The version of packer.
    19  const Version = "0.7.1"
    20  
    21  // Any pre-release marker for the version. If this is "" (empty string),
    22  // then it means that it is a final release. Otherwise, this is the
    23  // pre-release marker.
    24  const VersionPrerelease = ""
    25  
    26  // VersionCheckFunc is the callback that is called to check the latest
    27  // version of Packer.
    28  type VersionCheckFunc func(string) (VersionCheckInfo, error)
    29  
    30  // VersionCheckInfo is the return value for the VersionCheckFunc that
    31  // contains the latest version information.
    32  type VersionCheckInfo struct {
    33  	Outdated bool
    34  	Latest   string
    35  	Alerts   []string
    36  }
    37  
    38  type versionCommand byte
    39  
    40  func (versionCommand) Help() string {
    41  	return `usage: packer version
    42  
    43  Outputs the version of Packer that is running. There are no additional
    44  command-line flags for this command.`
    45  }
    46  
    47  func (versionCommand) Run(env Environment, args []string) int {
    48  	env.Ui().Machine("version", Version)
    49  	env.Ui().Machine("version-prelease", VersionPrerelease)
    50  	env.Ui().Machine("version-commit", GitCommit)
    51  	env.Ui().Say(VersionString())
    52  
    53  	if VersionChecker != nil {
    54  		current := Version
    55  		if VersionPrerelease != "" {
    56  			current += fmt.Sprintf(".%s", VersionPrerelease)
    57  		}
    58  
    59  		info, err := VersionChecker(current)
    60  		if err != nil {
    61  			env.Ui().Say(fmt.Sprintf("\nError checking latest version: %s", err))
    62  		}
    63  		if info.Outdated {
    64  			env.Ui().Say(fmt.Sprintf(
    65  				"\nYour version of Packer is out of date! The latest version\n"+
    66  					"is %s. You can update by downloading from www.packer.io.",
    67  				info.Latest))
    68  		}
    69  	}
    70  
    71  	return 0
    72  }
    73  
    74  func (versionCommand) Synopsis() string {
    75  	return "print Packer version"
    76  }
    77  
    78  // VersionString returns the Packer version in human-readable
    79  // form complete with pre-release and git commit info if it is
    80  // available.
    81  func VersionString() string {
    82  	var versionString bytes.Buffer
    83  	fmt.Fprintf(&versionString, "Packer v%s", Version)
    84  	if VersionPrerelease != "" {
    85  		fmt.Fprintf(&versionString, ".%s", VersionPrerelease)
    86  
    87  		if GitCommit != "" {
    88  			fmt.Fprintf(&versionString, " (%s)", GitCommit)
    89  		}
    90  	}
    91  
    92  	return versionString.String()
    93  }