From 728c4963700c1c5d658a2a2fe32fa0a9392c57aa Mon Sep 17 00:00:00 2001 From: Christopher Boumenot Date: Thu, 21 Apr 2016 13:19:43 -0700 Subject: [PATCH] Move version to its own package. (#3460) --- Makefile | 8 ++++---- checkpoint.go | 7 ++++--- commands.go | 7 ++++--- main.go | 9 ++++----- scripts/build.sh | 2 +- version.go => version/version.go | 21 ++++++++++++++++++++- 6 files changed, 37 insertions(+), 17 deletions(-) rename version.go => version/version.go (54%) diff --git a/Makefile b/Makefile index e13fdb533..b8c33e740 100644 --- a/Makefile +++ b/Makefile @@ -16,8 +16,8 @@ bin: deps @GO15VENDOREXPERIMENT=1 sh -c "$(CURDIR)/scripts/build.sh" releasebin: deps - @grep 'const VersionPrerelease = "dev"' version.go > /dev/null ; if [ $$? -eq 0 ]; then \ - echo "ERROR: You must remove prerelease tags from version.go prior to release."; \ + @grep 'const VersionPrerelease = "dev"' version/version.go > /dev/null ; if [ $$? -eq 0 ]; then \ + echo "ERROR: You must remove prerelease tags from version/version.go prior to release."; \ exit 1; \ fi @GO15VENDOREXPERIMENT=1 sh -c "$(CURDIR)/scripts/build.sh" @@ -36,8 +36,8 @@ deps: fi dev: deps - @grep 'const VersionPrerelease = ""' version.go > /dev/null ; if [ $$? -eq 0 ]; then \ - echo "ERROR: You must add prerelease tags to version.go prior to making a dev build."; \ + @grep 'const VersionPrerelease = ""' version/version.go > /dev/null ; if [ $$? -eq 0 ]; then \ + echo "ERROR: You must add prerelease tags to version/version.go prior to making a dev build."; \ exit 1; \ fi @PACKER_DEV=1 GO15VENDOREXPERIMENT=1 sh -c "$(CURDIR)/scripts/build.sh" diff --git a/checkpoint.go b/checkpoint.go index 87ef594c2..3fbd37b3f 100644 --- a/checkpoint.go +++ b/checkpoint.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/go-checkpoint" "github.com/mitchellh/packer/command" "github.com/mitchellh/packer/packer" + packerVersion "github.com/mitchellh/packer/version" ) func init() { @@ -33,9 +34,9 @@ func runCheckpoint(c *config) { return } - version := Version - if VersionPrerelease != "" { - version += fmt.Sprintf("-%s", VersionPrerelease) + version := packerVersion.Version + if packerVersion.VersionPrerelease != "" { + version += fmt.Sprintf("-%s", packerVersion.VersionPrerelease) } signaturePath := filepath.Join(configDir, "checkpoint_signature") diff --git a/commands.go b/commands.go index 21ed30df9..a3834c7b9 100644 --- a/commands.go +++ b/commands.go @@ -6,6 +6,7 @@ import ( "github.com/mitchellh/cli" "github.com/mitchellh/packer/command" + "github.com/mitchellh/packer/version" ) // Commands is the mapping of all the available Terraform commands. @@ -53,9 +54,9 @@ func init() { "version": func() (cli.Command, error) { return &command.VersionCommand{ Meta: *CommandMeta, - Revision: GitCommit, - Version: Version, - VersionPrerelease: VersionPrerelease, + Revision: version.GitCommit, + Version: version.Version, + VersionPrerelease: version.VersionPrerelease, CheckFunc: commandVersionCheck, }, nil }, diff --git a/main.go b/main.go index 3aa09846b..bfd81c649 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ import ( "github.com/mitchellh/packer/command" "github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer/plugin" + "github.com/mitchellh/packer/version" "github.com/mitchellh/panicwrap" "github.com/mitchellh/prefixedio" ) @@ -105,9 +106,7 @@ func wrappedMain() int { log.SetOutput(os.Stderr) - log.Printf( - "[INFO] Packer version: %s %s %s", - Version, VersionPrerelease, GitCommit) + log.Printf("[INFO] Packer version: %s", version.FormattedVersion()) log.Printf("Packer Target OS/Arch: %s %s", runtime.GOOS, runtime.GOARCH) log.Printf("Built with Go Version: %s", runtime.Version()) @@ -175,7 +174,7 @@ func wrappedMain() int { PostProcessor: config.LoadPostProcessor, Provisioner: config.LoadProvisioner, }, - Version: Version, + Version: version.Version, }, Cache: cache, Ui: ui, @@ -188,7 +187,7 @@ func wrappedMain() int { Commands: Commands, HelpFunc: excludeHelpFunc(Commands, []string{"plugin"}), HelpWriter: os.Stdout, - Version: Version, + Version: version.Version, } exitCode, err := cli.Run() diff --git a/scripts/build.sh b/scripts/build.sh index 343ea04cf..84f87d6d4 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -37,7 +37,7 @@ set +e gox \ -os="${XC_OS}" \ -arch="${XC_ARCH}" \ - -ldflags "-X main.GitCommit ${GIT_COMMIT}${GIT_DIRTY}" \ + -ldflags "-X github.com/mitchellh/packer/version.GitCommit ${GIT_COMMIT}${GIT_DIRTY}" \ -output "pkg/{{.OS}}_{{.Arch}}/packer" \ . set -e diff --git a/version.go b/version/version.go similarity index 54% rename from version.go rename to version/version.go index 87ef2cf8f..0394db2aa 100644 --- a/version.go +++ b/version/version.go @@ -1,4 +1,9 @@ -package main +package version + +import ( + "bytes" + "fmt" +) // The git commit that was compiled. This will be filled in by the compiler. var GitCommit string @@ -10,3 +15,17 @@ const Version = "0.10.1" // 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" + +func FormattedVersion() string { + var versionString bytes.Buffer + fmt.Fprintf(&versionString, "%s", Version) + if VersionPrerelease != "" { + fmt.Fprintf(&versionString, ".%s", VersionPrerelease) + + if GitCommit != "" { + fmt.Fprintf(&versionString, " (%s)", GitCommit) + } + } + + return versionString.String() +}