Skip to content

Commit d3f360d

Browse files
committed
Add cf cli version to user agent header
JIRA:LMCROSSITXSADEPLOY-2854
1 parent 6c45bc9 commit d3f360d

File tree

3 files changed

+106
-5
lines changed

3 files changed

+106
-5
lines changed

multiapps_plugin.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ func (p *MultiappsPlugin) Run(cliConnection plugin.CliConnection, args []string)
4343
if err != nil {
4444
log.Fatalln(err)
4545
}
46+
versionOutput, err := cliConnection.CliCommandWithoutTerminalOutput("version")
47+
if err != nil {
48+
log.Traceln(err)
49+
versionOutput = []string{util.DefaultCliVersion}
50+
}
51+
util.SetCfCliVersion(strings.Join(versionOutput, " "))
4652
util.SetPluginVersion(Version)
4753
command.Initialize(command.GetPluginCommand().Name, cliConnection)
4854
status := command.Execute(args[1:])

util/user_agent_builder.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,43 @@ import (
1010

1111
// Limit for MULTIAPPS_USER_AGENT_SUFFIX
1212
const maxSuffixLength = 128
13+
const DefaultCliVersion = "unknown-cf cli version"
1314

1415
// pluginVersion stores the version set from the main package
1516
var pluginVersion string = "0.0.0"
1617

18+
// cfCliVersion stores the version set from the main package
19+
var cfCliVersion string = DefaultCliVersion
20+
1721
// SetPluginVersion sets the plugin version for use in User-Agent
1822
func SetPluginVersion(version string) {
1923
pluginVersion = version
2024
}
2125

26+
// SetCfCliVersion sets the cf CLI version for use in User-Agent
27+
func SetCfCliVersion(version string) {
28+
cfCliVersion = version
29+
}
30+
2231
// GetPluginVersion returns the current plugin version
2332
func GetPluginVersion() string {
2433
return pluginVersion
2534
}
2635

36+
// GetCfCliVersion returns the current cf CLI version
37+
func GetCfCliVersion() string {
38+
return cfCliVersion
39+
}
40+
2741
// BuildUserAgent creates a User-Agent string in the format:
2842
// "Multiapps-CF-plugin/{version} ({operating system version}) {golang builder version} {custom_env_value}"
2943
func BuildUserAgent() string {
3044
osInformation := getOperatingSystemInformation()
3145
goVersion := runtime.Version()
46+
cfCliVersion := GetCfCliVersion()
3247
customValue := getCustomEnvValue()
3348

34-
userAgent := fmt.Sprintf("Multiapps-CF-plugin/%s (%s) %s", pluginVersion, osInformation, goVersion)
49+
userAgent := fmt.Sprintf("Multiapps-CF-plugin/%s (%s) %s (%s)", pluginVersion, osInformation, goVersion, cfCliVersion)
3550

3651
if customValue != "" {
3752
userAgent = fmt.Sprintf("%s %s", userAgent, customValue)

util/user_agent_builder_test.go

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package util_test
33
import (
44
"fmt"
55
"os"
6+
"regexp"
67
"runtime"
78
"strings"
89

@@ -15,15 +16,18 @@ var _ = Describe("UserAgentBuilder", func() {
1516

1617
Describe("BuildUserAgent", func() {
1718
var originalVersion string
19+
var originalCfCliVersion string
1820

1921
BeforeEach(func() {
20-
// Save original version for restoration
22+
// Save original versions for restoration
2123
originalVersion = util.GetPluginVersion()
24+
originalCfCliVersion = util.GetCfCliVersion()
2225
})
2326

2427
AfterEach(func() {
25-
// Restore original version and clean up environment
28+
// Restore original versions and clean up environment
2629
util.SetPluginVersion(originalVersion)
30+
util.SetCfCliVersion(originalCfCliVersion)
2731
os.Unsetenv("MULTIAPPS_USER_AGENT_SUFFIX")
2832
})
2933

@@ -73,6 +77,29 @@ var _ = Describe("UserAgentBuilder", func() {
7377
})
7478
})
7579

80+
Context("with custom CF CLI version", func() {
81+
BeforeEach(func() {
82+
util.SetPluginVersion("1.0.0")
83+
util.SetCfCliVersion("8.5.0")
84+
})
85+
86+
It("should contain the CF CLI version in parentheses", func() {
87+
userAgent := util.BuildUserAgent()
88+
89+
Expect(userAgent).To(ContainSubstring("(8.5.0)"))
90+
Expect(userAgent).To(ContainSubstring("Multiapps-CF-plugin/1.0.0"))
91+
})
92+
93+
It("should have correct format with CF CLI version", func() {
94+
userAgent := util.BuildUserAgent()
95+
96+
// Expected format: "Multiapps-CF-plugin/{version} ({os} {arch}) {go version} ({cf cli version})"
97+
expectedPattern := fmt.Sprintf("Multiapps-CF-plugin/1.0.0 \\(%s %s\\) %s \\(8.5.0\\)", runtime.GOOS, runtime.GOARCH, regexp.QuoteMeta(runtime.Version()))
98+
matched, _ := regexp.MatchString(expectedPattern, userAgent)
99+
Expect(matched).To(BeTrue(), fmt.Sprintf("User agent '%s' should match pattern '%s'", userAgent, expectedPattern))
100+
})
101+
})
102+
76103
Context("with custom environment value", func() {
77104
BeforeEach(func() {
78105
util.SetPluginVersion("3.6.0")
@@ -136,7 +163,7 @@ var _ = Describe("UserAgentBuilder", func() {
136163
userAgent := util.BuildUserAgent()
137164

138165
// Should only contain the base user agent without suffix
139-
expectedBase := fmt.Sprintf("Multiapps-CF-plugin/1.0.0 (%s %s) %s", runtime.GOOS, runtime.GOARCH, runtime.Version())
166+
expectedBase := fmt.Sprintf("Multiapps-CF-plugin/1.0.0 (%s %s) %s (unknown-cf cli version)", runtime.GOOS, runtime.GOARCH, runtime.Version())
140167
Expect(userAgent).To(Equal(expectedBase))
141168
})
142169

@@ -145,7 +172,7 @@ var _ = Describe("UserAgentBuilder", func() {
145172
userAgent := util.BuildUserAgent()
146173

147174
// Should only contain the base user agent without suffix (whitespace gets trimmed to empty)
148-
expectedBase := fmt.Sprintf("Multiapps-CF-plugin/1.0.0 (%s %s) %s", runtime.GOOS, runtime.GOARCH, runtime.Version())
175+
expectedBase := fmt.Sprintf("Multiapps-CF-plugin/1.0.0 (%s %s) %s (unknown-cf cli version)", runtime.GOOS, runtime.GOARCH, runtime.Version())
149176
Expect(userAgent).To(Equal(expectedBase))
150177
})
151178

@@ -245,4 +272,57 @@ var _ = Describe("UserAgentBuilder", func() {
245272
Expect(util.GetPluginVersion()).To(Equal("0.0.0"))
246273
})
247274
})
275+
276+
Describe("SetCfCliVersion", func() {
277+
var originalVersion string
278+
279+
BeforeEach(func() {
280+
originalVersion = util.GetCfCliVersion()
281+
})
282+
283+
AfterEach(func() {
284+
util.SetCfCliVersion(originalVersion)
285+
})
286+
287+
It("should set the CF CLI version correctly", func() {
288+
testVersion := "8.5.0"
289+
util.SetCfCliVersion(testVersion)
290+
291+
Expect(util.GetCfCliVersion()).To(Equal(testVersion))
292+
})
293+
294+
It("should be used in BuildUserAgent", func() {
295+
testVersion := "8.5.0"
296+
util.SetCfCliVersion(testVersion)
297+
util.SetPluginVersion("1.0.0")
298+
299+
userAgent := util.BuildUserAgent()
300+
Expect(userAgent).To(ContainSubstring(fmt.Sprintf("(%s)", testVersion)))
301+
})
302+
})
303+
304+
Describe("GetCfCliVersion", func() {
305+
var originalVersion string
306+
307+
BeforeEach(func() {
308+
originalVersion = util.GetCfCliVersion()
309+
})
310+
311+
AfterEach(func() {
312+
util.SetCfCliVersion(originalVersion)
313+
})
314+
315+
It("should return the current CF CLI version", func() {
316+
testVersion := "8.6.0"
317+
util.SetCfCliVersion(testVersion)
318+
319+
Expect(util.GetCfCliVersion()).To(Equal(testVersion))
320+
})
321+
322+
It("should return default CF CLI version initially", func() {
323+
util.SetCfCliVersion(util.DefaultCliVersion)
324+
325+
Expect(util.GetCfCliVersion()).To(Equal(util.DefaultCliVersion))
326+
})
327+
})
248328
})

0 commit comments

Comments
 (0)