initial commit
Format / gofmt (push) Failing after 27s
CI / Build (push) Successful in 51s
CI / Go Tests (push) Failing after 21s

This commit is contained in:
2026-06-06 07:54:44 -05:00
commit 1cc94f2c99
68 changed files with 14615 additions and 0 deletions
+82
View File
@@ -0,0 +1,82 @@
package version
import (
"strconv"
"strings"
)
// Version and Commit are injected separately at link time via -ldflags (see Makefile / Dockerfile).
// Version is the exact git tag on HEAD (empty if untagged). Commit is the short git hash (empty if unavailable).
var (
Version = ""
Commit = ""
)
func resolve() string {
if isSemverTag(Version) {
return Version
}
if Commit != "" {
return Commit
}
return "dev"
}
func String() string {
return resolve()
}
func Display() string {
return resolve()
}
// isSemverTag reports whether tag looks like semver (optional "v" + major.minor.patch, pre-release suffix allowed).
func isSemverTag(tag string) bool {
if tag == "" {
return false
}
core := tag
if i := strings.IndexAny(core, "-+"); i >= 0 {
core = core[:i]
}
core = strings.TrimPrefix(core, "v")
parts := strings.Split(core, ".")
if len(parts) != 3 {
return false
}
for _, p := range parts {
if p == "" {
return false
}
for _, c := range p {
if c < '0' || c > '9' {
return false
}
}
}
return true
}
// Parts splits the git tag (Version) into major/minor/revision for NPM-style API responses.
// Returns zeros when there is no semver tag (commit-only or dev builds).
func Parts() (major, minor, revision int) {
if !isSemverTag(Version) {
return 0, 0, 0
}
core := Version
if i := strings.IndexAny(core, "-+"); i >= 0 {
core = core[:i]
}
core = strings.TrimPrefix(core, "v")
parts := strings.Split(core, ".")
if len(parts) != 3 {
return 0, 0, 0
}
major, err0 := strconv.Atoi(parts[0])
minor, err1 := strconv.Atoi(parts[1])
revision, err2 := strconv.Atoi(parts[2])
if err0 != nil || err1 != nil || err2 != nil {
return 0, 0, 0
}
return major, minor, revision
}
+61
View File
@@ -0,0 +1,61 @@
package version
import "testing"
func TestResolve(t *testing.T) {
tests := []struct {
tag, commit, want string
}{
{"v1.0.0", "abc1234", "v1.0.0"},
{"1.2.3", "abc1234", "1.2.3"},
{"v1.0.0-rc.1", "abc1234", "v1.0.0-rc.1"},
{"release-candidate", "abc1234", "abc1234"},
{"latest", "abc1234", "abc1234"},
{"v1.2", "abc1234", "abc1234"},
{"", "abc1234", "abc1234"},
{"", "", "dev"},
}
for _, tc := range tests {
Version = tc.tag
Commit = tc.commit
if got := String(); got != tc.want {
t.Errorf("resolve(tag=%q, commit=%q) = %q; want %q", tc.tag, tc.commit, got, tc.want)
}
}
}
func TestIsSemverTag(t *testing.T) {
ok := []string{"v1.0.0", "1.2.3", "v0.0.1", "2.10.99", "v1.0.0-rc.1"}
bad := []string{"", "latest", "release-1", "v1.2", "1.2", "abc1234", "v1.2.3.4"}
for _, tag := range ok {
if !isSemverTag(tag) {
t.Errorf("isSemverTag(%q) = false; want true", tag)
}
}
for _, tag := range bad {
if isSemverTag(tag) {
t.Errorf("isSemverTag(%q) = true; want false", tag)
}
}
}
func TestParts(t *testing.T) {
tests := []struct {
tag string
major, minor, rev int
}{
{"v1.2.3", 1, 2, 3},
{"1.2.3", 1, 2, 3},
{"v1.2.3-rc1", 1, 2, 3},
{"", 0, 0, 0},
{"abc1234", 0, 0, 0},
{"1.2", 0, 0, 0},
}
for _, tc := range tests {
Version = tc.tag
major, minor, rev := Parts()
if major != tc.major || minor != tc.minor || rev != tc.rev {
t.Errorf("Parts(%q) = %d,%d,%d; want %d,%d,%d", tc.tag, major, minor, rev, tc.major, tc.minor, tc.rev)
}
}
}