Skip to content

Commit 52dd033

Browse files
User agent as a build option
1 parent 1f209f2 commit 52dd033

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

multiapps_plugin.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import (
1717
// Version is the version of the CLI plugin. It is injected on linking time.
1818
var Version string = "0.0.0"
1919

20+
// MultiappsUserAgentSuffixOption is the default user agent suffix option. It is injected on linking time.
21+
var MultiappsUserAgentSuffixOption string = ""
22+
2023
// MultiappsPlugin represents a cf CLI plugin for executing operations on MTAs
2124
type MultiappsPlugin struct{}
2225

@@ -50,6 +53,7 @@ func (p *MultiappsPlugin) Run(cliConnection plugin.CliConnection, args []string)
5053
}
5154
util.SetCfCliVersion(strings.Join(versionOutput, " "))
5255
util.SetPluginVersion(Version)
56+
util.SetUserAgentSuffixOption(MultiappsUserAgentSuffixOption)
5357
command.Initialize(command.GetPluginCommand().Name, cliConnection)
5458
status := command.Execute(args[1:])
5559
if status == commands.Failure {

util/user_agent_builder.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ var pluginVersion string = "0.0.0"
1818
// cfCliVersion stores the version set from the main package
1919
var cfCliVersion string = DefaultCliVersion
2020

21+
// userAgentSuffixOption stores the default user agent suffix option set from the main package
22+
var userAgentSuffixOption string = ""
23+
2124
// SetPluginVersion sets the plugin version for use in User-Agent
2225
func SetPluginVersion(version string) {
2326
pluginVersion = version
@@ -28,6 +31,11 @@ func SetCfCliVersion(version string) {
2831
cfCliVersion = version
2932
}
3033

34+
// SetUserAgentSuffixOption sets the default user agent suffix option for use in User-Agent
35+
func SetUserAgentSuffixOption(suffix string) {
36+
userAgentSuffixOption = suffix
37+
}
38+
3139
// GetPluginVersion returns the current plugin version
3240
func GetPluginVersion() string {
3341
return pluginVersion
@@ -38,6 +46,11 @@ func GetCfCliVersion() string {
3846
return cfCliVersion
3947
}
4048

49+
// GetUserAgentSuffixOption returns the current user agent suffix option
50+
func GetUserAgentSuffixOption() string {
51+
return userAgentSuffixOption
52+
}
53+
4154
// BuildUserAgent creates a User-Agent string in the format:
4255
// "Multiapps-CF-plugin/{version} ({operating system version}) {golang builder version} {custom_env_value}"
4356
func BuildUserAgent() string {
@@ -61,8 +74,13 @@ func getOperatingSystemInformation() string {
6174
}
6275

6376
// getCustomEnvValue reads value from custom environment variable with validation
77+
// Falls back to build-time value if environment variable is not set
6478
func getCustomEnvValue() string {
6579
value := os.Getenv("MULTIAPPS_USER_AGENT_SUFFIX")
80+
if value == "" {
81+
// Use build-time value as fallback
82+
value = userAgentSuffixOption
83+
}
6684
if value == "" {
6785
return ""
6886
}

util/user_agent_builder_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,48 @@ var _ = Describe("UserAgentBuilder", func() {
325325
Expect(util.GetCfCliVersion()).To(Equal(util.DefaultCliVersion))
326326
})
327327
})
328+
329+
Describe("Build-time user agent suffix", func() {
330+
var originalSuffix string
331+
332+
BeforeEach(func() {
333+
originalSuffix = util.GetUserAgentSuffixOption()
334+
util.SetPluginVersion("1.0.0")
335+
})
336+
337+
AfterEach(func() {
338+
util.SetUserAgentSuffixOption(originalSuffix)
339+
os.Unsetenv("MULTIAPPS_USER_AGENT_SUFFIX")
340+
})
341+
342+
It("should use build-time suffix when environment variable is not set", func() {
343+
buildTimeSuffix := "build-time-suffix"
344+
util.SetUserAgentSuffixOption(buildTimeSuffix)
345+
os.Unsetenv("MULTIAPPS_USER_AGENT_SUFFIX")
346+
347+
userAgent := util.BuildUserAgent()
348+
Expect(userAgent).To(ContainSubstring(buildTimeSuffix))
349+
})
350+
351+
It("should prioritize environment variable over build-time suffix", func() {
352+
buildTimeSuffix := "build-time"
353+
envSuffix := "env-override"
354+
355+
util.SetUserAgentSuffixOption(buildTimeSuffix)
356+
os.Setenv("MULTIAPPS_USER_AGENT_SUFFIX", envSuffix)
357+
358+
userAgent := util.BuildUserAgent()
359+
Expect(userAgent).To(ContainSubstring(envSuffix))
360+
Expect(userAgent).ToNot(ContainSubstring(buildTimeSuffix))
361+
})
362+
363+
It("should handle empty build-time suffix", func() {
364+
util.SetUserAgentSuffixOption("")
365+
os.Unsetenv("MULTIAPPS_USER_AGENT_SUFFIX")
366+
367+
userAgent := util.BuildUserAgent()
368+
expectedBase := fmt.Sprintf("Multiapps-CF-plugin/1.0.0 (%s %s) %s (%s)", runtime.GOOS, runtime.GOARCH, runtime.Version(), util.GetCfCliVersion())
369+
Expect(userAgent).To(Equal(expectedBase))
370+
})
371+
})
328372
})

0 commit comments

Comments
 (0)