-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add --version flags * Update goreleaser for updating Hub version * Fix linter warning * Move version management in common package * Add common package tests * Fix linter warning * Add commit into version informations * Set commit in ldflags * Changelog URL is based on dev version * Update version template * Use short commit in version information
- Loading branch information
Showing
4 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
"runtime/debug" | ||
"strings" | ||
) | ||
|
||
type AppVersionInfo struct { | ||
Version string | ||
BuildDate string | ||
Commit string | ||
} | ||
|
||
var AppVersion AppVersionInfo //nolint:gochecknoglobals | ||
|
||
// these variables are dynamically set at build. | ||
var version = "dev" | ||
var buildDate = "" //nolint:gochecknoglobals | ||
var commit = "" //nolint:gochecknoglobals | ||
|
||
func (v *AppVersionInfo) Shortline() string { | ||
shortline := v.Version | ||
|
||
if v.Commit != "" { | ||
shortline += fmt.Sprintf(", commit %s", v.Commit) | ||
} | ||
|
||
if v.BuildDate != "" { | ||
shortline += fmt.Sprintf(", built at %s", v.BuildDate) | ||
} | ||
|
||
return shortline | ||
} | ||
|
||
func (v *AppVersionInfo) ChangelogURL() string { | ||
path := "https://github.com/dunglas/mercure" | ||
|
||
if v.Version == "dev" { | ||
return fmt.Sprintf("%s/releases/latest", path) | ||
} | ||
|
||
return fmt.Sprintf("%s/releases/tag/v%s", path, strings.TrimPrefix(v.Version, "v")) | ||
} | ||
|
||
func init() { //nolint:gochecknoinits | ||
if version == "dev" { | ||
info, ok := debug.ReadBuildInfo() | ||
if ok && info.Main.Version != "(devel)" { | ||
version = info.Main.Version | ||
} | ||
} | ||
|
||
version = strings.TrimPrefix(version, "v") | ||
|
||
AppVersion = AppVersionInfo{ | ||
Version: version, | ||
BuildDate: buildDate, | ||
Commit: commit, | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package common | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestVersionInfo(t *testing.T) { | ||
v := AppVersionInfo{ | ||
Version: "dev", | ||
BuildDate: "", | ||
} | ||
|
||
assert.Equal(t, v.Shortline(), "dev") | ||
assert.Equal(t, v.ChangelogURL(), "https://github.com/dunglas/mercure/releases/latest") | ||
} | ||
|
||
func TestVersionInfoWithBuildDate(t *testing.T) { | ||
v := AppVersionInfo{ | ||
Version: "1.0.0", | ||
BuildDate: "2020-05-03T18:42:44Z", | ||
Commit: "", | ||
} | ||
|
||
assert.Equal(t, v.Shortline(), "1.0.0, built at 2020-05-03T18:42:44Z") | ||
assert.Equal(t, v.ChangelogURL(), "https://github.com/dunglas/mercure/releases/tag/v1.0.0") | ||
} | ||
|
||
func TestVersionInfoWithCommit(t *testing.T) { | ||
v := AppVersionInfo{ | ||
Version: "1.0.0", | ||
BuildDate: "", | ||
Commit: "96ee2b9", | ||
} | ||
|
||
assert.Equal(t, v.Shortline(), "1.0.0, commit 96ee2b9") | ||
assert.Equal(t, v.ChangelogURL(), "https://github.com/dunglas/mercure/releases/tag/v1.0.0") | ||
} | ||
|
||
func TestVersionInfoWithBuildDateAndCommit(t *testing.T) { | ||
v := AppVersionInfo{ | ||
Version: "1.0.0", | ||
BuildDate: "2020-05-03T18:42:44Z", | ||
Commit: "96ee2b9", | ||
} | ||
|
||
assert.Equal(t, v.Shortline(), "1.0.0, commit 96ee2b9, built at 2020-05-03T18:42:44Z") | ||
assert.Equal(t, v.ChangelogURL(), "https://github.com/dunglas/mercure/releases/tag/v1.0.0") | ||
} |