Skip to content

Commit

Permalink
fix: use user-agent as flag name (#1561)
Browse files Browse the repository at this point in the history
  • Loading branch information
enocom authored Nov 30, 2022
1 parent ce93d4a commit e1b2f7e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
20 changes: 10 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 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", "",
Expand Down Expand Up @@ -496,9 +496,9 @@ 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
conf.UserAgent = defaultUserAgent
}

if userHasSet("sqladmin-api-endpoint") && conf.APIEndpointURL != "" {
Expand Down
15 changes: 7 additions & 8 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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",
},
Expand Down

0 comments on commit e1b2f7e

Please sign in to comment.