1
0
mirror of https://github.com/moby/buildkit.git synced 2025-04-18 18:04:03 +03:00

allow setting user agent products

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2023-04-27 17:39:28 +02:00
parent fe5efe0353
commit 13a8a58d75
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
3 changed files with 99 additions and 28 deletions

49
version/ua.go Normal file
View File

@ -0,0 +1,49 @@
package version
import (
"fmt"
"regexp"
"strings"
"sync"
)
var (
reRelease *regexp.Regexp
reDev *regexp.Regexp
reOnce sync.Once
uapCbs map[string]func() string
)
func UserAgent() string {
uaVersion := defaultVersion
reOnce.Do(func() {
reRelease = regexp.MustCompile(`^(v[0-9]+\.[0-9]+)\.[0-9]+$`)
reDev = regexp.MustCompile(`^(v[0-9]+\.[0-9]+)\.[0-9]+`)
})
if matches := reRelease.FindAllStringSubmatch(Version, 1); len(matches) > 0 {
uaVersion = matches[0][1]
} else if matches := reDev.FindAllStringSubmatch(Version, 1); len(matches) > 0 {
uaVersion = matches[0][1] + "-dev"
}
res := &strings.Builder{}
fmt.Fprintf(res, "buildkit/%s", uaVersion)
for pname, pver := range uapCbs {
fmt.Fprintf(res, " %s/%s", pname, pver())
}
return res.String()
}
// SetUserAgentProduct sets a callback to get the version of a product to be
// included in the User-Agent header. The callback is called every time the
// User-Agent header is generated. Caller must ensure that the callback is
// cached if it is expensive to compute.
func SetUserAgentProduct(name string, cb func() (version string)) {
if uapCbs == nil {
uapCbs = make(map[string]func() string)
}
uapCbs[name] = cb
}

50
version/ua_test.go Normal file
View File

@ -0,0 +1,50 @@
package version
import "testing"
func TestUserAgent(t *testing.T) {
cases := []struct {
name string
version string
products map[string]string
want string
}{
{
name: "dev",
version: defaultVersion,
want: "buildkit/v0.0-dev",
},
{
name: "unknown",
version: "0.0.0+unknown",
want: "buildkit/v0.0.0+unknown",
},
{
name: "release",
version: "v0.11.6",
want: "buildkit/v0.11",
},
{
name: "product",
version: "v0.11.6",
products: map[string]string{
"moby": "v24.0",
},
want: "buildkit/v0.11 moby/v24.0",
},
}
for _, tt := range cases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
Version = tt.version
for pname, pver := range tt.products {
SetUserAgentProduct(pname, func() string {
return pver
})
}
if g, w := UserAgent(), tt.want; g != w {
t.Fatalf("got: %q\nwant: %q", g, w)
}
})
}
}

View File

@ -17,11 +17,6 @@
package version
import (
"regexp"
"sync"
)
const (
defaultVersion = "v0.0.0+unknown"
)
@ -37,26 +32,3 @@ var (
// the program at linking time.
Revision = ""
)
var (
reRelease *regexp.Regexp
reDev *regexp.Regexp
reOnce sync.Once
)
func UserAgent() string {
uaVersion := defaultVersion
reOnce.Do(func() {
reRelease = regexp.MustCompile(`^(v[0-9]+\.[0-9]+)\.[0-9]+$`)
reDev = regexp.MustCompile(`^(v[0-9]+\.[0-9]+)\.[0-9]+`)
})
if matches := reRelease.FindAllStringSubmatch(Version, 1); len(matches) > 0 {
uaVersion = matches[0][1]
} else if matches := reDev.FindAllStringSubmatch(Version, 1); len(matches) > 0 {
uaVersion = matches[0][1] + "-dev"
}
return "buildkit/" + uaVersion
}