Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add runtime version to user agent if present #1542

Merged
merged 13 commits into from
Nov 17, 2022
11 changes: 10 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,16 @@ var (

func init() {
versionString = strings.TrimSpace(versionString)
userAgent = "cloud-sql-proxy/" + versionString
userAgent = getUserAgentString()
}

func getUserAgentString() string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go prefers to leave off get for Getters. https://go.dev/doc/effective_go#Getters

userAgentString := "cloud-sql-proxy/" + versionString
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are inside a function about user agent, this could juse be ua, or u, or something similarly short.

operatorVersion, isSet := os.LookupEnv("CLOUD_SQL_PROXY_OPERATOR_VERSION")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here -- a short variable is ok since it's clear what it is. Also, isSet is typically named ok.

if isSet {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if !isSet return?

userAgentString = userAgentString + " cloud-sql-proxy-operator/" + operatorVersion
}
return userAgentString
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down
13 changes: 13 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -81,6 +82,18 @@ func invokeProxyCommand(args []string) (*Command, error) {
return c, err
}

func Test_UserAgentWithOperatorVersion(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've been omitting underscores in test names. Let's remove the underscore here.

os.Setenv("CLOUD_SQL_PROXY_OPERATOR_VERSION", "0.0.1")
defer os.Unsetenv("CLOUD_SQL_PROXY_OPERATOR_VERSION")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be run through go-fmt.


expected := "cloud-sql-proxy-operator/0.0.1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

want is conventional here

userAgentString := getUserAgentString()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got is a common name for variables here.

if !strings.Contains(userAgentString, expected) {
t.Errorf("expected userAgent to contain: %v; got: %v", expected, userAgentString)
}

}

func TestNewCommandArguments(t *testing.T) {
tcs := []struct {
desc string
Expand Down