diff --git a/internal/libinfo/lib_info.go b/internal/libinfo/lib_info.go index 74c65e8..2f33780 100644 --- a/internal/libinfo/lib_info.go +++ b/internal/libinfo/lib_info.go @@ -7,9 +7,9 @@ Released under MIT license. package libinfo func UserAgent() string { - return LibName + "/" + GetLibVersion() + return libShortName + "/" + GetLibVersion() } func LogPrefix() string { - return "[" + LibName + "/" + GetLibVersion() + "] " + return "[" + libShortName + "/" + GetLibVersion() + "] " } diff --git a/internal/libinfo/version.go b/internal/libinfo/version.go index cefbc8d..066a15a 100644 --- a/internal/libinfo/version.go +++ b/internal/libinfo/version.go @@ -7,31 +7,46 @@ Released under MIT license. package libinfo import ( + "debug/buildinfo" + "regexp" "sync" "runtime/debug" ) -const LibName = "go-authkit" +const libShortName = "go-authkit" -const libPath = "github.com/acronis/" + LibName +const moduleName = "github.com/acronis/" + libShortName var libVersion string var libVersionOnce sync.Once +func GetLibVersion() string { + libVersionOnce.Do(initLibVersion) + return libVersion +} + func initLibVersion() { - if buildInfo, ok := debug.ReadBuildInfo(); ok && buildInfo != nil { - for _, dep := range buildInfo.Deps { - if dep.Path == libPath { - libVersion = dep.Version - return - } - } + if buildInfo, ok := debug.ReadBuildInfo(); ok { + libVersion = extractLibVersion(buildInfo, moduleName) + } + if libVersion == "" { + libVersion = "v0.0.0" } - libVersion = "v0.0.0" } -func GetLibVersion() string { - libVersionOnce.Do(initLibVersion) - return libVersion +func extractLibVersion(buildInfo *buildinfo.BuildInfo, modName string) string { + if buildInfo == nil { + return "" + } + re, err := regexp.Compile(`^` + regexp.QuoteMeta(modName) + `(/v[0-9]+)?$`) + if err != nil { + return "" // should never happen + } + for _, dep := range buildInfo.Deps { + if re.MatchString(dep.Path) { + return dep.Version + } + } + return "" } diff --git a/internal/libinfo/version_test.go b/internal/libinfo/version_test.go new file mode 100644 index 0000000..9ba75fc --- /dev/null +++ b/internal/libinfo/version_test.go @@ -0,0 +1,75 @@ +/* +Copyright © 2024 Acronis International GmbH. + +Released under MIT license. +*/ + +package libinfo + +import ( + "debug/buildinfo" + "runtime/debug" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestExtractLibVersion(t *testing.T) { + tests := []struct { + name string + buildInfo *buildinfo.BuildInfo + moduleName string + expectedVer string + }{ + { + name: "module found", + buildInfo: &buildinfo.BuildInfo{ + Deps: []*debug.Module{ + {Path: "github.com/acronis/go-authkit", Version: "v1.2.3"}, + }, + }, + moduleName: "github.com/acronis/go-authkit", + expectedVer: "v1.2.3", + }, + { + name: "module found, v2", + buildInfo: &buildinfo.BuildInfo{ + Deps: []*debug.Module{ + {Path: "github.com/acronis/go-authkit/v2", Version: "v2.0.0"}, + }, + }, + moduleName: "github.com/acronis/go-authkit", + expectedVer: "v2.0.0", + }, + { + name: "module not found", + buildInfo: &buildinfo.BuildInfo{ + Deps: []*debug.Module{ + {Path: "github.com/other/module", Version: "v1.0.0"}, + }, + }, + moduleName: "github.com/acronis/go-authkit", + expectedVer: "", + }, + { + name: "empty deps", + buildInfo: &buildinfo.BuildInfo{ + Deps: []*debug.Module{}, + }, + moduleName: "github.com/acronis/go-authkit", + expectedVer: "", + }, + { + name: "nil build info", + buildInfo: nil, + moduleName: "github.com/acronis/go-authkit", + expectedVer: "", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := extractLibVersion(tt.buildInfo, tt.moduleName) + require.Equal(t, tt.expectedVer, got) + }) + } +}