diff --git a/Dockerfile b/Dockerfile index 9acf6a8f7..f70a0855d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,7 +23,7 @@ ARG TARGETARCH RUN go get ./... RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \ - go build -ldflags "-X main.metadataString=container" + go build -ldflags "-X github.com/GoogleCloudPlatform/cloud-sql-proxy/v2/cmd.metadataString=container" # Final Stage FROM gcr.io/distroless/static:nonroot diff --git a/Dockerfile.alpine b/Dockerfile.alpine index 00e38c03d..164336e58 100644 --- a/Dockerfile.alpine +++ b/Dockerfile.alpine @@ -23,7 +23,7 @@ ARG TARGETARCH RUN go get ./... RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} \ - go build -ldflags "-X main.metadataString=container.alpine" + go build -ldflags "-X github.com/GoogleCloudPlatform/cloud-sql-proxy/v2/cmd.metadataString=container.alpine" # Final stage FROM alpine:3 diff --git a/Dockerfile.bullseye b/Dockerfile.bullseye index af50feb5a..e12530e59 100644 --- a/Dockerfile.bullseye +++ b/Dockerfile.bullseye @@ -23,7 +23,7 @@ ARG TARGETARCH RUN go get ./... RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} \ - go build -ldflags "-X main.metadataString=container.bullseye" + go build -ldflags "-X github.com/GoogleCloudPlatform/cloud-sql-proxy/v2/cmd.metadataString=container.bullseye" # Final stage FROM debian:bullseye diff --git a/Dockerfile.buster b/Dockerfile.buster index 924010962..14b220dfe 100644 --- a/Dockerfile.buster +++ b/Dockerfile.buster @@ -23,7 +23,7 @@ ARG TARGETARCH RUN go get ./... RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} \ - go build -ldflags "-X main.metadataString=container.buster" + go build -ldflags "-X github.com/GoogleCloudPlatform/cloud-sql-proxy/v2/cmd.metadataString=container.buster" # Final stage FROM debian:buster diff --git a/cmd/root.go b/cmd/root.go index 0d53517b4..d7d1a87ab 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -47,13 +47,25 @@ import ( var ( // versionString indicates the version of this library. //go:embed version.txt - versionString string - defaultUserAgent string + versionString string + // metadataString indiciates additional build or distribution metadata. + metadataString string + userAgent string ) func init() { - versionString = strings.TrimSpace(versionString) - defaultUserAgent = "cloud-sql-proxy/" + versionString + versionString = semanticVersion() + userAgent = "cloud-sql-proxy/" + versionString +} + +// semanticVersion returns the version of the proxy including an compile-time +// metadata. +func semanticVersion() string { + v := strings.TrimSpace(versionString) + if metadataString != "" { + v += "+" + metadataString + } + return v } // Execute adds all child commands to the root command and sets flags appropriately. @@ -301,7 +313,7 @@ func NewCommand(opts ...Option) *Command { logger: logger, cleanup: func() error { return nil }, conf: &proxy.Config{ - UserAgent: defaultUserAgent, + UserAgent: userAgent, }, } for _, o := range opts { @@ -495,8 +507,8 @@ func parseConfig(cmd *Command, conf *proxy.Config, args []string) error { } if userHasSet("user-agent") { - defaultUserAgent += " " + cmd.conf.OtherUserAgents - conf.UserAgent = defaultUserAgent + userAgent += " " + cmd.conf.OtherUserAgents + conf.UserAgent = userAgent } if userHasSet("sqladmin-api-endpoint") && conf.APIEndpointURL != "" { diff --git a/cmd/root_test.go b/cmd/root_test.go index 64d43821a..b06529af7 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -34,7 +34,7 @@ import ( func withDefaults(c *proxy.Config) *proxy.Config { if c.UserAgent == "" { - c.UserAgent = defaultUserAgent + c.UserAgent = userAgent } if c.Addr == "" { c.Addr = "127.0.0.1"