Skip to content

Commit

Permalink
version: bring version command inline with HashiCorp standard.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasell committed Aug 14, 2020
1 parent 75a8cc6 commit d06a053
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 62 deletions.
17 changes: 0 additions & 17 deletions buildtime/consts.go

This file was deleted.

16 changes: 5 additions & 11 deletions commands.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main

import (
"fmt"
"os"

"github.com/jrasell/levant/command"
"github.com/jrasell/levant/version"
"github.com/hashicorp/levant/command"
"github.com/hashicorp/levant/version"
"github.com/mitchellh/cli"
)

Expand Down Expand Up @@ -57,16 +58,9 @@ func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory {
}, nil
},
"version": func() (cli.Command, error) {
ver := version.Version
rel := version.VersionPrerelease

if rel == "" && version.VersionPrerelease != "" {
rel = "dev"
}
return &command.VersionCommand{
Version: ver,
VersionPrerelease: rel,
UI: meta.UI,
Version: fmt.Sprintf("Levant %s", version.GetHumanVersion()),
UI: meta.UI,
}, nil
},
}
Expand Down
22 changes: 0 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,10 @@ import (
"fmt"
"os"

"github.com/jrasell/levant/buildtime"
"github.com/mitchellh/cli"
)

// These variables are populated by govvv during build time to provide detailed
// version output information.
var (
Version = "dev"
BuildDate string
GitCommit string
GitBranch string
GitState string
GitSummary string
)

func main() {
exportBuildtimeConsts()
os.Exit(Run(os.Args[1:]))
}

Expand Down Expand Up @@ -74,12 +61,3 @@ func RunCustom(args []string, commands map[string]cli.CommandFactory) int {

return exitCode
}

func exportBuildtimeConsts() {
buildtime.GitCommit = GitCommit
buildtime.GitBranch = GitBranch
buildtime.GitState = GitState
buildtime.GitSummary = GitSummary
buildtime.BuildDate = BuildDate
buildtime.Version = Version
}
55 changes: 43 additions & 12 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
package version

import "fmt"
import (
"fmt"
"strings"
)

// Version is the main version number that is being run at the moment.
const Version = "0.3.0"
var (
GitCommit string
GitDescribe string

// VersionPrerelease is 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"
// Version must conform to the format expected by
// github.com/hashicorp/go-version for tests to work.
Version = "0.3.0"

// Get returns a human readable version of Levant.
func Get() string {
// VersionPrerelease is the 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.
VersionPrerelease = "dev"
)

if VersionPrerelease != "" {
return fmt.Sprintf("%s-%s", Version, VersionPrerelease)
// GetHumanVersion composes the parts of the version in a way that's suitable
// for displaying to humans.
func GetHumanVersion() string {
version := Version
if GitDescribe != "" {
version = GitDescribe
}

return Version
// Add v as prefix if not present
if !strings.HasPrefix(version, "v") {
version = fmt.Sprintf("v%s", version)
}

release := VersionPrerelease
if GitDescribe == "" && release == "" {
release = "dev"
}

if release != "" {
if !strings.HasSuffix(version, "-"+release) {
// if we tagged a prerelease version then the release is in the version already
version += fmt.Sprintf("-%s", release)
}
if GitCommit != "" {
version += fmt.Sprintf(" (%s)", GitCommit)
}
}

// Strip off any single quotes added by the git information.
return strings.Replace(version, "'", "", -1)
}
46 changes: 46 additions & 0 deletions version/version_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,47 @@
package version

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_GetHumanVersion(t *testing.T) {
testCases := []struct {
inputCommit string
inputDescribe string
inputVersion string
inputPrerelease string
expectedOutput string
}{
{
inputCommit: "440bca3+CHANGES",
inputDescribe: "",
inputVersion: "0.1.3",
inputPrerelease: "dev",
expectedOutput: "v0.1.3-dev (440bca3+CHANGES)",
},
{
inputCommit: "440bca3",
inputDescribe: "",
inputVersion: "0.6.0",
inputPrerelease: "beta1",
expectedOutput: "v0.6.0-beta1 (440bca3)",
},
{
inputCommit: "440bca3",
inputDescribe: "v1.0.0",
inputVersion: "1.0.0",
inputPrerelease: "",
expectedOutput: "v1.0.0",
},
}

for _, tc := range testCases {
GitCommit = tc.inputCommit
GitDescribe = tc.inputDescribe
Version = tc.inputVersion
VersionPrerelease = tc.inputPrerelease
assert.Equal(t, tc.expectedOutput, GetHumanVersion())
}
}

0 comments on commit d06a053

Please sign in to comment.