Skip to content

Commit a48953a

Browse files
kurtisvgenocom
authored andcommitted
feat: add '--version' flag (#1161)
1 parent 57a5ff1 commit a48953a

File tree

7 files changed

+82
-10
lines changed

7 files changed

+82
-10
lines changed

.github/release-please.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
handleGHRelease: true
1616
packageName: cloud-sql-proxy
1717
releaseType: simple
18-
versionFile: 'cmd/cloud_sql_proxy/version.txt'
18+
versionFile: 'cmd/version.txt'

cmd/root.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package cmd
1616

1717
import (
1818
"context"
19+
_ "embed"
1920
"errors"
2021
"fmt"
2122
"net"
@@ -31,6 +32,18 @@ import (
3132
"github.com/spf13/cobra"
3233
)
3334

35+
var (
36+
// versionString indicates the version of this library.
37+
//go:embed version.txt
38+
versionString string
39+
userAgent string
40+
)
41+
42+
func init() {
43+
versionString = strings.TrimSpace(versionString)
44+
userAgent = "cloud-sql-auth-proxy/" + versionString
45+
}
46+
3447
// Execute adds all child commands to the root command and sets flags appropriately.
3548
// This is called by main.main(). It only needs to happen once to the rootCmd.
3649
func Execute() {
@@ -56,8 +69,9 @@ func NewCommand() *Command {
5669
}
5770

5871
cmd := &cobra.Command{
59-
Use: "cloud_sql_proxy instance_connection_name...",
60-
Short: "cloud_sql_proxy provides a secure way to authorize connections to Cloud SQL.",
72+
Use: "cloud_sql_proxy instance_connection_name...",
73+
Version: versionString,
74+
Short: "cloud_sql_proxy provides a secure way to authorize connections to Cloud SQL.",
6175
Long: `The Cloud SQL Auth proxy provides IAM-based authorization and encryption when
6276
connecting to Cloud SQL instances. It listens on a local port and forwards connections
6377
to your instance's IP address, providing a secure connection without having to manage
@@ -178,7 +192,8 @@ func runSignalWrapper(cmd *Command) error {
178192
startCh := make(chan *proxy.Client)
179193
go func() {
180194
defer close(startCh)
181-
d, err := cloudsqlconn.NewDialer(ctx, cmd.conf.DialerOpts()...)
195+
opts := append(cmd.conf.DialerOpts(), cloudsqlconn.WithUserAgent(userAgent))
196+
d, err := cloudsqlconn.NewDialer(ctx, opts...)
182197
if err != nil {
183198
shutdownCh <- fmt.Errorf("error initializing dialer: %v", err)
184199
return

cmd/version.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.0.0-dev

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/GoogleCloudPlatform/cloudsql-proxy/v2
33
go 1.16
44

55
require (
6-
cloud.google.com/go/cloudsqlconn v0.2.1-0.20220317212813-c79136972083
6+
cloud.google.com/go/cloudsqlconn v0.2.1-0.20220401153611-87e713b37755
77
cloud.google.com/go/compute v1.7.0
88
github.com/GoogleCloudPlatform/cloudsql-proxy v1.29.0
99
github.com/coreos/go-systemd/v22 v22.3.2

go.sum

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ cloud.google.com/go/bigquery v1.4.0/go.mod h1:S8dzgnTigyfTmLBfrtrhyYhwRxG72rYxvf
3535
cloud.google.com/go/bigquery v1.5.0/go.mod h1:snEHRnqQbz117VIFhE8bmtwIDY80NLUZUMb4Nv6dBIg=
3636
cloud.google.com/go/bigquery v1.7.0/go.mod h1://okPTzCYNXSlb24MZs83e2Do+h+VXtc4gLoIoXIAPc=
3737
cloud.google.com/go/bigquery v1.8.0/go.mod h1:J5hqkt3O0uAFnINi6JXValWIb1v0goeZM77hZzJN/fQ=
38-
cloud.google.com/go/cloudsqlconn v0.2.1-0.20220317212813-c79136972083 h1:wD6r6mYz97+/iS4OuX1tf+8WIRFGQN/nPqFl8/gPQzI=
39-
cloud.google.com/go/cloudsqlconn v0.2.1-0.20220317212813-c79136972083/go.mod h1:7JeNiyYmQKqNFJ+HMceyAiYra2KRVowsLZqPmu1IMe0=
38+
cloud.google.com/go/cloudsqlconn v0.2.1-0.20220401153611-87e713b37755 h1:74ZeQyxqrRSBoytWhFCDqScGNuF4PpCh7fe+BR9eX/I=
39+
cloud.google.com/go/cloudsqlconn v0.2.1-0.20220401153611-87e713b37755/go.mod h1:y/ogms5BQof7JguwhcHsTPfskl7BFxXI8lvyXCQu/5E=
4040
cloud.google.com/go/compute v0.1.0/go.mod h1:GAesmwr110a34z04OlxYkATPBEfVhkymfTBXtfbBFow=
4141
cloud.google.com/go/compute v1.2.0/go.mod h1:xlogom/6gr8RJGBe7nT2eGsQYAFUbbv8dbC29qE3Xmw=
4242
cloud.google.com/go/compute v1.3.0/go.mod h1:cCZiE1NHEtai4wiufUhW8I8S1JKkAnhnQJWM7YD99wM=
@@ -635,7 +635,6 @@ golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBc
635635
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
636636
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
637637
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
638-
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
639638
golang.org/x/sys v0.0.0-20220328115105-d36c6a25d886/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
640639
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
641640
golang.org/x/sys v0.0.0-20220502124256-b6088ccd6cba/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -770,7 +769,6 @@ google.golang.org/api v0.67.0/go.mod h1:ShHKP8E60yPsKNw/w8w+VYaj9H6buA5UqDp8dhbQ
770769
google.golang.org/api v0.68.0/go.mod h1:sOM8pTpwgflXRhz+oC8H2Dr+UcbMqkPPWNJo88Q7TH8=
771770
google.golang.org/api v0.70.0/go.mod h1:Bs4ZM2HGifEvXwd50TtW70ovgJffJYw2oRCOFU/SkfA=
772771
google.golang.org/api v0.71.0/go.mod h1:4PyU6e6JogV1f9eA4voyrTY2batOLdgZ5qZ5HOCc4j8=
773-
google.golang.org/api v0.73.0/go.mod h1:lbd/q6BRFJbdpV6OUCXstVeiI5mL/d3/WifG7iNKnjI=
774772
google.golang.org/api v0.74.0/go.mod h1:ZpfMZOVRMywNyvJFeqL9HRWBgAuRfSjJFpe9QtRRyDs=
775773
google.golang.org/api v0.75.0/go.mod h1:pU9QmyHLnzlpar1Mjt4IbapUCy8J+6HD6GeELN69ljA=
776774
google.golang.org/api v0.78.0/go.mod h1:1Sg78yoMLOhlQTeF+ARBoytAcH1NNyyl390YMy6rKmw=
@@ -857,6 +855,7 @@ google.golang.org/genproto v0.0.0-20220222213610-43724f9ea8cf/go.mod h1:kGP+zUP2
857855
google.golang.org/genproto v0.0.0-20220304144024-325a89244dc8/go.mod h1:kGP+zUP2Ddo0ayMi4YuN7C3WZyJvGLZRh8Z5wnAqvEI=
858856
google.golang.org/genproto v0.0.0-20220310185008-1973136f34c6/go.mod h1:kGP+zUP2Ddo0ayMi4YuN7C3WZyJvGLZRh8Z5wnAqvEI=
859857
google.golang.org/genproto v0.0.0-20220324131243-acbaeb5b85eb/go.mod h1:hAL49I2IFola2sVEjAn7MEwsja0xp51I0tlGAf9hz4E=
858+
google.golang.org/genproto v0.0.0-20220329172620-7be39ac1afc7/go.mod h1:8w6bsBMX6yCPbAVTeqQHvzxW0EIFigd5lZyahWgyfDo=
860859
google.golang.org/genproto v0.0.0-20220407144326-9054f6ed7bac/go.mod h1:8w6bsBMX6yCPbAVTeqQHvzxW0EIFigd5lZyahWgyfDo=
861860
google.golang.org/genproto v0.0.0-20220413183235-5e96e2839df9/go.mod h1:8w6bsBMX6yCPbAVTeqQHvzxW0EIFigd5lZyahWgyfDo=
862861
google.golang.org/genproto v0.0.0-20220414192740-2d67ff6cf2b4/go.mod h1:8w6bsBMX6yCPbAVTeqQHvzxW0EIFigd5lZyahWgyfDo=

testsV2/common_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (p *proxyExec) Wait(ctx context.Context) error {
8989
}
9090
}
9191

92-
// Stop sends the pskill signal to the proxy and returns.
92+
// Done returns true if the proxy has exited.
9393
func (p *proxyExec) Done() bool {
9494
select {
9595
case <-p.done:

testsV2/other_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2022 Google LLC
2+
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// other_test runs various tests that are database agnostic.
16+
package tests
17+
18+
import (
19+
"bufio"
20+
"context"
21+
"os"
22+
"strings"
23+
"testing"
24+
"time"
25+
)
26+
27+
func TestVersion(t *testing.T) {
28+
ctx := context.Background()
29+
30+
data, err := os.ReadFile("../cmd/version.txt")
31+
if err != nil {
32+
t.Fatalf("failed to read version.txt: %v", err)
33+
}
34+
want := strings.TrimSpace(string(data))
35+
36+
// Start the proxy
37+
p, err := StartProxy(ctx, "--version")
38+
if err != nil {
39+
t.Fatalf("proxy start failed: %v", err)
40+
}
41+
defer p.Close()
42+
43+
// Assume the proxy should be able to print "version" relatively quickly
44+
ctx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
45+
defer cancel()
46+
err = p.Wait(ctx)
47+
if err != nil {
48+
t.Fatalf("proxy exited unexpectedly: %v", err)
49+
}
50+
output, err := bufio.NewReader(p.Out).ReadString('\n')
51+
if err != nil {
52+
t.Fatalf("failed to read output from proxy: %v", err)
53+
}
54+
if !strings.Contains(output, want) {
55+
t.Errorf("proxy did not return correct version: want %q, got %q", want, output)
56+
}
57+
}

0 commit comments

Comments
 (0)