Skip to content

Commit

Permalink
Add --version flag (#276)
Browse files Browse the repository at this point in the history
* 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
jdecool committed May 10, 2020
1 parent a38f4dc commit a7732ca
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ before:
hooks:
- go mod download
builds:
-
- <<: &build_defaults
ldflags:
- -s -w -X github.com/dunglas/mercure/common.version={{ .Version }} -X github.com/dunglas/mercure/common.commit={{ .ShortCommit }} -X github.com/dunglas/mercure/common.buildDate={{ .Date }}
env:
- CGO_ENABLED=0
goos:
Expand Down
9 changes: 9 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package cmd

import (
"fmt"

log "github.com/sirupsen/logrus"

"github.com/dunglas/mercure/common"
"github.com/dunglas/mercure/hub"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -37,4 +40,10 @@ func init() { //nolint:gochecknoinits
})
fs := rootCmd.Flags()
hub.SetFlags(fs, v)

appVersion := common.AppVersion
rootCmd.Version = appVersion.Shortline()

versionTemplate := fmt.Sprintf("Mercure.rocks Hub version %s\n%s\n", rootCmd.Version, appVersion.ChangelogURL())
rootCmd.SetVersionTemplate(versionTemplate)
}
61 changes: 61 additions & 0 deletions common/version.go
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,
}
}
50 changes: 50 additions & 0 deletions common/version_test.go
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")
}

0 comments on commit a7732ca

Please sign in to comment.