From 8dbe0f065cfb43767226afac89d805e52afea5cb Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 27 Oct 2014 20:51:34 -0700 Subject: [PATCH] Remove version from "packer" package --- checkpoint.go | 19 +++++---- command/version.go | 79 ++++++++++++++++++++++++++++++++++ commands.go | 10 +++++ common/step_download.go | 7 ++-- main.go | 4 +- packer/plugin/server.go | 3 -- packer/template.go | 2 + packer/version.go | 93 ----------------------------------------- version.go | 12 ++++++ 9 files changed, 119 insertions(+), 110 deletions(-) create mode 100644 command/version.go delete mode 100644 packer/version.go create mode 100644 version.go diff --git a/checkpoint.go b/checkpoint.go index 159e9673d..5d2e486db 100644 --- a/checkpoint.go +++ b/checkpoint.go @@ -6,11 +6,10 @@ import ( "path/filepath" "github.com/hashicorp/go-checkpoint" - "github.com/mitchellh/packer/packer" + "github.com/mitchellh/packer/command" ) func init() { - packer.VersionChecker = packerVersionCheck checkpointResult = make(chan *checkpoint.CheckResponse, 1) } @@ -33,9 +32,9 @@ func runCheckpoint(c *config) { return } - version := packer.Version - if packer.VersionPrerelease != "" { - version += fmt.Sprintf(".%s", packer.VersionPrerelease) + version := Version + if VersionPrerelease != "" { + version += fmt.Sprintf(".%s", VersionPrerelease) } signaturePath := filepath.Join(configDir, "checkpoint_signature") @@ -58,21 +57,23 @@ func runCheckpoint(c *config) { checkpointResult <- resp } -// packerVersionCheck implements packer.VersionCheckFunc and is used +// commandVersionCheck implements command.VersionCheckFunc and is used // as the version checker. -func packerVersionCheck(current string) (packer.VersionCheckInfo, error) { +func commandVersionCheck() (command.VersionCheckInfo, error) { + // Wait for the result to come through info := <-checkpointResult if info == nil { - var zero packer.VersionCheckInfo + var zero command.VersionCheckInfo return zero, nil } + // Build the alerts that we may have received about our version alerts := make([]string, len(info.Alerts)) for i, a := range info.Alerts { alerts[i] = a.Message } - return packer.VersionCheckInfo{ + return command.VersionCheckInfo{ Outdated: info.Outdated, Latest: info.CurrentVersion, Alerts: alerts, diff --git a/command/version.go b/command/version.go new file mode 100644 index 000000000..5ca8e6004 --- /dev/null +++ b/command/version.go @@ -0,0 +1,79 @@ +package command + +import ( + "bytes" + "fmt" +) + +// VersionCommand is a Command implementation prints the version. +type VersionCommand struct { + Meta + + Revision string + Version string + VersionPrerelease string + CheckFunc VersionCheckFunc +} + +// VersionCheckFunc is the callback called by the Version command to +// check if there is a new version of Packer. +type VersionCheckFunc func() (VersionCheckInfo, error) + +// VersionCheckInfo is the return value for the VersionCheckFunc callback +// and tells the Version command information about the latest version +// of Packer. +type VersionCheckInfo struct { + Outdated bool + Latest string + Alerts []string +} + +func (c *VersionCommand) Help() string { + return "" +} + +func (c *VersionCommand) Run(args []string) int { + /* + env.Ui().Machine("version", Version) + env.Ui().Machine("version-prelease", VersionPrerelease) + env.Ui().Machine("version-commit", GitCommit) + */ + + var versionString bytes.Buffer + fmt.Fprintf(&versionString, "Packer v%s", c.Version) + if c.VersionPrerelease != "" { + fmt.Fprintf(&versionString, ".%s", c.VersionPrerelease) + + if c.Revision != "" { + fmt.Fprintf(&versionString, " (%s)", c.Revision) + } + } + + c.Ui.Output(versionString.String()) + + // If we have a version check function, then let's check for + // the latest version as well. + if c.CheckFunc != nil { + // Separate the prior output with a newline + c.Ui.Output("") + + // Check the latest version + info, err := c.CheckFunc() + if err != nil { + c.Ui.Error(fmt.Sprintf( + "Error checking latest version: %s", err)) + } + if info.Outdated { + c.Ui.Output(fmt.Sprintf( + "Your version of Packer is out of date! The latest version\n"+ + "is %s. You can update by downloading from www.packer.io", + info.Latest)) + } + } + + return 0 +} + +func (c *VersionCommand) Synopsis() string { + return "Prints the Packer version" +} diff --git a/commands.go b/commands.go index 8aa6314aa..193fd1465 100644 --- a/commands.go +++ b/commands.go @@ -55,6 +55,16 @@ func init() { Meta: meta, }, nil }, + + "version": func() (cli.Command, error) { + return &command.VersionCommand{ + Meta: meta, + Revision: GitCommit, + Version: Version, + VersionPrerelease: VersionPrerelease, + CheckFunc: commandVersionCheck, + }, nil + }, } } diff --git a/common/step_download.go b/common/step_download.go index 34156d52d..8d6378adc 100644 --- a/common/step_download.go +++ b/common/step_download.go @@ -3,10 +3,11 @@ package common import ( "encoding/hex" "fmt" - "github.com/mitchellh/multistep" - "github.com/mitchellh/packer/packer" "log" "time" + + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" ) // StepDownload downloads a remote file using the download client within @@ -70,7 +71,7 @@ func (s *StepDownload) Run(state multistep.StateBag) multistep.StepAction { CopyFile: false, Hash: HashForType(s.ChecksumType), Checksum: checksum, - UserAgent: packer.VersionString(), + UserAgent: "Packer", } path, err, retry := s.download(config, state) diff --git a/main.go b/main.go index f72c876b4..093719b3f 100644 --- a/main.go +++ b/main.go @@ -101,8 +101,8 @@ func wrappedMain() int { log.SetOutput(os.Stderr) log.Printf( - "Packer Version: %s %s %s", - packer.Version, packer.VersionPrerelease, packer.GitCommit) + "[INFO] Packer version: %s %s %s", + Version, VersionPrerelease, GitCommit) log.Printf("Packer Target OS/Arch: %s %s", runtime.GOOS, runtime.GOARCH) log.Printf("Built with Go Version: %s", runtime.Version()) diff --git a/packer/plugin/server.go b/packer/plugin/server.go index 83292c320..23f39c028 100644 --- a/packer/plugin/server.go +++ b/packer/plugin/server.go @@ -10,7 +10,6 @@ package plugin import ( "errors" "fmt" - "github.com/mitchellh/packer/packer" packrpc "github.com/mitchellh/packer/packer/rpc" "io/ioutil" "log" @@ -38,8 +37,6 @@ const APIVersion = "4" // Server waits for a connection to this plugin and returns a Packer // RPC server that you can use to register components and serve them. func Server() (*packrpc.Server, error) { - log.Printf("Plugin build against Packer '%s'", packer.GitCommit) - if os.Getenv(MagicCookieKey) != MagicCookieValue { return nil, errors.New( "Please do not execute plugins directly. Packer will execute these for you.") diff --git a/packer/template.go b/packer/template.go index ed71e90d6..93f487d61 100644 --- a/packer/template.go +++ b/packer/template.go @@ -119,6 +119,8 @@ func ParseTemplate(data []byte, vars map[string]string) (t *Template, err error) } if rawTpl.MinimumPackerVersion != "" { + // TODO: NOPE! Replace this + Version := "1.0" vCur, err := version.NewVersion(Version) if err != nil { panic(err) diff --git a/packer/version.go b/packer/version.go deleted file mode 100644 index 0be4b66a3..000000000 --- a/packer/version.go +++ /dev/null @@ -1,93 +0,0 @@ -package packer - -import ( - "bytes" - "fmt" -) - -// The git commit that is being compiled. This will be filled in by the -// compiler for source builds. -var GitCommit string - -// 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 - -// The version of packer. -const Version = "0.7.1" - -// 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. -const VersionPrerelease = "" - -// 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 -} - -type versionCommand byte - -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.` -} - -func (versionCommand) Run(env Environment, args []string) int { - env.Ui().Machine("version", Version) - env.Ui().Machine("version-prelease", VersionPrerelease) - env.Ui().Machine("version-commit", GitCommit) - env.Ui().Say(VersionString()) - - 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)) - } - } - - return 0 -} - -func (versionCommand) Synopsis() string { - return "print Packer version" -} - -// VersionString returns the Packer version in human-readable -// form complete with pre-release and git commit info if it is -// available. -func VersionString() string { - var versionString bytes.Buffer - fmt.Fprintf(&versionString, "Packer v%s", Version) - if VersionPrerelease != "" { - fmt.Fprintf(&versionString, ".%s", VersionPrerelease) - - if GitCommit != "" { - fmt.Fprintf(&versionString, " (%s)", GitCommit) - } - } - - return versionString.String() -} diff --git a/version.go b/version.go new file mode 100644 index 000000000..dbfc8da12 --- /dev/null +++ b/version.go @@ -0,0 +1,12 @@ +package main + +// The git commit that was compiled. This will be filled in by the compiler. +var GitCommit string + +// The main version number that is being run at the moment. +const Version = "0.8.0" + +// A pre-release marker for the version. If this is "" (empty string) +// then it means that it is a final release. Otherwise, this is a pre-release +// such as "dev" (in development), "beta", "rc1", etc. +const VersionPrerelease = "dev"