From 4fce5ba81a1174df6dee61998efb3b975f787419 Mon Sep 17 00:00:00 2001 From: Roger Peppe Date: Tue, 12 Mar 2024 16:08:31 +0000 Subject: [PATCH] internal/cueversion: convert spaces to underscore in Go version A Component/Version pair inside a User-Agent header should not contain spaces, but `runtime.Version` can return a version containing spaces when using a development version (for example "devel go1.23-2ab9218c86 2024-03-11 22:10:38 +0000"). So convert spaces to underscores before putting that string in the User-Agent header. Signed-off-by: Roger Peppe Change-Id: I03e433429db80ccadf44983b4938fbdf14b08673 Dispatch-Trailer: {"type":"trybot","CL":1178179,"patchset":2,"ref":"refs/changes/79/1178179/2","targetBranch":"master"} --- internal/cueversion/version.go | 8 +++++++- internal/cueversion/version_test.go | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/internal/cueversion/version.go b/internal/cueversion/version.go index 2676aad14..f9062d88c 100644 --- a/internal/cueversion/version.go +++ b/internal/cueversion/version.go @@ -6,6 +6,7 @@ import ( "fmt" "runtime" "runtime/debug" + "strings" "sync" ) @@ -51,5 +52,10 @@ func UserAgent(clientType string) string { if clientType == "" { clientType = "cuelang.org/go" } - return fmt.Sprintf("Cue/%s (%s) Go/%s (%s/%s)", Version(), clientType, runtime.Version(), runtime.GOOS, runtime.GOARCH) + // The Go version can contain spaces, but we don't want spaces inside + // Component/Version pair, so replace them with underscores. + // As the runtime version won't contain underscores itself, this + // is reversible. + goVersion := strings.ReplaceAll(runtime.Version(), "_", " ") + return fmt.Sprintf("Cue/%s (%s) Go/%s (%s/%s)", Version(), clientType, goVersion, runtime.GOOS, runtime.GOARCH) } diff --git a/internal/cueversion/version_test.go b/internal/cueversion/version_test.go index 020232c18..a946eb99e 100644 --- a/internal/cueversion/version_test.go +++ b/internal/cueversion/version_test.go @@ -24,7 +24,7 @@ func TestVersion(t *testing.T) { func TestUserAgent(t *testing.T) { agent := UserAgent("custom") qt.Assert(t, qt.Matches(agent, - `Cue/v[^ ]+ \(custom\) Go/go1\.[^ ]+ \([^/]+/[^/]+\)`, + `Cue/v[^ ]+ \(custom\) Go/[^ ]+ \([^/]+/[^/]+\)`, )) }