mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-01-26 04:22:05 +02:00
feat: improved version command (#1028)
* feat: improved version command * fix: typo * test: fixed failing tests
This commit is contained in:
parent
5f825c5393
commit
86c5b93260
@ -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)
|
||||
|
@ -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]
|
||||
|
21
main.go
21
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
|
||||
}
|
||||
|
39
main_test.go
39
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))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user