Skip to content

Commit 123c119

Browse files
kurtisvgenocom
authored andcommitted
feat: add '--version' flag (#1161)
1 parent 1556024 commit 123c119

File tree

8 files changed

+90
-10
lines changed

8 files changed

+90
-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'

.github/workflows/tests.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,3 @@ jobs:
3131
- run: goimports -w .
3232
- run: go mod tidy
3333
- name: Verify no changes from goimports and go mod tidy. If you're reading this and the check has failed, run `goimports -w . && go mod tidy`.
34-
run: git diff --exit-code

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.6.1
88
github.com/GoogleCloudPlatform/cloudsql-proxy v1.29.0
99
github.com/coreos/go-systemd/v22 v22.3.2

go.sum

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ cloud.google.com/go/bigquery v1.4.0/go.mod h1:S8dzgnTigyfTmLBfrtrhyYhwRxG72rYxvf
3434
cloud.google.com/go/bigquery v1.5.0/go.mod h1:snEHRnqQbz117VIFhE8bmtwIDY80NLUZUMb4Nv6dBIg=
3535
cloud.google.com/go/bigquery v1.7.0/go.mod h1://okPTzCYNXSlb24MZs83e2Do+h+VXtc4gLoIoXIAPc=
3636
cloud.google.com/go/bigquery v1.8.0/go.mod h1:J5hqkt3O0uAFnINi6JXValWIb1v0goeZM77hZzJN/fQ=
37-
cloud.google.com/go/cloudsqlconn v0.2.1-0.20220317212813-c79136972083 h1:wD6r6mYz97+/iS4OuX1tf+8WIRFGQN/nPqFl8/gPQzI=
38-
cloud.google.com/go/cloudsqlconn v0.2.1-0.20220317212813-c79136972083/go.mod h1:7JeNiyYmQKqNFJ+HMceyAiYra2KRVowsLZqPmu1IMe0=
37+
cloud.google.com/go/cloudsqlconn v0.2.1-0.20220401153611-87e713b37755 h1:74ZeQyxqrRSBoytWhFCDqScGNuF4PpCh7fe+BR9eX/I=
38+
cloud.google.com/go/cloudsqlconn v0.2.1-0.20220401153611-87e713b37755/go.mod h1:y/ogms5BQof7JguwhcHsTPfskl7BFxXI8lvyXCQu/5E=
3939
cloud.google.com/go/compute v0.1.0/go.mod h1:GAesmwr110a34z04OlxYkATPBEfVhkymfTBXtfbBFow=
4040
cloud.google.com/go/compute v1.2.0/go.mod h1:xlogom/6gr8RJGBe7nT2eGsQYAFUbbv8dbC29qE3Xmw=
4141
cloud.google.com/go/compute v1.3.0/go.mod h1:cCZiE1NHEtai4wiufUhW8I8S1JKkAnhnQJWM7YD99wM=
@@ -614,7 +614,6 @@ golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBc
614614
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
615615
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
616616
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
617-
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
618617
golang.org/x/sys v0.0.0-20220328115105-d36c6a25d886/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
619618
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
620619
golang.org/x/sys v0.0.0-20220429121018-84afa8d3f7b3 h1:kBsBifDikLCf5sUMbcD8p73OinDtAQWQp8+n7FiyzlA=
@@ -743,7 +742,11 @@ google.golang.org/api v0.67.0/go.mod h1:ShHKP8E60yPsKNw/w8w+VYaj9H6buA5UqDp8dhbQ
743742
google.golang.org/api v0.68.0/go.mod h1:sOM8pTpwgflXRhz+oC8H2Dr+UcbMqkPPWNJo88Q7TH8=
744743
google.golang.org/api v0.70.0/go.mod h1:Bs4ZM2HGifEvXwd50TtW70ovgJffJYw2oRCOFU/SkfA=
745744
google.golang.org/api v0.71.0/go.mod h1:4PyU6e6JogV1f9eA4voyrTY2batOLdgZ5qZ5HOCc4j8=
745+
<<<<<<< HEAD
746746
google.golang.org/api v0.73.0/go.mod h1:lbd/q6BRFJbdpV6OUCXstVeiI5mL/d3/WifG7iNKnjI=
747+
=======
748+
google.golang.org/api v0.74.0 h1:ExR2D+5TYIrMphWgs5JCgwRhEDlPDXXrLwHHMgPHTXE=
749+
>>>>>>> 57af4a0 (feat: add '--version' flag (#1161))
747750
google.golang.org/api v0.74.0/go.mod h1:ZpfMZOVRMywNyvJFeqL9HRWBgAuRfSjJFpe9QtRRyDs=
748751
google.golang.org/api v0.75.0/go.mod h1:pU9QmyHLnzlpar1Mjt4IbapUCy8J+6HD6GeELN69ljA=
749752
google.golang.org/api v0.77.0 h1:msijLTxwkJ7Jub5tv9KBVCKtHOQwnvnvkX7ErFFCVxY=
@@ -826,11 +829,16 @@ google.golang.org/genproto v0.0.0-20220222213610-43724f9ea8cf/go.mod h1:kGP+zUP2
826829
google.golang.org/genproto v0.0.0-20220304144024-325a89244dc8/go.mod h1:kGP+zUP2Ddo0ayMi4YuN7C3WZyJvGLZRh8Z5wnAqvEI=
827830
google.golang.org/genproto v0.0.0-20220310185008-1973136f34c6/go.mod h1:kGP+zUP2Ddo0ayMi4YuN7C3WZyJvGLZRh8Z5wnAqvEI=
828831
google.golang.org/genproto v0.0.0-20220324131243-acbaeb5b85eb/go.mod h1:hAL49I2IFola2sVEjAn7MEwsja0xp51I0tlGAf9hz4E=
832+
<<<<<<< HEAD
829833
google.golang.org/genproto v0.0.0-20220407144326-9054f6ed7bac/go.mod h1:8w6bsBMX6yCPbAVTeqQHvzxW0EIFigd5lZyahWgyfDo=
830834
google.golang.org/genproto v0.0.0-20220413183235-5e96e2839df9/go.mod h1:8w6bsBMX6yCPbAVTeqQHvzxW0EIFigd5lZyahWgyfDo=
831835
google.golang.org/genproto v0.0.0-20220414192740-2d67ff6cf2b4/go.mod h1:8w6bsBMX6yCPbAVTeqQHvzxW0EIFigd5lZyahWgyfDo=
832836
google.golang.org/genproto v0.0.0-20220421151946-72621c1f0bd3 h1:SeX3QUcBj3fciwnfPT9kt5gBhFy/FCZtYZ+I/RB8agc=
833837
google.golang.org/genproto v0.0.0-20220421151946-72621c1f0bd3/go.mod h1:8w6bsBMX6yCPbAVTeqQHvzxW0EIFigd5lZyahWgyfDo=
838+
=======
839+
google.golang.org/genproto v0.0.0-20220329172620-7be39ac1afc7 h1:HOL66YCI20JvN2hVk6o2YIp9i/3RvzVUz82PqNr7fXw=
840+
google.golang.org/genproto v0.0.0-20220329172620-7be39ac1afc7/go.mod h1:8w6bsBMX6yCPbAVTeqQHvzxW0EIFigd5lZyahWgyfDo=
841+
>>>>>>> 57af4a0 (feat: add '--version' flag (#1161))
834842
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
835843
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
836844
google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=

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)