dockerversion: remove insertUpstreamUserAgent()

It was not really "inserting" anything, just formatting and appending.
Simplify this by changing this in to a `getUpstreamUserAgent()` function
which returns the upstream User-Agent (if any) into a `UpstreamClient()`.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-06-01 13:50:30 +02:00
parent ff40d2d787
commit 9a1f2e6d7c
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -18,11 +18,11 @@ type UAStringKey struct{}
// //
// [docker client's UA] UpstreamClient([upstream client's UA]) // [docker client's UA] UpstreamClient([upstream client's UA])
func DockerUserAgent(ctx context.Context) string { func DockerUserAgent(ctx context.Context) string {
daemonUA := getDaemonUserAgent() ua := getDaemonUserAgent()
if upstreamUA := getUserAgentFromContext(ctx); len(upstreamUA) > 0 { if upstreamUA := getUpstreamUserAgent(ctx); upstreamUA != "" {
return insertUpstreamUserAgent(upstreamUA, daemonUA) ua += " " + upstreamUA
} }
return daemonUA return ua
} }
var ( var (
@ -57,16 +57,23 @@ func getDaemonUserAgent() string {
return daemonUA return daemonUA
} }
// getUserAgentFromContext returns the previously saved user-agent context stored in ctx, if one exists // getUpstreamUserAgent returns the previously saved user-agent context stored
func getUserAgentFromContext(ctx context.Context) string { // in ctx, if one exists, and formats it as:
//
// UpstreamClient(<upstream user agent string>)
//
// It returns an empty string if no user-agent is present in the context.
func getUpstreamUserAgent(ctx context.Context) string {
var upstreamUA string var upstreamUA string
if ctx != nil { if ctx != nil {
var ki interface{} = ctx.Value(UAStringKey{}) if ki := ctx.Value(UAStringKey{}); ki != nil {
if ki != nil {
upstreamUA = ctx.Value(UAStringKey{}).(string) upstreamUA = ctx.Value(UAStringKey{}).(string)
} }
} }
return upstreamUA if upstreamUA == "" {
return ""
}
return fmt.Sprintf("UpstreamClient(%s)", escapeStr(upstreamUA))
} }
const charsToEscape = `();\` const charsToEscape = `();\`
@ -89,11 +96,3 @@ func escapeStr(s string) string {
} }
return ret return ret
} }
// insertUpstreamUserAgent adds the upstream client useragent to create a user-agent
// string of the form:
//
// $dockerUA UpstreamClient($upstreamUA)
func insertUpstreamUserAgent(upstreamUA string, dockerUA string) string {
return fmt.Sprintf("%s UpstreamClient(%s)", dockerUA, escapeStr(upstreamUA))
}