Skip to content

Commit 37e2cf2

Browse files
authored
[CDTOOL-1097] feat(env): Add environment variable for extending the UserAgent string. (#1502)
The environment variable FASTLY_USER_AGENT_EXTENSION can be used to supply a string which will be inserted into the User-Agent header in requests sent to the Fastly API; the existing strings representing the Fastly CLI version and go-fastly version (when applicable) will still be present. All Submissions: * [X] Have you followed the guidelines in our Contributing document? * [X] Have you checked to ensure there aren't other open [Pull Requests](https://github.com/fastly/cli/pulls) for the same update/change? Test - before: ``` kpfleming@kpfleming:~/src/fastly/cli$ FASTLY_DEBUG_MODE=true ./fastly whoami http.Request (dump): "GET /verify HTTP/1.1\r\nHost: api.fastly.com\r\nAccept: application/json\r\nFastly-Key: <redacted>\r\nUser-Agent: FastlyCLI/v0.0.0-unknown\r\n\r\n" ``` Test - after: ``` kpfleming@kpfleming:~/src/fastly/cli$ FASTLY_USER_AGENT_EXTENSION=test1234 FASTLY_DEBUG_MODE=true ./fastly whoami http.Request (dump): "GET /verify HTTP/1.1\r\nHost: api.fastly.com\r\nAccept: application/json\r\nFastly-Key: <redacted>\r\nUser-Agent: FastlyCLI/v0.0.0-unknown, test1234\r\n\r\n" ```
1 parent d8235ef commit 37e2cf2

File tree

6 files changed

+31
-14
lines changed

6 files changed

+31
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
### Enhancements:
88

9+
- feat(env): Add environment variable for extending the UserAgent string. ([#1502](https://github.com/fastly/cli/pull/1502))
10+
911
### Bug fixes:
1012
- fix(sso): Ensure that OPTIONS requests sent by browsers do not break SSO authentication. ([#1496](https://github.com/fastly/cli/pull/1496))
1113

pkg/app/run.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/fastly/cli/pkg/revision"
3939
"github.com/fastly/cli/pkg/sync"
4040
"github.com/fastly/cli/pkg/text"
41+
"github.com/fastly/cli/pkg/useragent"
4142
)
4243

4344
// Run kick starts the CLI application.
@@ -152,6 +153,18 @@ var Init = func(args []string, stdin io.Reader) (*global.Data, error) {
152153
}),
153154
}
154155

156+
// If a UserAgent extension has been set in the environment,
157+
// apply it
158+
if e.UserAgentExtension != "" {
159+
useragent.SetExtension(e.UserAgentExtension)
160+
}
161+
// Override the go-fastly UserAgent value by prepending the CLI version.
162+
//
163+
// Results in a header similar to:
164+
// User-Agent: FastlyCLI/v11.3.0, FastlyGo/10.5.0 (+github.com/fastly/go-fastly; go1.24.3)
165+
// (with any extension supplied above between the FastlyCLI and FastlyGo values)
166+
fastly.UserAgent = fmt.Sprintf("%s, %s", useragent.Name, fastly.UserAgent)
167+
155168
return &global.Data{
156169
APIClientFactory: factory,
157170
Args: args,

pkg/commands/version/root.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,12 @@ import (
88
"strings"
99
"time"
1010

11-
"github.com/fastly/go-fastly/v10/fastly"
12-
1311
"github.com/fastly/cli/pkg/argparser"
1412
"github.com/fastly/cli/pkg/github"
1513
"github.com/fastly/cli/pkg/global"
1614
"github.com/fastly/cli/pkg/revision"
17-
"github.com/fastly/cli/pkg/useragent"
1815
)
1916

20-
func init() {
21-
// Override the go-fastly UserAgent value by prepending the CLI version.
22-
//
23-
// Results in a header similar too:
24-
// User-Agent: FastlyCLI/0.1.0, FastlyGo/1.5.0 (1.13.0)
25-
fastly.UserAgent = fmt.Sprintf("%s, %s", useragent.Name, fastly.UserAgent)
26-
}
27-
2817
// RootCommand is the parent command for all subcommands in this package.
2918
// It should be installed under the primary root command.
3019
type RootCommand struct {

pkg/config/config.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,12 @@ type Environment struct {
458458
// UseSSO indicates if user wants to use SSO/OAuth token flow.
459459
// 1: enabled, 0: disabled.
460460
UseSSO string
461-
// WasmMetadataDisable is the env var we look in to disable all data
462-
// collection related to a Wasm binary.
463-
// Set to "true" to disable all forms of data collection.
461+
// UserAgentExtension is the string we'll add to the UserAgent
462+
// we send in API requests.
463+
UserAgentExtension string
464+
// WasmMetadataDisable is the env var we look in to disable
465+
// all data collection related to a Wasm binary. Set to
466+
// "true" to disable all forms of data collection.
464467
WasmMetadataDisable string
465468
}
466469

@@ -471,6 +474,7 @@ func (e *Environment) Read(state map[string]string) {
471474
e.APIToken = state[env.APIToken]
472475
e.DebugMode = state[env.DebugMode]
473476
e.UseSSO = state[env.UseSSO]
477+
e.UserAgentExtension = state[env.UserAgentExtension]
474478
e.WasmMetadataDisable = state[env.WasmMetadataDisable]
475479
}
476480

pkg/env/env.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ const (
4040
// Assigned value should be a boolean 1/0 (enable/disable).
4141
UseSSO = "FASTLY_USE_SSO"
4242

43+
// UserAgentExtension informs the CLI of an additional string
44+
// which should be added to the UserAgent included in
45+
// requests made by the CLI.
46+
UserAgentExtension = "FASTLY_USER_AGENT_EXTENSION"
47+
4348
// WasmMetadataDisable is the env var we look in to disable all data
4449
// collection related to a Wasm binary.
4550
// Set to "true" to disable all forms of data collection.

pkg/useragent/useragent.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ import (
88

99
// Name is the user agent which we report in all HTTP requests.
1010
var Name = fmt.Sprintf("%s/%s", "FastlyCLI", revision.AppVersion)
11+
12+
func SetExtension(extension string) {
13+
Name = fmt.Sprintf("%s, %s", Name, extension)
14+
}

0 commit comments

Comments
 (0)