cli/command: add WithUserAgent option

Add support to the `cli/command` package to accept a custom User
Agent to pass to the underlying client.

This is used as the `UpstreamClient` portion of the `User-Agent`
when the Moby daemon makes requests.

For example, pushing and pulling images with Compose might result
in the registry seeing a `User-Agent` value of:

```
docker/24.0.7 go/go1.20.10 git-commit/311b9ff kernel/6.5.13-linuxkit os/linux arch/arm64 UpstreamClient(docker-cli-plugin-compose/v2.24.0)
```

Signed-off-by: Milas Bowman <milas.bowman@docker.com>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Milas Bowman
2023-09-20 14:36:55 -04:00
committed by Sebastiaan van Stijn
parent 8fbb70ae56
commit 048e931b42
4 changed files with 44 additions and 5 deletions

View File

@ -3,6 +3,7 @@ package command
import (
"context"
"encoding/csv"
"errors"
"fmt"
"io"
"net/http"
@ -236,3 +237,14 @@ func withCustomHeadersFromEnv() (client.Opt, error) {
// see https://github.com/docker/cli/pull/5098#issuecomment-2147403871 (when updating, also update the WARNING in the function and env-var GoDoc)
return client.WithHTTPHeaders(env), nil
}
// WithUserAgent configures the User-Agent string for cli HTTP requests.
func WithUserAgent(userAgent string) CLIOption {
return func(cli *DockerCli) error {
if userAgent == "" {
return errors.New("user agent cannot be blank")
}
cli.userAgent = userAgent
return nil
}
}