From 10632b008b12e65b81ae3da1f63cdf3ef424ed14 Mon Sep 17 00:00:00 2001 From: Ulysses Souza Date: Wed, 7 Oct 2020 15:33:27 +0200 Subject: [PATCH] Add version to UserAgent on ACI This also refactors the Version insertion to make it global Signed-off-by: Ulysses Souza --- aci/login/client.go | 5 ++--- builder.Makefile | 4 +++- cli/cmd/version.go | 11 ++++++----- cli/main.go | 8 ++------ ecs/sdk.go | 3 ++- internal/variables.go | 29 +++++++++++++++++++++++++++++ 6 files changed, 44 insertions(+), 16 deletions(-) create mode 100644 internal/variables.go diff --git a/aci/login/client.go b/aci/login/client.go index da827b623..6b49c7328 100644 --- a/aci/login/client.go +++ b/aci/login/client.go @@ -27,10 +27,9 @@ import ( "github.com/pkg/errors" "github.com/docker/compose-cli/errdefs" + "github.com/docker/compose-cli/internal" ) -const userAgent = "docker-cli" - // NewContainerGroupsClient get client toi manipulate containerGrouos func NewContainerGroupsClient(subscriptionID string) (containerinstance.ContainerGroupsClient, error) { containerGroupsClient := containerinstance.NewContainerGroupsClient(subscriptionID) @@ -45,7 +44,7 @@ func NewContainerGroupsClient(subscriptionID string) (containerinstance.Containe } func setupClient(aciClient *autorest.Client) error { - aciClient.UserAgent = userAgent + aciClient.UserAgent = internal.UserAgentName + "/" + internal.Version auth, err := NewAuthorizerFromLogin() if err != nil { return err diff --git a/builder.Makefile b/builder.Makefile index 90d17de64..c89b23820 100644 --- a/builder.Makefile +++ b/builder.Makefile @@ -15,6 +15,8 @@ GOOS?=$(shell go env GOOS) GOARCH?=$(shell go env GOARCH) +PKG_NAME := github.com/docker/compose-cli + PROTOS=$(shell find protos -name \*.proto) EXTENSION:= @@ -26,7 +28,7 @@ STATIC_FLAGS=CGO_ENABLED=0 GIT_TAG?=$(shell git describe --tags --match "v[0-9]*") -LDFLAGS="-s -w -X main.version=${GIT_TAG}" +LDFLAGS="-s -w -X $(PKG_NAME)/internal.Version=${GIT_TAG}" GO_BUILD=$(STATIC_FLAGS) go build -trimpath -ldflags=$(LDFLAGS) BINARY?=bin/docker diff --git a/cli/cmd/version.go b/cli/cmd/version.go index 5c66439bf..77bf060ce 100644 --- a/cli/cmd/version.go +++ b/cli/cmd/version.go @@ -26,18 +26,19 @@ import ( "github.com/docker/compose-cli/cli/cmd/mobyflags" "github.com/docker/compose-cli/cli/mobycli" "github.com/docker/compose-cli/formatter" + "github.com/docker/compose-cli/internal" ) const formatOpt = "format" // VersionCommand command to display version -func VersionCommand(version string) *cobra.Command { +func VersionCommand() *cobra.Command { cmd := &cobra.Command{ Use: "version", Short: "Show the Docker version information", Args: cobra.MaximumNArgs(0), Run: func(cmd *cobra.Command, _ []string) { - runVersion(cmd, version) + runVersion(cmd) }, } // define flags for backward compatibility with com.docker.cli @@ -49,15 +50,15 @@ func VersionCommand(version string) *cobra.Command { return cmd } -func runVersion(cmd *cobra.Command, version string) { +func runVersion(cmd *cobra.Command) { var versionString string format := strings.ToLower(strings.ReplaceAll(cmd.Flag(formatOpt).Value.String(), " ", "")) - displayedVersion := strings.TrimPrefix(version, "v") + displayedVersion := strings.TrimPrefix(internal.Version, "v") // Replace is preferred in this case to keep the order. switch format { case formatter.PRETTY, "": versionString = strings.Replace(getOutFromMoby(cmd, fixedPrettyArgs(os.Args[1:])...), - "\n Version:", "\n Cloud integration: "+displayedVersion+"\n Version:", 1) + "\n Version:", "\n Cloud integration: "+displayedVersion+"\n Version:", 1) case formatter.JSON, formatter.TemplateJSON: // Try to catch full JSON formats versionString = strings.Replace(getOutFromMoby(cmd, fixedJSONArgs(os.Args[1:])...), `"Version":`, fmt.Sprintf(`"CloudIntegration":%q,"Version":`, displayedVersion), 1) diff --git a/cli/main.go b/cli/main.go index ea6d3e4cd..2de093e1c 100644 --- a/cli/main.go +++ b/cli/main.go @@ -37,7 +37,7 @@ import ( "github.com/docker/compose-cli/cli/cmd/login" "github.com/docker/compose-cli/cli/cmd/logout" "github.com/docker/compose-cli/cli/cmd/run" - volume "github.com/docker/compose-cli/cli/cmd/volume" + "github.com/docker/compose-cli/cli/cmd/volume" "github.com/docker/compose-cli/cli/mobycli" cliopts "github.com/docker/compose-cli/cli/options" "github.com/docker/compose-cli/config" @@ -54,10 +54,6 @@ import ( _ "github.com/docker/compose-cli/local" ) -var ( - version = "dev" -) - var ( contextAgnosticCommands = map[string]struct{}{ "compose": {}, @@ -122,7 +118,7 @@ func main() { cmd.InspectCommand(), login.Command(), logout.Command(), - cmd.VersionCommand(version), + cmd.VersionCommand(), cmd.StopCommand(), cmd.KillCommand(), cmd.SecretCommand(), diff --git a/ecs/sdk.go b/ecs/sdk.go index 5b98ad48e..f43f37ecd 100644 --- a/ecs/sdk.go +++ b/ecs/sdk.go @@ -28,6 +28,7 @@ import ( "github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/secrets" + "github.com/docker/compose-cli/internal" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/request" @@ -69,7 +70,7 @@ type sdk struct { func newSDK(sess *session.Session) sdk { sess.Handlers.Build.PushBack(func(r *request.Request) { - request.AddToUserAgent(r, "Docker CLI") + request.AddToUserAgent(r, internal.ECSUserAgentName+"/"+internal.Version) }) return sdk{ ECS: ecs.New(sess), diff --git a/internal/variables.go b/internal/variables.go new file mode 100644 index 000000000..ddb705412 --- /dev/null +++ b/internal/variables.go @@ -0,0 +1,29 @@ +/* + Copyright 2020 Docker Compose CLI authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package internal + +const ( + // UserAgentName is the default user agent used by the cli + UserAgentName = "docker-cli" + // ECSUserAgentName is the ECS specific user agent used by the cli + ECSUserAgentName = "Docker CLI" +) + +var ( + // Version is the version of the CLI injected in compilation time + Version = "dev" +)