Skip to content

Commit

Permalink
fix(changelog): stage buildinfo go files
Browse files Browse the repository at this point in the history
  • Loading branch information
zbindenren committed Jan 5, 2021
1 parent 85b778f commit e6f7a8c
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
76 changes: 76 additions & 0 deletions internal/cmd/buildinfo.go
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())
}
73 changes: 73 additions & 0 deletions internal/cmd/buildinfo_test.go
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"
}

0 comments on commit e6f7a8c

Please sign in to comment.