2013-03-24 17:03:53 -04:00
|
|
|
package packer
|
|
|
|
|
2013-06-20 01:20:52 -04:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
)
|
2013-05-27 18:12:48 -04:00
|
|
|
|
2013-07-08 18:37:01 -04:00
|
|
|
// The git commit that is being compiled. This will be filled in by the
|
|
|
|
// compiler for source builds.
|
|
|
|
var GitCommit string
|
|
|
|
|
2014-09-08 18:25:50 -04:00
|
|
|
// This should be check to a callback to check for the latest version.
|
|
|
|
//
|
|
|
|
// The global nature of this variable is dirty, but a version checker
|
|
|
|
// really shouldn't change anyways.
|
|
|
|
var VersionChecker VersionCheckFunc
|
|
|
|
|
2013-03-24 17:03:53 -04:00
|
|
|
// The version of packer.
|
2014-09-08 22:36:11 -04:00
|
|
|
const Version = "0.7.1"
|
2013-06-20 01:20:52 -04:00
|
|
|
|
|
|
|
// Any pre-release marker for the version. If this is "" (empty string),
|
|
|
|
// then it means that it is a final release. Otherwise, this is the
|
|
|
|
// pre-release marker.
|
2014-09-08 22:36:11 -04:00
|
|
|
const VersionPrerelease = "dev"
|
2013-03-24 17:03:53 -04:00
|
|
|
|
2014-09-08 18:25:50 -04:00
|
|
|
// VersionCheckFunc is the callback that is called to check the latest
|
|
|
|
// version of Packer.
|
|
|
|
type VersionCheckFunc func(string) (VersionCheckInfo, error)
|
|
|
|
|
|
|
|
// VersionCheckInfo is the return value for the VersionCheckFunc that
|
|
|
|
// contains the latest version information.
|
|
|
|
type VersionCheckInfo struct {
|
|
|
|
Outdated bool
|
|
|
|
Latest string
|
|
|
|
Alerts []string
|
|
|
|
}
|
|
|
|
|
2013-03-24 17:03:53 -04:00
|
|
|
type versionCommand byte
|
|
|
|
|
2013-06-02 14:41:12 -04:00
|
|
|
func (versionCommand) Help() string {
|
|
|
|
return `usage: packer version
|
|
|
|
|
|
|
|
Outputs the version of Packer that is running. There are no additional
|
|
|
|
command-line flags for this command.`
|
|
|
|
}
|
|
|
|
|
2013-05-02 17:03:55 -04:00
|
|
|
func (versionCommand) Run(env Environment, args []string) int {
|
2013-08-12 02:56:47 -04:00
|
|
|
env.Ui().Machine("version", Version)
|
|
|
|
env.Ui().Machine("version-prelease", VersionPrerelease)
|
|
|
|
env.Ui().Machine("version-commit", GitCommit)
|
2014-01-09 16:20:30 -05:00
|
|
|
env.Ui().Say(VersionString())
|
2014-09-08 18:25:50 -04:00
|
|
|
|
|
|
|
if VersionChecker != nil {
|
|
|
|
current := Version
|
|
|
|
if VersionPrerelease != "" {
|
|
|
|
current += fmt.Sprintf(".%s", VersionPrerelease)
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := VersionChecker(current)
|
|
|
|
if err != nil {
|
|
|
|
env.Ui().Say(fmt.Sprintf("\nError checking latest version: %s", err))
|
|
|
|
}
|
|
|
|
if info.Outdated {
|
|
|
|
env.Ui().Say(fmt.Sprintf(
|
|
|
|
"\nYour version of Packer is out of date! The latest version\n"+
|
|
|
|
"is %s. You can update by downloading from www.packer.io.",
|
|
|
|
info.Latest))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 16:20:30 -05:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (versionCommand) Synopsis() string {
|
|
|
|
return "print Packer version"
|
|
|
|
}
|
|
|
|
|
2014-01-19 18:48:56 -05:00
|
|
|
// VersionString returns the Packer version in human-readable
|
|
|
|
// form complete with pre-release and git commit info if it is
|
|
|
|
// available.
|
2014-01-09 16:20:30 -05:00
|
|
|
func VersionString() string {
|
2013-06-20 01:20:52 -04:00
|
|
|
var versionString bytes.Buffer
|
|
|
|
fmt.Fprintf(&versionString, "Packer v%s", Version)
|
|
|
|
if VersionPrerelease != "" {
|
|
|
|
fmt.Fprintf(&versionString, ".%s", VersionPrerelease)
|
|
|
|
|
2013-07-08 18:38:14 -04:00
|
|
|
if GitCommit != "" {
|
|
|
|
fmt.Fprintf(&versionString, " (%s)", GitCommit)
|
|
|
|
}
|
2013-07-08 18:37:01 -04:00
|
|
|
}
|
|
|
|
|
2014-01-19 22:30:11 -05:00
|
|
|
return versionString.String()
|
2013-03-24 19:28:35 -04:00
|
|
|
}
|