diff --git a/Makefile b/Makefile index 94386e425cd43e87279cf5b23ff08f95023ffc26..fe53f24963ab0f3632e59fc3bcf95d720ab1a2b1 100644 --- a/Makefile +++ b/Makefile @@ -45,8 +45,9 @@ 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 BUILDLOC ?= ./bin/glab diff --git a/api/client.go b/api/client.go index 444e9d5d597c23774ad9903870f2530f28ba7e26..32674147e6eab9f8f0291eb1b4d670b1ff48b518 100644 --- a/api/client.go +++ b/api/client.go @@ -27,7 +27,11 @@ const ( PrivateToken ) -const UserAgent = "GLab - GitLab CLI" +type glabInstall struct { + version, platform, build string +} + +var currentGlabInstall glabInstall // Global api client to be used throughout glab var apiClient *Client @@ -56,6 +60,19 @@ type Client struct { refreshLabInstance bool } +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 SetUserAgent(version string, build string, platform string) { + currentGlabInstall = glabInstall{ + version: version, + build: build, + platform: platform, + } +} + func init() { // initialise the global api client to be used throughout glab RefreshClient() @@ -303,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/cmd/glab/main.go b/cmd/glab/main.go index 00d0fd914bbf1e0758efeb9d37c65e0d3c0469df..b78dfde00dc287c8762a0c72976b39c3796bb17e 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" @@ -28,11 +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 +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 @@ -53,6 +57,7 @@ func main() { os.Exit(2) } + api.SetUserAgent(version, buildDate, platform) maybeOverrideDefaultHost(cmdFactory, cfg) if !cmdFactory.IO.ColorEnabled() { @@ -75,7 +80,7 @@ func main() { } } - rootCmd := commands.NewCmdRoot(cmdFactory, version, build) + rootCmd := commands.NewCmdRoot(cmdFactory, version, buildDate) // Set Debug mode debugMode, _ = cfg.Get("", "debug") diff --git a/commands/api/http_test.go b/commands/api/http_test.go index 2fedea98f1c6121ae6d41073caedcfb9cd90012c..86dbe1c44db7cd46df3ebc475852caa7bfb29cb8 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" @@ -97,6 +98,9 @@ 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", "") } @@ -146,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 - GitLab CLI\r\n", + headers: fmt.Sprintf("Private-Token: OTOKEN\r\nUser-Agent: %s\r\n", versionString), }, }, { @@ -164,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 - GitLab CLI\r\n", + headers: fmt.Sprintf("Private-Token: OTOKEN\r\nUser-Agent: %s\r\n", versionString), }, }, { @@ -184,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 - GitLab CLI\r\n", + headers: fmt.Sprintf("Private-Token: OTOKEN\r\nUser-Agent: %s\r\n", versionString), }, }, { @@ -204,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 - GitLab CLI\r\n", + headers: fmt.Sprintf("Content-Type: application/json; charset=utf-8\r\nPrivate-Token: OTOKEN\r\nUser-Agent: %s\r\n", versionString), }, }, { @@ -225,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 - GitLab CLI\r\n", + headers: fmt.Sprintf("Accept: application/json\r\nContent-Type: text/plain\r\nPrivate-Token: OTOKEN\r\nUser-Agent: %s\r\n", versionString), }, }, }