From ec0f7f292534e059767d8bc71267b35101220716 Mon Sep 17 00:00:00 2001 From: Gary Holtz Date: Tue, 28 Feb 2023 16:18:52 -0600 Subject: [PATCH 1/4] chore: Modify UserAgent to be more like other CLIs --- Makefile | 1 + api/client.go | 19 +++++++++++++++++-- cmd/glab/main.go | 5 +++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 94386e425..677baeff4 100644 --- a/Makefile +++ b/Makefile @@ -47,6 +47,7 @@ endif GO_LDFLAGS := -X main.build=$(BUILD_DATE) $(GO_LDFLAGS) GO_LDFLAGS := $(GO_LDFLAGS) -X main.version=$(GLAB_VERSION) +GO_LDFLAGS := $(GO_LDFLAGS) -X main.platform=$(GOOS) GOURL ?= gitlab.com/gitlab-org/cli BUILDLOC ?= ./bin/glab diff --git a/api/client.go b/api/client.go index 444e9d5d5..12b8a87a6 100644 --- a/api/client.go +++ b/api/client.go @@ -27,7 +27,11 @@ const ( PrivateToken ) -const UserAgent = "GLab - GitLab CLI" +var ( + glabVersion string + glabPlatform string + glabBuild string +) // Global api client to be used throughout glab var apiClient *Client @@ -61,6 +65,17 @@ func init() { RefreshClient() } +func UserAgent() string { + // UserAgent format: glab/v1.25.3-27-g7ec258fb (built 2023-02-16), darwin + return fmt.Sprintf("glab/%s (built %s), %s", glabVersion, glabBuild, glabPlatform) +} + +func SetUserAgent(version string, build string, platform string) { + glabVersion = version + glabBuild = build + glabPlatform = platform +} + // RefreshClient re-initializes the api client func RefreshClient() { apiClient = &Client{ @@ -303,7 +318,7 @@ func (c *Client) NewLab() error { if err != nil { return fmt.Errorf("failed to initialize GitLab client: %v", err) } - c.LabClient.UserAgent = UserAgent + c.LabClient.UserAgent = UserAgent() if c.token != "" { c.AuthType = PrivateToken diff --git a/cmd/glab/main.go b/cmd/glab/main.go index 00d0fd914..bf295d468 100644 --- a/cmd/glab/main.go +++ b/cmd/glab/main.go @@ -9,6 +9,7 @@ import ( "strconv" "strings" + "gitlab.com/gitlab-org/cli/api" "gitlab.com/gitlab-org/cli/pkg/iostreams" "github.com/mgutz/ansi" @@ -34,6 +35,9 @@ var version = "DEV" // build is set dynamically at build var build string +// platform is set dynamically at build +var platform string + // debug is set dynamically at build and can be overridden by // the configuration file or environment variable // sets to "true" or "false" or "1" or "0" as string @@ -53,6 +57,7 @@ func main() { os.Exit(2) } + api.SetUserAgent(version, build, platform) maybeOverrideDefaultHost(cmdFactory, cfg) if !cmdFactory.IO.ColorEnabled() { -- GitLab From 1844675adc9f244fbe24dd905a3d71821a47e2cf Mon Sep 17 00:00:00 2001 From: Gary Holtz Date: Tue, 28 Feb 2023 23:31:05 -0600 Subject: [PATCH 2/4] updating tests --- commands/api/http_test.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/commands/api/http_test.go b/commands/api/http_test.go index 2fedea98f..71cc97228 100644 --- a/commands/api/http_test.go +++ b/commands/api/http_test.go @@ -97,6 +97,8 @@ hosts: `, "")() originalEnvVarToken, envIsSet := os.LookupEnv("GITLAB_TOKEN") + api.SetUserAgent("v1.2.3", "12-31-1999", "darwin") + if envIsSet && originalEnvVarToken != "" { _ = os.Setenv("GITLAB_TOKEN", "") } @@ -146,7 +148,7 @@ hosts: method: "GET", u: "https://gitlab.com/api/v4/projects/gitlab-com%2Fwww-gitlab-com", body: "", - headers: "Private-Token: OTOKEN\r\nUser-Agent: GLab - GitLab CLI\r\n", + headers: "Private-Token: OTOKEN\r\nUser-Agent: glab/v1.2.3 (built 12-31-1999), darwin\r\n", }, }, { @@ -164,7 +166,7 @@ hosts: method: "GET", u: "https://gitlab.com/api/v4/projects/gitlab-com%2Fwww-gitlab-com", body: "", - headers: "Private-Token: OTOKEN\r\nUser-Agent: GLab - GitLab CLI\r\n", + headers: "Private-Token: OTOKEN\r\nUser-Agent: glab/v1.2.3 (built 12-31-1999), darwin\r\n", }, }, { @@ -184,7 +186,7 @@ hosts: method: "GET", u: "https://gitlab.com/api/v4/projects/gitlab-com%2Fwww-gitlab-com?a=b", body: "", - headers: "Private-Token: OTOKEN\r\nUser-Agent: GLab - GitLab CLI\r\n", + headers: "Private-Token: OTOKEN\r\nUser-Agent: glab/v1.2.3 (built 12-31-1999), darwin\r\n", }, }, { @@ -204,7 +206,7 @@ hosts: method: "POST", u: "https://gitlab.com/api/graphql/", body: `{"variables":{"a":"b"}}`, - headers: "Content-Type: application/json; charset=utf-8\r\nPrivate-Token: OTOKEN\r\nUser-Agent: GLab - GitLab CLI\r\n", + headers: "Content-Type: application/json; charset=utf-8\r\nPrivate-Token: OTOKEN\r\nUser-Agent: glab/v1.2.3 (built 12-31-1999), darwin\r\n", }, }, { @@ -225,7 +227,7 @@ hosts: method: "POST", u: "https://gitlab.com/api/v4/projects", body: `CUSTOM`, - headers: "Accept: application/json\r\nContent-Type: text/plain\r\nPrivate-Token: OTOKEN\r\nUser-Agent: GLab - GitLab CLI\r\n", + headers: "Accept: application/json\r\nContent-Type: text/plain\r\nPrivate-Token: OTOKEN\r\nUser-Agent: glab/v1.2.3 (built 12-31-1999), darwin\r\n", }, }, } -- GitLab From aefe159328c1861385b43462c588715b70509d88 Mon Sep 17 00:00:00 2001 From: Gary Holtz Date: Mon, 6 Mar 2023 14:02:05 -0600 Subject: [PATCH 3/4] using a struct and moving things around --- api/client.go | 34 ++++++++++++++++++---------------- commands/api/http_test.go | 12 +++++++----- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/api/client.go b/api/client.go index 12b8a87a6..32674147e 100644 --- a/api/client.go +++ b/api/client.go @@ -27,11 +27,11 @@ const ( PrivateToken ) -var ( - glabVersion string - glabPlatform string - glabBuild string -) +type glabInstall struct { + version, platform, build string +} + +var currentGlabInstall glabInstall // Global api client to be used throughout glab var apiClient *Client @@ -60,20 +60,22 @@ type Client struct { refreshLabInstance bool } -func init() { - // initialise the global api client to be used throughout glab - RefreshClient() +func (i glabInstall) UserAgent() string { + // UserAgent format: glab/v1.25.3-27-g7ec258fb - built 2023-02-16, (darwin) + return fmt.Sprintf("glab/%s - built %s, (%s)", i.version, i.build, i.platform) } -func UserAgent() string { - // UserAgent format: glab/v1.25.3-27-g7ec258fb (built 2023-02-16), darwin - return fmt.Sprintf("glab/%s (built %s), %s", glabVersion, glabBuild, glabPlatform) +func SetUserAgent(version string, build string, platform string) { + currentGlabInstall = glabInstall{ + version: version, + build: build, + platform: platform, + } } -func SetUserAgent(version string, build string, platform string) { - glabVersion = version - glabBuild = build - glabPlatform = platform +func init() { + // initialise the global api client to be used throughout glab + RefreshClient() } // RefreshClient re-initializes the api client @@ -318,7 +320,7 @@ func (c *Client) NewLab() error { if err != nil { return fmt.Errorf("failed to initialize GitLab client: %v", err) } - c.LabClient.UserAgent = UserAgent() + c.LabClient.UserAgent = currentGlabInstall.UserAgent() if c.token != "" { c.AuthType = PrivateToken diff --git a/commands/api/http_test.go b/commands/api/http_test.go index 71cc97228..86dbe1c44 100644 --- a/commands/api/http_test.go +++ b/commands/api/http_test.go @@ -2,6 +2,7 @@ package api import ( "bytes" + "fmt" "io" "net/http" "os" @@ -98,6 +99,7 @@ hosts: originalEnvVarToken, envIsSet := os.LookupEnv("GITLAB_TOKEN") api.SetUserAgent("v1.2.3", "12-31-1999", "darwin") + versionString := "glab/v1.2.3 - built 12-31-1999, (darwin)" if envIsSet && originalEnvVarToken != "" { _ = os.Setenv("GITLAB_TOKEN", "") @@ -148,7 +150,7 @@ hosts: method: "GET", u: "https://gitlab.com/api/v4/projects/gitlab-com%2Fwww-gitlab-com", body: "", - headers: "Private-Token: OTOKEN\r\nUser-Agent: glab/v1.2.3 (built 12-31-1999), darwin\r\n", + headers: fmt.Sprintf("Private-Token: OTOKEN\r\nUser-Agent: %s\r\n", versionString), }, }, { @@ -166,7 +168,7 @@ hosts: method: "GET", u: "https://gitlab.com/api/v4/projects/gitlab-com%2Fwww-gitlab-com", body: "", - headers: "Private-Token: OTOKEN\r\nUser-Agent: glab/v1.2.3 (built 12-31-1999), darwin\r\n", + headers: fmt.Sprintf("Private-Token: OTOKEN\r\nUser-Agent: %s\r\n", versionString), }, }, { @@ -186,7 +188,7 @@ hosts: method: "GET", u: "https://gitlab.com/api/v4/projects/gitlab-com%2Fwww-gitlab-com?a=b", body: "", - headers: "Private-Token: OTOKEN\r\nUser-Agent: glab/v1.2.3 (built 12-31-1999), darwin\r\n", + headers: fmt.Sprintf("Private-Token: OTOKEN\r\nUser-Agent: %s\r\n", versionString), }, }, { @@ -206,7 +208,7 @@ hosts: method: "POST", u: "https://gitlab.com/api/graphql/", body: `{"variables":{"a":"b"}}`, - headers: "Content-Type: application/json; charset=utf-8\r\nPrivate-Token: OTOKEN\r\nUser-Agent: glab/v1.2.3 (built 12-31-1999), darwin\r\n", + headers: fmt.Sprintf("Content-Type: application/json; charset=utf-8\r\nPrivate-Token: OTOKEN\r\nUser-Agent: %s\r\n", versionString), }, }, { @@ -227,7 +229,7 @@ hosts: method: "POST", u: "https://gitlab.com/api/v4/projects", body: `CUSTOM`, - headers: "Accept: application/json\r\nContent-Type: text/plain\r\nPrivate-Token: OTOKEN\r\nUser-Agent: glab/v1.2.3 (built 12-31-1999), darwin\r\n", + headers: fmt.Sprintf("Accept: application/json\r\nContent-Type: text/plain\r\nPrivate-Token: OTOKEN\r\nUser-Agent: %s\r\n", versionString), }, }, } -- GitLab From f8fe0986667641fec96eac610e3c2c9e26826652 Mon Sep 17 00:00:00 2001 From: Gary Holtz Date: Wed, 8 Mar 2023 09:55:35 -0600 Subject: [PATCH 4/4] cleaning up main variables --- Makefile | 2 +- cmd/glab/main.go | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 677baeff4..fe53f2496 100644 --- a/Makefile +++ b/Makefile @@ -45,7 +45,7 @@ else GOLINT=bin/golangci-lint endif -GO_LDFLAGS := -X main.build=$(BUILD_DATE) $(GO_LDFLAGS) +GO_LDFLAGS := -X main.buildDate=$(BUILD_DATE) $(GO_LDFLAGS) GO_LDFLAGS := $(GO_LDFLAGS) -X main.version=$(GLAB_VERSION) GO_LDFLAGS := $(GO_LDFLAGS) -X main.platform=$(GOOS) GOURL ?= gitlab.com/gitlab-org/cli diff --git a/cmd/glab/main.go b/cmd/glab/main.go index bf295d468..b78dfde00 100644 --- a/cmd/glab/main.go +++ b/cmd/glab/main.go @@ -29,14 +29,14 @@ import ( "github.com/spf13/cobra" ) -// version is set dynamically at build -var version = "DEV" - -// build is set dynamically at build -var build string - -// platform is set dynamically at build -var platform string +var ( + // version is set dynamically at build + version = "DEV" + // buildDate is set dynamically at build + buildDate string + // platform is set dynamically at build + platform string +) // debug is set dynamically at build and can be overridden by // the configuration file or environment variable @@ -57,7 +57,7 @@ func main() { os.Exit(2) } - api.SetUserAgent(version, build, platform) + api.SetUserAgent(version, buildDate, platform) maybeOverrideDefaultHost(cmdFactory, cfg) if !cmdFactory.IO.ColorEnabled() { @@ -80,7 +80,7 @@ func main() { } } - rootCmd := commands.NewCmdRoot(cmdFactory, version, build) + rootCmd := commands.NewCmdRoot(cmdFactory, version, buildDate) // Set Debug mode debugMode, _ = cfg.Get("", "debug") -- GitLab