-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(changelog): stage buildinfo go files
- Loading branch information
1 parent
85b778f
commit e6f7a8c
Showing
2 changed files
with
149 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"runtime" | ||
"strings" | ||
"text/template" | ||
"time" | ||
) | ||
|
||
const versionInfoTmpl = ` | ||
{{.Program}}, version {{.Version}} (revision: {{.Commit}}) | ||
build date: {{.Date}} | ||
go version: {{.GoVersion}} | ||
` | ||
|
||
// BuildInfo contains information about build | ||
// like version tag, commit and build date. | ||
type BuildInfo struct { | ||
version string | ||
date time.Time | ||
commit string | ||
runtimeVersion func() string | ||
} | ||
|
||
// NewBuildInfo creates a new BuildInfo. The date has to be in RFC3329 format | ||
// and the commit hash has to be at leas 8 characters long. | ||
func NewBuildInfo(version, date, commit string) (*BuildInfo, error) { | ||
if version == "" { | ||
return nil, errors.New("version cannot be empty") | ||
} | ||
|
||
d, err := time.Parse(time.RFC3339, date) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse date %s: %w", date, err) | ||
} | ||
|
||
if len(commit) < 8 { | ||
return nil, errors.New("commit hash has to be at least 8 characters long") | ||
} | ||
|
||
return &BuildInfo{ | ||
version: version, | ||
date: d, | ||
commit: commit, | ||
runtimeVersion: runtime.Version, | ||
}, nil | ||
} | ||
|
||
// Version returns the version information as string. | ||
func (b BuildInfo) Version(program string) string { | ||
t := template.Must(template.New("version").Parse(versionInfoTmpl)) | ||
|
||
data := struct { | ||
Version string | ||
Date string | ||
Commit string | ||
GoVersion string | ||
Program string | ||
}{ | ||
Version: b.version, | ||
Date: b.date.String(), | ||
Commit: b.commit[:8], | ||
GoVersion: b.runtimeVersion(), | ||
Program: program, | ||
} | ||
|
||
var buf bytes.Buffer | ||
if err := t.ExecuteTemplate(&buf, "version", data); err != nil { | ||
panic(err) | ||
} | ||
|
||
return strings.TrimSpace(buf.String()) | ||
} |
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,73 @@ | ||
package cmd | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"gotest.tools/assert" | ||
) | ||
|
||
func TestBuildInfo(t *testing.T) { | ||
var tt = []struct { | ||
name string | ||
version string | ||
commit string | ||
date string | ||
expected string | ||
expectsError bool | ||
}{ | ||
{ | ||
"ok", | ||
"1.0.0", | ||
"4a8804f18ea7560fe45fecaa052605b1a8a66fe8", | ||
"2021-01-05T08:08:54+01:00", | ||
`prog, version 1.0.0 (revision: 4a8804f1) | ||
build date: 2021-01-05 08:08:54 +0100 CET | ||
go version: go1.15.6`, | ||
false, | ||
}, | ||
{ | ||
"invalid - commit to short", | ||
"1.0.0", | ||
"123", | ||
"2021-01-05T08:08:54+01:00", | ||
"", | ||
true, | ||
}, | ||
{ | ||
"invalid - empty version", | ||
"", | ||
"4a8804f18ea7560fe45fecaa052605b1a8a66fe8", | ||
"2021-01-05T08:08:54+01:00", | ||
"", | ||
true, | ||
}, | ||
{ | ||
"invalid - wrong date format", | ||
"1.0.0", | ||
"4a8804f18ea7560fe45fecaa052605b1a8a66fe8", | ||
"2021-01-05T08:08:54", | ||
"", | ||
true, | ||
}, | ||
} | ||
|
||
for i := range tt { | ||
tc := tt[i] | ||
t.Run(tc.name, func(t *testing.T) { | ||
b, err := NewBuildInfo(tc.version, tc.date, tc.commit) | ||
if tc.expectsError { | ||
require.Error(t, err) | ||
return | ||
} | ||
b.runtimeVersion = runtimeVersionMock | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, tc.expected, b.Version("prog")) | ||
}) | ||
} | ||
} | ||
|
||
func runtimeVersionMock() string { | ||
return "go1.15.6" | ||
} |