Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions multiapps_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
// Version is the version of the CLI plugin. It is injected on linking time.
var Version string = "0.0.0"

// MultiappsUserAgentSuffixOption is the default user agent suffix option. It is injected on linking time.
var MultiappsUserAgentSuffixOption string = ""

// MultiappsPlugin represents a cf CLI plugin for executing operations on MTAs
type MultiappsPlugin struct{}

Expand Down Expand Up @@ -50,6 +53,7 @@ func (p *MultiappsPlugin) Run(cliConnection plugin.CliConnection, args []string)
}
util.SetCfCliVersion(strings.Join(versionOutput, " "))
util.SetPluginVersion(Version)
util.SetUserAgentSuffixOption(MultiappsUserAgentSuffixOption)
command.Initialize(command.GetPluginCommand().Name, cliConnection)
status := command.Execute(args[1:])
if status == commands.Failure {
Expand Down
18 changes: 18 additions & 0 deletions util/user_agent_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ var pluginVersion string = "0.0.0"
// cfCliVersion stores the version set from the main package
var cfCliVersion string = DefaultCliVersion

// userAgentSuffixOption stores the default user agent suffix option set from the main package
var userAgentSuffixOption string = ""

// SetPluginVersion sets the plugin version for use in User-Agent
func SetPluginVersion(version string) {
pluginVersion = version
Expand All @@ -28,6 +31,11 @@ func SetCfCliVersion(version string) {
cfCliVersion = version
}

// SetUserAgentSuffixOption sets the default user agent suffix option for use in User-Agent
func SetUserAgentSuffixOption(suffix string) {
userAgentSuffixOption = suffix
}

// GetPluginVersion returns the current plugin version
func GetPluginVersion() string {
return pluginVersion
Expand All @@ -38,6 +46,11 @@ func GetCfCliVersion() string {
return cfCliVersion
}

// GetUserAgentSuffixOption returns the current user agent suffix option
func GetUserAgentSuffixOption() string {
return userAgentSuffixOption
}

// BuildUserAgent creates a User-Agent string in the format:
// "Multiapps-CF-plugin/{version} ({operating system version}) {golang builder version} {custom_env_value}"
func BuildUserAgent() string {
Expand All @@ -61,8 +74,13 @@ func getOperatingSystemInformation() string {
}

// getCustomEnvValue reads value from custom environment variable with validation
// Falls back to build-time value if environment variable is not set
func getCustomEnvValue() string {
value := os.Getenv("MULTIAPPS_USER_AGENT_SUFFIX")
if value == "" {
// Use build-time value as fallback
value = userAgentSuffixOption
}
if value == "" {
return ""
}
Expand Down
44 changes: 44 additions & 0 deletions util/user_agent_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,48 @@ var _ = Describe("UserAgentBuilder", func() {
Expect(util.GetCfCliVersion()).To(Equal(util.DefaultCliVersion))
})
})

Describe("Build-time user agent suffix", func() {
var originalSuffix string

BeforeEach(func() {
originalSuffix = util.GetUserAgentSuffixOption()
util.SetPluginVersion("1.0.0")
})

AfterEach(func() {
util.SetUserAgentSuffixOption(originalSuffix)
os.Unsetenv("MULTIAPPS_USER_AGENT_SUFFIX")
})

It("should use build-time suffix when environment variable is not set", func() {
buildTimeSuffix := "build-time-suffix"
util.SetUserAgentSuffixOption(buildTimeSuffix)
os.Unsetenv("MULTIAPPS_USER_AGENT_SUFFIX")

userAgent := util.BuildUserAgent()
Expect(userAgent).To(ContainSubstring(buildTimeSuffix))
})

It("should prioritize environment variable over build-time suffix", func() {
buildTimeSuffix := "build-time"
envSuffix := "env-override"

util.SetUserAgentSuffixOption(buildTimeSuffix)
os.Setenv("MULTIAPPS_USER_AGENT_SUFFIX", envSuffix)

userAgent := util.BuildUserAgent()
Expect(userAgent).To(ContainSubstring(envSuffix))
Expect(userAgent).ToNot(ContainSubstring(buildTimeSuffix))
})

It("should handle empty build-time suffix", func() {
util.SetUserAgentSuffixOption("")
os.Unsetenv("MULTIAPPS_USER_AGENT_SUFFIX")

userAgent := util.BuildUserAgent()
expectedBase := fmt.Sprintf("Multiapps-CF-plugin/1.0.0 (%s %s) %s (%s)", runtime.GOOS, runtime.GOARCH, runtime.Version(), util.GetCfCliVersion())
Expect(userAgent).To(Equal(expectedBase))
})
})
})
Loading