Repository: goreleaser/goreleaser
Pull request #3962 (2023-04-30 15:40:34)

feat: new --version output
using the same output as most cloud-native Go-written CLI's use, i.e. using [sigs.k8s.io/release-utils](https://github.com/kubernetes-sigs/release-utils).


cloudflare-pages[bot]: ## Deploying with &nbsp;<a href="https://pages.dev"><img alt="Cloudflare Pages" src="https://user-images.githubusercontent.com/23264/106598434-9e719e00-654f-11eb-9e59-6167043cfa01.png" width="16"></a> &nbsp;Cloudflare Pages

<table><tr><td><strong>Latest commit:</strong> </td><td>
<code>fcd1df4</code>
</td></tr>
<tr><td><strong>Status:</strong></td><td>⚡️&nbsp; Build in progress...</td></tr>
</table>

[View logs](https://dash.cloudflare.com/?to=/:account/pages/view/goreleaser/9a608da0-6e16-45b0-bcf1-178f0d412025)


Patch:
diff --git a/art.txt b/art.txt
new file mode 100644
index 00000000..8dbc9969
--- /dev/null
+++ b/art.txt
@@ -0,0 +1,5 @@
+  ____       ____      _
+ / ___| ___ |  _ \ ___| | ___  __ _ ___  ___ _ __
+| |  _ / _ \| |_) / _ \ |/ _ \/ _` / __|/ _ \ '__|
+| |_| | (_) |  _ <  __/ |  __/ (_| \__ \  __/ |
+ \____|\___/|_| \_\___|_|\___|\__,_|___/\___|_|
diff --git a/cmd/root.go b/cmd/root.go
index c8eb63aa..76cd8eef 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -76,6 +76,7 @@ Check out our website for more information, examples and documentation: https://
 			}
 		},
 	}
+	cmd.SetVersionTemplate("{{.Version}}")
 
 	cmd.PersistentFlags().BoolVar(&root.debug, "debug", false, "Enable debug mode")
 	cmd.AddCommand(
diff --git a/go.mod b/go.mod
index 1ac374fe..ed46fe1d 100644
--- a/go.mod
+++ b/go.mod
@@ -11,6 +11,7 @@ require (
 	github.com/caarlos0/env/v8 v8.0.0
 	github.com/caarlos0/go-reddit/v3 v3.0.1
 	github.com/caarlos0/go-shellwords v1.0.12
+	github.com/caarlos0/go-version v0.1.1
 	github.com/caarlos0/log v0.2.2
 	github.com/charmbracelet/keygen v0.4.2
 	github.com/charmbracelet/lipgloss v0.7.1
diff --git a/go.sum b/go.sum
index 04d2fae0..297ac985 100644
--- a/go.sum
+++ b/go.sum
@@ -702,6 +702,8 @@ github.com/caarlos0/go-reddit/v3 v3.0.1/go.mod h1:QlwgmG5SAqxMeQvg/A2dD1x9cIZCO5
 github.com/caarlos0/go-rpmutils v0.2.1-0.20211112020245-2cd62ff89b11 h1:IRrDwVlWQr6kS1U8/EtyA1+EHcc4yl8pndcqXWrEamg=
 github.com/caarlos0/go-shellwords v1.0.12 h1:HWrUnu6lGbWfrDcFiHcZiwOLzHWjjrPVehULaTFgPp8=
 github.com/caarlos0/go-shellwords v1.0.12/go.mod h1:bYeeX1GrTLPl5cAMYEzdm272qdsQAZiaHgeF0KTk1Gw=
+github.com/caarlos0/go-version v0.1.1 h1:1bikKHkGGVIIxqCmufhSSs3hpBScgHGacrvsi8FuIfc=
+github.com/caarlos0/go-version v0.1.1/go.mod h1:Ze5Qx4TsBBi5FyrSKVg1Ibc44KGV/llAaKGp86oTwZ0=
 github.com/caarlos0/log v0.2.2 h1:Rier7889+dTKHjvwf0cMNu8DjMF+/wCYC7wrPj9fXFk=
 github.com/caarlos0/log v0.2.2/go.mod h1:IbSeDN+hKHdOwE6t2z9i2rcraz+r6N1XZkRHE4BAKm0=
 github.com/caarlos0/sshmarshal v0.1.0 h1:zTCZrDORFfWh526Tsb7vCm3+Yg/SfW/Ub8aQDeosk0I=
diff --git a/internal/pipeline/pipeline.go b/internal/pipeline/pipeline.go
index 01a9ff45..4f619020 100644
--- a/internal/pipeline/pipeline.go
+++ b/internal/pipeline/pipeline.go
@@ -78,7 +78,11 @@ var BuildPipeline = []Piper{
 
 // BuildCmdPipeline is the pipeline run by goreleaser build.
 // nolint:gochecknoglobals
-var BuildCmdPipeline = append(BuildPipeline, metadata.Pipe{}, reportsizes.Pipe{})
+var BuildCmdPipeline = append(
+	BuildPipeline,
+	reportsizes.Pipe{},
+	metadata.Pipe{},
+)
 
 // Pipeline contains all pipe implementations in order.
 // nolint: gochecknoglobals
diff --git a/main.go b/main.go
index ec1d7995..eb4192cf 100644
--- a/main.go
+++ b/main.go
@@ -1,11 +1,11 @@
 package main
 
 import (
-	"fmt"
 	"os"
-	"runtime"
-	"runtime/debug"
 
+	_ "embed"
+
+	goversion "github.com/caarlos0/go-version"
 	"github.com/caarlos0/log"
 	"github.com/charmbracelet/lipgloss"
 	"github.com/goreleaser/goreleaser/cmd"
@@ -15,7 +15,7 @@ import (
 
 // nolint: gochecknoglobals
 var (
-	version = "dev"
+	version = ""
 	commit  = ""
 	date    = ""
 	builtBy = ""
@@ -36,28 +36,34 @@ func init() {
 
 func main() {
 	cmd.Execute(
-		buildVersion(version, commit, date, builtBy),
+		buildVersion(version, commit, date, builtBy).String(),
 		os.Exit,
 		os.Args[1:],
 	)
 }
 
-const website = "\n\nhttps://goreleaser.com"
+const website = "https://goreleaser.com"
 
-func buildVersion(version, commit, date, builtBy string) string {
-	result := 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)
-	}
-	result = fmt.Sprintf("%s\ngoos: %s\ngoarch: %s", result, runtime.GOOS, runtime.GOARCH)
-	if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" {
-		result = fmt.Sprintf("%s\nmodule version: %s, checksum: %s", result, info.Main.Version, info.Main.Sum)
-	}
-	return result + website
+//go:embed art.txt
+var asciiArt string
+
+func buildVersion(version, commit, date, builtBy string) goversion.Info {
+	return goversion.GetVersionInfo(
+		goversion.WithAppDetails("goreleaser", "Deliver Go Binaries as fast and easily as possible", website),
+		goversion.WithASCIIName(asciiArt),
+		func(i *goversion.Info) {
+			if commit != "" {
+				i.GitCommit = commit
+			}
+			if date != "" {
+				i.BuildDate = date
+			}
+			if version != "" {
+				i.GitVersion = version
+			}
+			if builtBy != "" {
+				i.BuiltBy = builtBy
+			}
+		},
+	)
 }


Test patch:
diff --git a/cmd/root_test.go b/cmd/root_test.go
index 13faac65..fb331f1c 100644
--- a/cmd/root_test.go
+++ b/cmd/root_test.go
@@ -28,7 +28,7 @@ func TestRootCmdVersion(t *testing.T) {
 	cmd.SetOut(&b)
 	cmd.SetArgs([]string{"-v"})
 	require.NoError(t, cmd.Execute())
-	require.Equal(t, "goreleaser version 1.2.3\n", b.String())
+	require.Equal(t, "1.2.3", b.String())
 	require.Equal(t, 0, mem.code)
 }
 
diff --git a/main_test.go b/main_test.go
index 8fc25375..d55f601e 100644
--- a/main_test.go
+++ b/main_test.go
@@ -1,48 +1,43 @@
 package main
 
 import (
-	"fmt"
-	"runtime"
 	"testing"
 
+	"github.com/goreleaser/goreleaser/internal/golden"
 	"github.com/stretchr/testify/require"
 )
 
 func TestVersion(t *testing.T) {
-	os := fmt.Sprintf("\ngoos: %s\ngoarch: %s", runtime.GOOS, runtime.GOARCH)
 	for name, tt := range map[string]struct {
 		version, commit, date, builtBy string
-		out                            string
 	}{
-		"all empty": {
-			out: os + website,
-		},
+		"all empty": {},
 		"complete": {
 			version: "1.2.3",
 			date:    "12/12/12",
 			commit:  "aaaa",
 			builtBy: "me",
-			out:     "1.2.3\ncommit: aaaa\nbuilt at: 12/12/12\nbuilt by: me" + os + website,
 		},
 		"only version": {
 			version: "1.2.3",
-			out:     "1.2.3" + os + website,
 		},
 		"version and date": {
 			version: "1.2.3",
 			date:    "12/12/12",
-			out:     "1.2.3\nbuilt at: 12/12/12" + os + website,
 		},
 		"version, date, built by": {
 			version: "1.2.3",
 			date:    "12/12/12",
 			builtBy: "me",
-			out:     "1.2.3\nbuilt at: 12/12/12\nbuilt by: me" + os + website,
 		},
 	} {
 		tt := tt
 		t.Run(name, func(t *testing.T) {
-			require.Equal(t, tt.out, buildVersion(tt.version, tt.commit, tt.date, tt.builtBy))
+			v := buildVersion(tt.version, tt.commit, tt.date, tt.builtBy)
+			out, err := v.JSONString()
+			require.NoError(t, err)
+
+			golden.RequireEqualJSON(t, []byte(out))
 		})
 	}
 }
diff --git a/testdata/TestVersion/all_empty.json.golden b/testdata/TestVersion/all_empty.json.golden
new file mode 100644
index 00000000..7eb7deae
--- /dev/null
+++ b/testdata/TestVersion/all_empty.json.golden
@@ -0,0 +1,11 @@
+{
+  "gitVersion": "devel",
+  "moduleCheksum": "unknown",
+  "gitCommit": "unknown",
+  "gitTreeState": "unknown",
+  "buildDate": "unknown",
+  "builtBy": "unknown",
+  "goVersion": "go1.20.3",
+  "compiler": "gc",
+  "platform": "linux/amd64"
+}
\ No newline at end of file
diff --git a/testdata/TestVersion/complete.json.golden b/testdata/TestVersion/complete.json.golden
new file mode 100644
index 00000000..04ffb13b
--- /dev/null
+++ b/testdata/TestVersion/complete.json.golden
@@ -0,0 +1,11 @@
+{
+  "gitVersion": "1.2.3",
+  "moduleCheksum": "unknown",
+  "gitCommit": "aaaa",
+  "gitTreeState": "unknown",
+  "buildDate": "12/12/12",
+  "builtBy": "me",
+  "goVersion": "go1.20.3",
+  "compiler": "gc",
+  "platform": "linux/amd64"
+}
\ No newline at end of file
diff --git a/testdata/TestVersion/only_version.json.golden b/testdata/TestVersion/only_version.json.golden
new file mode 100644
index 00000000..2bf10d04
--- /dev/null
+++ b/testdata/TestVersion/only_version.json.golden
@@ -0,0 +1,11 @@
+{
+  "gitVersion": "1.2.3",
+  "moduleCheksum": "unknown",
+  "gitCommit": "unknown",
+  "gitTreeState": "unknown",
+  "buildDate": "unknown",
+  "builtBy": "unknown",
+  "goVersion": "go1.20.3",
+  "compiler": "gc",
+  "platform": "linux/amd64"
+}
\ No newline at end of file
diff --git a/testdata/TestVersion/version,_date,_built_by.json.golden b/testdata/TestVersion/version,_date,_built_by.json.golden
new file mode 100644
index 00000000..14446c0b
--- /dev/null
+++ b/testdata/TestVersion/version,_date,_built_by.json.golden
@@ -0,0 +1,11 @@
+{
+  "gitVersion": "1.2.3",
+  "moduleCheksum": "unknown",
+  "gitCommit": "unknown",
+  "gitTreeState": "unknown",
+  "buildDate": "12/12/12",
+  "builtBy": "me",
+  "goVersion": "go1.20.3",
+  "compiler": "gc",
+  "platform": "linux/amd64"
+}
\ No newline at end of file
diff --git a/testdata/TestVersion/version_and_date.json.golden b/testdata/TestVersion/version_and_date.json.golden
new file mode 100644
index 00000000..ff2ce8e3
--- /dev/null
+++ b/testdata/TestVersion/version_and_date.json.golden
@@ -0,0 +1,11 @@
+{
+  "gitVersion": "1.2.3",
+  "moduleCheksum": "unknown",
+  "gitCommit": "unknown",
+  "gitTreeState": "unknown",
+  "buildDate": "12/12/12",
+  "builtBy": "unknown",
+  "goVersion": "go1.20.3",
+  "compiler": "gc",
+  "platform": "linux/amd64"
+}
\ No newline at end of file