Skip to content

Commit 6589ef3

Browse files
fix: replace the hardcoded default version string (#1836)
Remove empty line fixed cloudsqlutil.go format fix test code Fixed the typo in the test message
1 parent 52d5fa0 commit 6589ef3

File tree

7 files changed

+59
-42
lines changed

7 files changed

+59
-42
lines changed

.build/release_artifacts.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ PROJ_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )"/.. >/dev/null 2>&1 && pwd )"
2121
cd $PROJ_ROOT
2222

2323
# get the current version
24-
export VERSION=$(cat cmd/cloud_sql_proxy/version.txt)
24+
export VERSION=$(cat proxy/util/version.txt)
2525
if [ -z "$VERSION" ]; then
2626
echo "error: No version.txt found in $PROJ_ROOT"
2727
exit 1

cmd/cloud_sql_proxy/cloud_sql_proxy.go

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -274,28 +274,6 @@ Information for all flags:
274274

275275
var defaultTmp = filepath.Join(os.TempDir(), "cloudsql-proxy-tmp")
276276

277-
// versionString indiciates the version of the proxy currently in use.
278-
//
279-
//go:embed version.txt
280-
var versionString string
281-
282-
// metadataString indiciates additional build or distribution metadata.
283-
var metadataString = ""
284-
285-
// semanticVersion returns the version of the proxy in a semver format.
286-
func semanticVersion() string {
287-
v := strings.TrimSpace(versionString)
288-
if metadataString != "" {
289-
v += "+" + metadataString
290-
}
291-
return v
292-
}
293-
294-
// userAgentFromVersionString returns an appropriate user agent string for identifying this proxy process.
295-
func userAgentFromVersionString() string {
296-
return "cloud_sql_proxy/" + semanticVersion()
297-
}
298-
299277
const accountErrorSuffix = `Please create a new VM with Cloud SQL access (scope) enabled under "Identity and API access". Alternatively, create a new "service account key" and specify it using the -credential_file parameter`
300278

301279
type stringListValue []string
@@ -548,7 +526,7 @@ use the value from the flag, Not compatible with -fuse.`,
548526
flag.Parse()
549527

550528
if *version {
551-
fmt.Println("Cloud SQL Auth proxy:", semanticVersion())
529+
fmt.Println("Cloud SQL Auth proxy:", util.SemanticVersion())
552530
return 0
553531
}
554532

@@ -664,7 +642,7 @@ use the value from the flag, Not compatible with -fuse.`,
664642
Certs: certs.NewCertSourceOpts(client, certs.RemoteOpts{
665643
APIBasePath: *host,
666644
IgnoreRegion: !*checkRegion,
667-
UserAgent: userAgentFromVersionString(),
645+
UserAgent: util.UserAgentFromVersionString(),
668646
IPAddrTypeOpts: ipAddrTypeOptsInput,
669647
EnableIAMLogin: *enableIAMLogin,
670648
TokenSource: tokSrc,

cmd/cloud_sql_proxy/cloud_sql_proxy_test.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,11 @@
1515
package main
1616

1717
import (
18-
"os"
19-
"strings"
2018
"testing"
2119

2220
"golang.org/x/net/context"
2321
)
2422

25-
func TestVersionStripsNewline(t *testing.T) {
26-
v, err := os.ReadFile("version.txt")
27-
if err != nil {
28-
t.Fatalf("failed to read verion.txt: %v", err)
29-
}
30-
want := strings.TrimSpace(string(v))
31-
32-
if got := semanticVersion(); got != want {
33-
t.Fatalf("want = %q, got = %q", want, got)
34-
}
35-
}
36-
3723
func TestAuthenticatedClient(t *testing.T) {
3824
tcs := []struct {
3925
desc string

proxy/certs/certs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737
sqladmin "google.golang.org/api/sqladmin/v1beta4"
3838
)
3939

40-
const defaultUserAgent = "custom cloud_sql_proxy version >= 1.10"
40+
var defaultUserAgent = util.UserAgentFromVersionString()
4141

4242
// NewCertSource returns a CertSource which can be used to authenticate using
4343
// the provided client, which must not be nil.

proxy/util/cloudsqlutil.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
// Package util contains utility functions for use throughout the Cloud SQL Auth proxy.
1616
package util
1717

18-
import "strings"
18+
import (
19+
_ "embed"
20+
"strings"
21+
)
1922

2023
// SplitName splits a fully qualified instance into its project, region, and
2124
// instance name components. While we make the transition to regionalized
@@ -44,3 +47,25 @@ func SplitName(instance string) (project, region, name string) {
4447
return spl[0], spl[1], spl[2]
4548
}
4649
}
50+
51+
// versionString indiciates the version of the proxy currently in use.
52+
//
53+
//go:embed version.txt
54+
var versionString string
55+
56+
// metadataString indiciates additional build or distribution metadata.
57+
var metadataString = ""
58+
59+
// semanticVersion returns the version of the proxy in a semver format.
60+
func SemanticVersion() string {
61+
v := strings.TrimSpace(versionString)
62+
if metadataString != "" {
63+
v += "+" + metadataString
64+
}
65+
return v
66+
}
67+
68+
// userAgentFromVersionString returns an appropriate user agent string for identifying this proxy process.
69+
func UserAgentFromVersionString() string {
70+
return "cloud_sql_proxy/" + SemanticVersion()
71+
}

proxy/util/cloudsqlutil_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,35 @@
1414

1515
package util
1616

17-
import "testing"
17+
import (
18+
"os"
19+
"strings"
20+
"testing"
21+
)
22+
23+
func TestSemanticVersion(t *testing.T) {
24+
v, err := os.ReadFile("version.txt")
25+
if err != nil {
26+
t.Fatalf("failed to read version.txt: %v", err)
27+
}
28+
want := strings.TrimSpace(string(v))
29+
30+
if got := SemanticVersion(); got != want {
31+
t.Fatalf("want = %q, got = %q", want, got)
32+
}
33+
}
34+
35+
func TestUserAgentFromVersionString(t *testing.T) {
36+
v, err := os.ReadFile("version.txt")
37+
if err != nil {
38+
t.Fatalf("failed to read version.txt: %v", err)
39+
}
40+
want := "cloud_sql_proxy/" + strings.TrimSpace(string(v))
41+
42+
if got := UserAgentFromVersionString(); got != want {
43+
t.Fatalf("want = %q, got = %q", want, got)
44+
}
45+
}
1846

1947
func TestSplitName(t *testing.T) {
2048
table := []struct{ in, wantProj, wantRegion, wantInstance string }{

0 commit comments

Comments
 (0)