diff --git a/multiapps_plugin.go b/multiapps_plugin.go index 6be8166..1e301c8 100644 --- a/multiapps_plugin.go +++ b/multiapps_plugin.go @@ -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{} @@ -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 { diff --git a/util/user_agent_builder.go b/util/user_agent_builder.go index 5c81611..f0f0c12 100644 --- a/util/user_agent_builder.go +++ b/util/user_agent_builder.go @@ -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 @@ -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 @@ -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 { @@ -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 "" } diff --git a/util/user_agent_builder_test.go b/util/user_agent_builder_test.go index 06e00db..8f42f30 100644 --- a/util/user_agent_builder_test.go +++ b/util/user_agent_builder_test.go @@ -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)) + }) + }) })