diff --git a/cmd/root.go b/cmd/root.go index a70c8675..7a6ad44d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -19,7 +19,7 @@ func RootCommand() *cli.App { cliApp := cli.NewApp() cliApp.Name = "helmfile" cliApp.Usage = "" - cliApp.Version = version.Version + cliApp.Version = version.GetVersion() cliApp.EnableBashCompletion = true cliApp.Before = configureLogging setRootCommandFlags(cliApp) diff --git a/go.mod b/go.mod index 5ccdc148..132de0ec 100644 --- a/go.mod +++ b/go.mod @@ -32,6 +32,7 @@ require ( golang.org/x/sync v0.0.0-20210220032951-036812b2e83c golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 gopkg.in/yaml.v2 v2.4.0 + gotest.tools v2.2.0+incompatible k8s.io/apimachinery v0.23.4 ) diff --git a/pkg/app/version/version.go b/pkg/app/version/version.go index 0bbfd911..d1e2f5a9 100644 --- a/pkg/app/version/version.go +++ b/pkg/app/version/version.go @@ -1,5 +1,28 @@ // Package version is used to get the version of the Helmfile CLI. package version +import "fmt" + // Version is the version of Helmfile var Version string + +// Commit is the git revision +var Commit string + +func GetVersion() string { + if Version == "" { + Version = "0.0.0-dev" + } + return Version +} + +func GetCommit() string { + if Commit == "" { + Commit = "unknown_commit" + } + return Commit +} + +func GetVersionWithCommit() string { + return fmt.Sprintf("%s-%s", GetVersion(), GetCommit()) +} diff --git a/pkg/app/version/version_test.go b/pkg/app/version/version_test.go new file mode 100644 index 00000000..02422b23 --- /dev/null +++ b/pkg/app/version/version_test.go @@ -0,0 +1,38 @@ +package version + +import ( + "testing" + + "gotest.tools/assert" +) + +func TestGetVersion(t *testing.T) { + current := Version + Version = "" + assert.Equal(t, "0.0.0-dev", GetVersion()) + Version = "1.2.3" + assert.Equal(t, "1.2.3", GetVersion()) + Version = current +} + +func TestGetCommit(t *testing.T) { + current := Commit + Commit = "" + assert.Equal(t, "unknown_commit", GetCommit()) + Commit = "abc123xyz" + assert.Equal(t, "abc123xyz", GetCommit()) + Commit = current +} + +func TestGetVersionWithCommit(t *testing.T) { + currentVersion := Version + currentCommit := Commit + Version = "" + Commit = "" + assert.Equal(t, "0.0.0-dev-unknown_commit", GetVersionWithCommit()) + Version = "1.2.3" + Commit = "abc123xyz" + assert.Equal(t, "1.2.3-abc123xyz", GetVersionWithCommit()) + Version = currentVersion + Commit = currentCommit +}