From 9111e12f489a2692bd908b5937e0f0ee4b09d233 Mon Sep 17 00:00:00 2001 From: Eno Compton Date: Wed, 30 Nov 2022 11:22:11 -0700 Subject: [PATCH 1/3] fix: use user-agent as flag name --- cmd/root.go | 21 +++++++++++---------- cmd/root_test.go | 15 +++++++-------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 4be1cf2b3..dd0fff2d1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -46,13 +46,13 @@ import ( var ( // versionString indicates the version of this library. //go:embed version.txt - versionString string - userAgent string + versionString string + defaultUserAgent string ) func init() { versionString = strings.TrimSpace(versionString) - userAgent = "cloud-sql-proxy/" + versionString + defaultUserAgent = "cloud-sql-proxy/" + versionString } // Execute adds all child commands to the root command and sets flags appropriately. @@ -86,7 +86,7 @@ type Command struct { httpAddress string httpPort string quiet bool - runtime string + otherUserAgents string // impersonationChain is a comma separated list of one or more service // accounts. The first entry in the chain is the impersonation target. Any @@ -307,7 +307,7 @@ func NewCommand(opts ...Option) *Command { logger: logger, cleanup: func() error { return nil }, conf: &proxy.Config{ - UserAgent: userAgent, + UserAgent: defaultUserAgent, }, } for _, o := range opts { @@ -346,8 +346,8 @@ func NewCommand(opts ...Option) *Command { pflags.BoolP("version", "v", false, "Print the cloud-sql-proxy version") // Global-only flags - pflags.StringVar(&c.runtime, "runtime", "", - "(for internal use only) Runtime and version, e.g. cloud-sql-proxy-operator/0.0.1") + pflags.StringVar(&c.otherUserAgents, "user-agent", "", + "Space separate list of additional user agents, e.g. cloud-sql-proxy-operator/0.0.1") pflags.StringVarP(&c.conf.Token, "token", "t", "", "Use bearer token as a source of IAM credentials.") pflags.StringVarP(&c.conf.CredentialsFile, "credentials-file", "c", "", @@ -496,9 +496,10 @@ func parseConfig(cmd *Command, conf *proxy.Config, args []string) error { cmd.logger.Infof("Ignoring --disable-traces because --telemetry-project was not set") } - if userHasSet("runtime") { - userAgent += " " + cmd.runtime - conf.UserAgent = userAgent + if userHasSet("user-agent") { + defaultUserAgent += " " + cmd.otherUserAgents + fmt.Println(defaultUserAgent) + conf.UserAgent = defaultUserAgent } if userHasSet("sqladmin-api-endpoint") && conf.APIEndpointURL != "" { diff --git a/cmd/root_test.go b/cmd/root_test.go index fe327b64a..d80bb1f52 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -34,7 +34,7 @@ import ( func withDefaults(c *proxy.Config) *proxy.Config { if c.UserAgent == "" { - c.UserAgent = userAgent + c.UserAgent = defaultUserAgent } if c.Addr == "" { c.Addr = "127.0.0.1" @@ -82,9 +82,9 @@ func invokeProxyCommand(args []string) (*Command, error) { return c, err } -func TestUserAgentWithOperatorVersionEnvVar(t *testing.T) { - os.Setenv("CSQL_PROXY_RUNTIME", "cloud-sql-proxy-operator/0.0.1") - defer os.Unsetenv("CSQL_PROXY_RUNTIME") +func TestUserAgentWithVersionEnvVar(t *testing.T) { + os.Setenv("CSQL_PROXY_USER_AGENT", "cloud-sql-proxy-operator/0.0.1") + defer os.Unsetenv("CSQL_PROXY_USER_AGENT") cmd, err := invokeProxyCommand([]string{"proj:region:inst"}) if err != nil { @@ -94,15 +94,14 @@ func TestUserAgentWithOperatorVersionEnvVar(t *testing.T) { want := "cloud-sql-proxy-operator/0.0.1" got := cmd.conf.UserAgent if !strings.Contains(got, want) { - t.Errorf("expected userAgent to contain: %v; got: %v", want, got) + t.Errorf("expected user agent to contain: %v; got: %v", want, got) } } -func TestUserAgentWithOperatorVersionFlag(t *testing.T) { - +func TestUserAgent(t *testing.T) { cmd, err := invokeProxyCommand( []string{ - "--runtime", + "--user-agent", "cloud-sql-proxy-operator/0.0.1", "proj:region:inst", }, From 073837a6ff3cf8185b88528b493555e144c6500e Mon Sep 17 00:00:00 2001 From: Eno Compton Date: Wed, 30 Nov 2022 11:23:38 -0700 Subject: [PATCH 2/3] Remove dev artifact --- cmd/root.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index dd0fff2d1..fdb578dbc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -498,7 +498,6 @@ func parseConfig(cmd *Command, conf *proxy.Config, args []string) error { if userHasSet("user-agent") { defaultUserAgent += " " + cmd.otherUserAgents - fmt.Println(defaultUserAgent) conf.UserAgent = defaultUserAgent } From 61679fe361c380a6cb424bf0ba27a657f825709b Mon Sep 17 00:00:00 2001 From: Eno Compton Date: Wed, 30 Nov 2022 11:24:59 -0700 Subject: [PATCH 3/3] Typo --- cmd/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index fdb578dbc..6716045a4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -347,7 +347,7 @@ func NewCommand(opts ...Option) *Command { // Global-only flags pflags.StringVar(&c.otherUserAgents, "user-agent", "", - "Space separate list of additional user agents, e.g. cloud-sql-proxy-operator/0.0.1") + "Space separated list of additional user agents, e.g. cloud-sql-proxy-operator/0.0.1") pflags.StringVarP(&c.conf.Token, "token", "t", "", "Use bearer token as a source of IAM credentials.") pflags.StringVarP(&c.conf.CredentialsFile, "credentials-file", "c", "",