Repository: goreleaser/goreleaser
Pull request #1028 (2019-05-27 21:14:52)

feat: improved version command
refs https://github.com/Homebrew/homebrew-core/pull/40045

codecov-io: # [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/1028?src=pr&el=h1) Report
> Merging [#1028](https://codecov.io/gh/goreleaser/goreleaser/pull/1028?src=pr&el=desc) into [master](https://codecov.io/gh/goreleaser/goreleaser/commit/5f825c539333fdb754ded18be8ff1b1b94e58900?src=pr&el=desc) will **increase** coverage by `0.03%`.
> The diff coverage is `90.9%`.

[![Impacted file tree graph](https://codecov.io/gh/goreleaser/goreleaser/pull/1028/graphs/tree.svg?width=650&token=3bJvv6b7hN&height=150&src=pr)](https://codecov.io/gh/goreleaser/goreleaser/pull/1028?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master    #1028      +/-   ##
==========================================
+ Coverage   88.53%   88.57%   +0.03%     
==========================================
  Files          53       53              
  Lines        2607     2616       +9     
==========================================
+ Hits         2308     2317       +9     
  Misses        236      236              
  Partials       63       63
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/1028?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [internal/builders/golang/build.go](https://codecov.io/gh/goreleaser/goreleaser/pull/1028/diff?src=pr&el=tree#diff-aW50ZXJuYWwvYnVpbGRlcnMvZ29sYW5nL2J1aWxkLmdv) | `98.34% <100%> (ø)` | :arrow_up: |
| [main.go](https://codecov.io/gh/goreleaser/goreleaser/pull/1028/diff?src=pr&el=tree#diff-bWFpbi5nbw==) | `50.94% <90%> (+4.55%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/1028?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/1028?src=pr&el=footer). Last update [5f825c5...32c5326](https://codecov.io/gh/goreleaser/goreleaser/pull/1028?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/internal/builders/golang/build.go b/internal/builders/golang/build.go
index 46e23a30..d6961ee8 100644
--- a/internal/builders/golang/build.go
+++ b/internal/builders/golang/build.go
@@ -45,7 +45,7 @@ func (*Builder) WithDefaults(build config.Build) config.Build {
 		build.Goarm = []string{"6"}
 	}
 	if len(build.Ldflags) == 0 {
-		build.Ldflags = []string{"-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}"}
+		build.Ldflags = []string{"-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser"}
 	}
 	if len(build.Targets) == 0 {
 		build.Targets = matrix(build)
diff --git a/main.go b/main.go
index fa2aff5b..934c61d8 100644
--- a/main.go
+++ b/main.go
@@ -21,8 +21,9 @@ import (
 // nolint: gochecknoglobals
 var (
 	version = "dev"
-	commit  = "none"
-	date    = "unknown"
+	commit  = ""
+	date    = ""
+	builtBy = ""
 )
 
 type releaseOptions struct {
@@ -61,7 +62,7 @@ func main() {
 	var parallelism = releaseCmd.Flag("parallelism", "Amount tasks to run concurrently").Short('p').Default("4").Int()
 	var timeout = releaseCmd.Flag("timeout", "Timeout to the entire release process").Default("30m").Duration()
 
-	app.Version(fmt.Sprintf("%v, commit %v, built at %v", version, commit, date))
+	app.Version(buildVersion(version, commit, date, builtBy))
 	app.VersionFlag.Short('v')
 	app.HelpFlag.Short('h')
 	app.UsageTemplate(static.UsageTemplate)
@@ -164,3 +165,17 @@ func loadConfig(path string) (config.Project, error) {
 	log.Warn("could not find a config file, using defaults...")
 	return config.Project{}, nil
 }
+
+func buildVersion(version, commit, date, builtBy string) string {
+	var result = fmt.Sprintf("version: %s", version)
+	if commit != "" {
+		result = fmt.Sprintf("%s\ncommit: %s", result, commit)
+	}
+	if date != "" {
+		result = fmt.Sprintf("%s\nbuilt at: %s", result, date)
+	}
+	if builtBy != "" {
+		result = fmt.Sprintf("%s\nbuilt by: %s", result, builtBy)
+	}
+	return result
+}
diff --git a/www/content/build.md b/www/content/build.md
index 8167d3cc..dc1048c5 100644
--- a/www/content/build.md
+++ b/www/content/build.md
@@ -49,7 +49,7 @@ builds:
       - ./dontoptimizeme=-N
 
     # Custom ldflags templates.
-    # Default is `-s -w -X main.version={{.Version}} -X main.commit={{.ShortCommit}} -X main.date={{.Date}}`.
+    # Default is `-s -w -X main.version={{.Version}} -X main.commit={{.ShortCommit}} -X main.date={{.Date}} -X main.builtBy=goreleaser`.
     ldflags:
      - -s -w -X main.build={{.Version}}
      - ./usemsan=-msan


Test patch:
diff --git a/internal/pipe/build/build_test.go b/internal/pipe/build/build_test.go
index cf713ce5..80bb2916 100644
--- a/internal/pipe/build/build_test.go
+++ b/internal/pipe/build/build_test.go
@@ -217,7 +217,7 @@ func TestDefaultEmptyBuild(t *testing.T) {
 	assert.Equal(t, []string{"amd64", "386"}, build.Goarch)
 	assert.Equal(t, []string{"6"}, build.Goarm)
 	assert.Len(t, build.Ldflags, 1)
-	assert.Equal(t, "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}", build.Ldflags[0])
+	assert.Equal(t, "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser", build.Ldflags[0])
 }
 
 func TestSeveralBuildsWithTheSameID(t *testing.T) {
@@ -266,7 +266,7 @@ func TestDefaultPartialBuilds(t *testing.T) {
 		assert.Equal(t, []string{"amd64", "386"}, build.Goarch)
 		assert.Equal(t, []string{"6"}, build.Goarm)
 		assert.Len(t, build.Ldflags, 1)
-		assert.Equal(t, "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}", build.Ldflags[0])
+		assert.Equal(t, "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser", build.Ldflags[0])
 	})
 	t.Run("build1", func(t *testing.T) {
 		var build = ctx.Config.Builds[1]
diff --git a/main_test.go b/main_test.go
index b81ccb7d..c5e713b5 100644
--- a/main_test.go
+++ b/main_test.go
@@ -10,6 +10,7 @@ import (
 	"github.com/goreleaser/goreleaser/internal/testlib"
 	"github.com/goreleaser/goreleaser/pkg/config"
 	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
 	yaml "gopkg.in/yaml.v2"
 )
 
@@ -181,3 +182,41 @@ release:
 `
 	createFile(t, "goreleaser.yml", yaml)
 }
+
+func TestVersion(t *testing.T) {
+	for name, tt := range map[string]struct {
+		version, commit, date, builtBy string
+		out                            string
+	}{
+		"all empty": {
+			out: "version: ",
+		},
+		"complete": {
+			version: "1.2.3",
+			date:    "12/12/12",
+			commit:  "aaaa",
+			builtBy: "me",
+			out:     "version: 1.2.3\ncommit: aaaa\nbuilt at: 12/12/12\nbuilt by: me",
+		},
+		"only version": {
+			version: "1.2.3",
+			out:     "version: 1.2.3",
+		},
+		"version and date": {
+			version: "1.2.3",
+			date:    "12/12/12",
+			out:     "version: 1.2.3\nbuilt at: 12/12/12",
+		},
+		"version, date, built by": {
+			version: "1.2.3",
+			date:    "12/12/12",
+			builtBy: "me",
+			out:     "version: 1.2.3\nbuilt at: 12/12/12\nbuilt by: me",
+		},
+	} {
+		tt := tt
+		t.Run(name, func(t *testing.T) {
+			require.Equal(t, tt.out, buildVersion(tt.version, tt.commit, tt.date, tt.builtBy))
+		})
+	}
+}