Repository: scaleway/scaleway-cli
Pull request #4129 (2024-09-17 12:21:51)

chore: migrate the user-agent definition to the main package
### Community Note

* Please vote on this pull request by adding a 👍 [reaction](https://blog.github.com/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/) to the original pull request comment to help the community and maintainers prioritize this request.
* Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for pull request followers and do not help prioritize the request

<!--- Thank you for keeping this note for the community --->

<!--- If your PR fully resolves and should automatically close the linked issue, use Closes. Otherwise, use Relates --->
Relates OR Closes #0000

Release note for [CHANGELOG](https://github.com/scaleway/scaleway-cli/blob/master/CHANGELOG.md):
<!--
If the change is not user facing, just write "NONE" in the release-note block below.
-->

```release-note

```



Patch:
diff --git a/cmd/scw/main.go b/cmd/scw/main.go
index 812c1543..2b0821ba 100644
--- a/cmd/scw/main.go
+++ b/cmd/scw/main.go
@@ -31,6 +31,8 @@ var (
 	GoOS      = runtime.GOOS
 	GoArch    = runtime.GOARCH
 	BetaMode  = os.Getenv(scw.ScwEnableBeta) == "true"
+
+	userAgentPrefix = "scaleway-cli"
 )
 
 // cleanup does the recover
@@ -66,13 +68,14 @@ func main() {
 
 func mainNoExit() int {
 	buildInfo := &core.BuildInfo{
-		Version:   version.Must(version.NewSemver(buildVersion())), // panic when version does not respect semantic versioning
-		BuildDate: BuildDate,
-		GoVersion: GoVersion,
-		GitBranch: GitBranch,
-		GitCommit: GitCommit,
-		GoOS:      GoOS,
-		GoArch:    GoArch,
+		Version:         version.Must(version.NewSemver(buildVersion())), // panic when version does not respect semantic versioning
+		BuildDate:       BuildDate,
+		GoVersion:       GoVersion,
+		GitBranch:       GitBranch,
+		GitCommit:       GitCommit,
+		GoOS:            GoOS,
+		GoArch:          GoArch,
+		UserAgentPrefix: userAgentPrefix,
 	}
 	defer cleanup(buildInfo)
 
diff --git a/core/build_info.go b/core/build_info.go
index ddbf12ef..3023029c 100644
--- a/core/build_info.go
+++ b/core/build_info.go
@@ -13,13 +13,14 @@ import (
 )
 
 type BuildInfo struct {
-	Version   *version.Version `json:"-"`
-	BuildDate string           `json:"build_date"`
-	GoVersion string           `json:"go_version"`
-	GitBranch string           `json:"git_branch"`
-	GitCommit string           `json:"git_commit"`
-	GoArch    string           `json:"go_arch"`
-	GoOS      string           `json:"go_os"`
+	Version         *version.Version `json:"-"`
+	BuildDate       string           `json:"build_date"`
+	GoVersion       string           `json:"go_version"`
+	GitBranch       string           `json:"git_branch"`
+	GitCommit       string           `json:"git_commit"`
+	GoArch          string           `json:"go_arch"`
+	GoOS            string           `json:"go_os"`
+	UserAgentPrefix string           `json:"user_agent_prefix"`
 }
 
 func (b *BuildInfo) MarshalJSON() ([]byte, error) {
@@ -36,7 +37,6 @@ const (
 	scwDisableCheckVersionEnv   = "SCW_DISABLE_CHECK_VERSION"
 	latestGithubReleaseURL      = "https://api.github.com/repos/scaleway/scaleway-cli/releases/latest"
 	latestVersionRequestTimeout = 1 * time.Second
-	userAgentPrefix             = "scaleway-cli"
 )
 
 // IsRelease returns true when the version of the CLI is an official release:
@@ -48,9 +48,9 @@ func (b *BuildInfo) IsRelease() bool {
 
 func (b *BuildInfo) GetUserAgent() string {
 	if b.Version != nil {
-		return userAgentPrefix + "/" + b.Version.String()
+		return b.UserAgentPrefix + "/" + b.Version.String()
 	}
-	return userAgentPrefix
+	return b.UserAgentPrefix
 }
 
 func (b *BuildInfo) Tags() map[string]string {


Test patch:
diff --git a/core/testing.go b/core/testing.go
index 64881297..eeca6d40 100644
--- a/core/testing.go
+++ b/core/testing.go
@@ -367,13 +367,14 @@ func Test(config *TestConfig) func(t *testing.T) {
 		buildInfo := config.BuildInfo
 		if buildInfo == nil {
 			buildInfo = &BuildInfo{
-				Version:   version.Must(version.NewSemver("v0.0.0+test")),
-				BuildDate: "unknown",
-				GoVersion: "runtime.Version()",
-				GitBranch: "unknown",
-				GitCommit: "unknown",
-				GoArch:    "runtime.GOARCH",
-				GoOS:      "runtime.GOOS",
+				Version:         version.Must(version.NewSemver("v0.0.0+test")),
+				BuildDate:       "unknown",
+				GoVersion:       "runtime.Version()",
+				GitBranch:       "unknown",
+				GitCommit:       "unknown",
+				GoArch:          "runtime.GOARCH",
+				GoOS:            "runtime.GOOS",
+				UserAgentPrefix: "scaleway-cli",
 			}
 		}
 
diff --git a/internal/namespaces/feedback/custom_test.go b/internal/namespaces/feedback/custom_test.go
index 7605be3d..bb3329eb 100644
--- a/internal/namespaces/feedback/custom_test.go
+++ b/internal/namespaces/feedback/custom_test.go
@@ -24,7 +24,7 @@ func Test_FeedbackBug(t *testing.T) {
 				observed = cmd.Args[1]
 			}
 			assert.Equal(t,
-				"https://github.com/scaleway/scaleway-cli/issues/new?body=%0A%23%23+Description%3A%0A%0A%23%23+How+to+reproduce%3A%0A%0A%23%23%23+Command+attempted%0A%0A%23%23%23+Expected+Behavior%0A%0A%23%23%23+Actual+Behavior%0A%0A%23%23+More+info%0A%0A%23%23+Version%0A%0AVersion++++0.0.0%26%2343%3Btest%0ABuildDate++unknown%0AGoVersion++runtime.Version%28%29%0AGitBranch++unknown%0AGitCommit++unknown%0AGoArch+++++runtime.GOARCH%0AGoOS+++++++runtime.GOOS%0A&issueTemplate=bug_report.md&labels=bug",
+				"https://github.com/scaleway/scaleway-cli/issues/new?body=%0A%23%23+Description%3A%0A%0A%23%23+How+to+reproduce%3A%0A%0A%23%23%23+Command+attempted%0A%0A%23%23%23+Expected+Behavior%0A%0A%23%23%23+Actual+Behavior%0A%0A%23%23+More+info%0A%0A%23%23+Version%0A%0AVersion++++++++++0.0.0%26%2343%3Btest%0ABuildDate++++++++unknown%0AGoVersion++++++++runtime.Version%28%29%0AGitBranch++++++++unknown%0AGitCommit++++++++unknown%0AGoArch+++++++++++runtime.GOARCH%0AGoOS+++++++++++++runtime.GOOS%0AUserAgentPrefix++scaleway-cli%0A&issueTemplate=bug_report.md&labels=bug",
 				observed)
 
 			return 0, nil
@@ -50,7 +50,7 @@ func Test_FeedbackFeature(t *testing.T) {
 				observed = cmd.Args[1]
 			}
 			assert.Equal(t,
-				"https://github.com/scaleway/scaleway-cli/issues/new?body=%0A%23%23+Description%0A%0A%23%23+How+this+functionality+would+be+exposed%0A%0A%23%23+References%0A%0A%23%23+Version%0A%0AVersion++++0.0.0%26%2343%3Btest%0ABuildDate++unknown%0AGoVersion++runtime.Version%28%29%0AGitBranch++unknown%0AGitCommit++unknown%0AGoArch+++++runtime.GOARCH%0AGoOS+++++++runtime.GOOS%0A&issueTemplate=feature_request.md&labels=enhancement",
+				"https://github.com/scaleway/scaleway-cli/issues/new?body=%0A%23%23+Description%0A%0A%23%23+How+this+functionality+would+be+exposed%0A%0A%23%23+References%0A%0A%23%23+Version%0A%0AVersion++++++++++0.0.0%26%2343%3Btest%0ABuildDate++++++++unknown%0AGoVersion++++++++runtime.Version%28%29%0AGitBranch++++++++unknown%0AGitCommit++++++++unknown%0AGoArch+++++++++++runtime.GOARCH%0AGoOS+++++++++++++runtime.GOOS%0AUserAgentPrefix++scaleway-cli%0A&issueTemplate=feature_request.md&labels=enhancement",
 				observed)
 
 			return 0, nil
diff --git a/internal/namespaces/feedback/testdata/test-feedback-bug-simple.golden b/internal/namespaces/feedback/testdata/test-feedback-bug-simple.golden
index c4ff8fb7..5db83bee 100644
--- a/internal/namespaces/feedback/testdata/test-feedback-bug-simple.golden
+++ b/internal/namespaces/feedback/testdata/test-feedback-bug-simple.golden
@@ -1,9 +1,9 @@
 🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
 🟩🟩🟩 STDOUT️ 🟩🟩🟩️
 ✅ Successfully opened the page.
-  https://github.com/scaleway/scaleway-cli/issues/new?body=%0A%23%23+Description%3A%0A%0A%23%23+How+to+reproduce%3A%0A%0A%23%23%23+Command+attempted%0A%0A%23%23%23+Expected+Behavior%0A%0A%23%23%23+Actual+Behavior%0A%0A%23%23+More+info%0A%0A%23%23+Version%0A%0AVersion++++0.0.0%26%2343%3Btest%0ABuildDate++unknown%0AGoVersion++runtime.Version%28%29%0AGitBranch++unknown%0AGitCommit++unknown%0AGoArch+++++runtime.GOARCH%0AGoOS+++++++runtime.GOOS%0A&issueTemplate=bug_report.md&labels=bug
+  https://github.com/scaleway/scaleway-cli/issues/new?body=%0A%23%23+Description%3A%0A%0A%23%23+How+to+reproduce%3A%0A%0A%23%23%23+Command+attempted%0A%0A%23%23%23+Expected+Behavior%0A%0A%23%23%23+Actual+Behavior%0A%0A%23%23+More+info%0A%0A%23%23+Version%0A%0AVersion++++++++++0.0.0%26%2343%3Btest%0ABuildDate++++++++unknown%0AGoVersion++++++++runtime.Version%28%29%0AGitBranch++++++++unknown%0AGitCommit++++++++unknown%0AGoArch+++++++++++runtime.GOARCH%0AGoOS+++++++++++++runtime.GOOS%0AUserAgentPrefix++scaleway-cli%0A&issueTemplate=bug_report.md&labels=bug
 🟩🟩🟩 JSON STDOUT 🟩🟩🟩
 {
   "message": "Successfully opened the page",
-  "details": "https://github.com/scaleway/scaleway-cli/issues/new?body=%0A%23%23+Description%3A%0A%0A%23%23+How+to+reproduce%3A%0A%0A%23%23%23+Command+attempted%0A%0A%23%23%23+Expected+Behavior%0A%0A%23%23%23+Actual+Behavior%0A%0A%23%23+More+info%0A%0A%23%23+Version%0A%0AVersion++++0.0.0%26%2343%3Btest%0ABuildDate++unknown%0AGoVersion++runtime.Version%28%29%0AGitBranch++unknown%0AGitCommit++unknown%0AGoArch+++++runtime.GOARCH%0AGoOS+++++++runtime.GOOS%0A\u0026issueTemplate=bug_report.md\u0026labels=bug"
+  "details": "https://github.com/scaleway/scaleway-cli/issues/new?body=%0A%23%23+Description%3A%0A%0A%23%23+How+to+reproduce%3A%0A%0A%23%23%23+Command+attempted%0A%0A%23%23%23+Expected+Behavior%0A%0A%23%23%23+Actual+Behavior%0A%0A%23%23+More+info%0A%0A%23%23+Version%0A%0AVersion++++++++++0.0.0%26%2343%3Btest%0ABuildDate++++++++unknown%0AGoVersion++++++++runtime.Version%28%29%0AGitBranch++++++++unknown%0AGitCommit++++++++unknown%0AGoArch+++++++++++runtime.GOARCH%0AGoOS+++++++++++++runtime.GOOS%0AUserAgentPrefix++scaleway-cli%0A\u0026issueTemplate=bug_report.md\u0026labels=bug"
 }
diff --git a/internal/namespaces/feedback/testdata/test-feedback-feature-simple.golden b/internal/namespaces/feedback/testdata/test-feedback-feature-simple.golden
index 387145ed..c85f0dec 100644
--- a/internal/namespaces/feedback/testdata/test-feedback-feature-simple.golden
+++ b/internal/namespaces/feedback/testdata/test-feedback-feature-simple.golden
@@ -1,9 +1,9 @@
 🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
 🟩🟩🟩 STDOUT️ 🟩🟩🟩️
 ✅ Successfully opened the page.
-  https://github.com/scaleway/scaleway-cli/issues/new?body=%0A%23%23+Description%0A%0A%23%23+How+this+functionality+would+be+exposed%0A%0A%23%23+References%0A%0A%23%23+Version%0A%0AVersion++++0.0.0%26%2343%3Btest%0ABuildDate++unknown%0AGoVersion++runtime.Version%28%29%0AGitBranch++unknown%0AGitCommit++unknown%0AGoArch+++++runtime.GOARCH%0AGoOS+++++++runtime.GOOS%0A&issueTemplate=feature_request.md&labels=enhancement
+  https://github.com/scaleway/scaleway-cli/issues/new?body=%0A%23%23+Description%0A%0A%23%23+How+this+functionality+would+be+exposed%0A%0A%23%23+References%0A%0A%23%23+Version%0A%0AVersion++++++++++0.0.0%26%2343%3Btest%0ABuildDate++++++++unknown%0AGoVersion++++++++runtime.Version%28%29%0AGitBranch++++++++unknown%0AGitCommit++++++++unknown%0AGoArch+++++++++++runtime.GOARCH%0AGoOS+++++++++++++runtime.GOOS%0AUserAgentPrefix++scaleway-cli%0A&issueTemplate=feature_request.md&labels=enhancement
 🟩🟩🟩 JSON STDOUT 🟩🟩🟩
 {
   "message": "Successfully opened the page",
-  "details": "https://github.com/scaleway/scaleway-cli/issues/new?body=%0A%23%23+Description%0A%0A%23%23+How+this+functionality+would+be+exposed%0A%0A%23%23+References%0A%0A%23%23+Version%0A%0AVersion++++0.0.0%26%2343%3Btest%0ABuildDate++unknown%0AGoVersion++runtime.Version%28%29%0AGitBranch++unknown%0AGitCommit++unknown%0AGoArch+++++runtime.GOARCH%0AGoOS+++++++runtime.GOOS%0A\u0026issueTemplate=feature_request.md\u0026labels=enhancement"
+  "details": "https://github.com/scaleway/scaleway-cli/issues/new?body=%0A%23%23+Description%0A%0A%23%23+How+this+functionality+would+be+exposed%0A%0A%23%23+References%0A%0A%23%23+Version%0A%0AVersion++++++++++0.0.0%26%2343%3Btest%0ABuildDate++++++++unknown%0AGoVersion++++++++runtime.Version%28%29%0AGitBranch++++++++unknown%0AGitCommit++++++++unknown%0AGoArch+++++++++++runtime.GOARCH%0AGoOS+++++++++++++runtime.GOOS%0AUserAgentPrefix++scaleway-cli%0A\u0026issueTemplate=feature_request.md\u0026labels=enhancement"
 }
diff --git a/internal/namespaces/info/testdata/test-info-show-secret.golden b/internal/namespaces/info/testdata/test-info-show-secret.golden
index fe6de41d..1732cc4e 100644
--- a/internal/namespaces/info/testdata/test-info-show-secret.golden
+++ b/internal/namespaces/info/testdata/test-info-show-secret.golden
@@ -1,13 +1,14 @@
 🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
 🟩🟩🟩 STDOUT️ 🟩🟩🟩️
 Build Info:
-Version    0.0.0+test
-BuildDate  unknown
-GoVersion  runtime.Version()
-GitBranch  unknown
-GitCommit  unknown
-GoArch     runtime.GOARCH
-GoOS       runtime.GOOS
+Version          0.0.0+test
+BuildDate        unknown
+GoVersion        runtime.Version()
+GitBranch        unknown
+GitCommit        unknown
+GoArch           runtime.GOARCH
+GoOS             runtime.GOOS
+UserAgentPrefix  scaleway-cli
 
 Settings:
 KEY                      VALUE                                 ORIGIN
@@ -28,6 +29,7 @@ secret_key               22222222-2222-2222-2222-222222222222  env (SCW_SECRET_K
     "git_commit": "unknown",
     "go_arch": "runtime.GOARCH",
     "go_os": "runtime.GOOS",
+    "user_agent_prefix": "scaleway-cli",
     "version": "0.0.0+test"
   },
   "settings": [
diff --git a/internal/namespaces/info/testdata/test-info-simple.golden b/internal/namespaces/info/testdata/test-info-simple.golden
index c21e0b19..4ed5eb96 100644
--- a/internal/namespaces/info/testdata/test-info-simple.golden
+++ b/internal/namespaces/info/testdata/test-info-simple.golden
@@ -1,13 +1,14 @@
 🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
 🟩🟩🟩 STDOUT️ 🟩🟩🟩️
 Build Info:
-Version    0.0.0+test
-BuildDate  unknown
-GoVersion  runtime.Version()
-GitBranch  unknown
-GitCommit  unknown
-GoArch     runtime.GOARCH
-GoOS       runtime.GOOS
+Version          0.0.0+test
+BuildDate        unknown
+GoVersion        runtime.Version()
+GitBranch        unknown
+GitCommit        unknown
+GoArch           runtime.GOARCH
+GoOS             runtime.GOOS
+UserAgentPrefix  scaleway-cli
 
 Settings:
 KEY                      VALUE                                 ORIGIN
@@ -28,6 +29,7 @@ secret_key               22222222-xxxx-xxxx-xxxx-xxxxxxxxxxxx  env (SCW_SECRET_K
     "git_commit": "unknown",
     "go_arch": "runtime.GOARCH",
     "go_os": "runtime.GOOS",
+    "user_agent_prefix": "scaleway-cli",
     "version": "0.0.0+test"
   },
   "settings": [