forked from hashicorp/levant
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
version: bring version command inline with HashiCorp standard.
- Loading branch information
Showing
5 changed files
with
94 additions
and
62 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |