Merge pull request #734 from docker/add-version-in-user-agent

Add version to UserAgent on  ACI
This commit is contained in:
Nicolas De loof 2020-10-12 11:11:55 +02:00 committed by GitHub
commit a067882b6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 16 deletions

View File

@ -27,10 +27,9 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/docker/compose-cli/errdefs" "github.com/docker/compose-cli/errdefs"
"github.com/docker/compose-cli/internal"
) )
const userAgent = "docker-cli"
// NewContainerGroupsClient get client toi manipulate containerGrouos // NewContainerGroupsClient get client toi manipulate containerGrouos
func NewContainerGroupsClient(subscriptionID string) (containerinstance.ContainerGroupsClient, error) { func NewContainerGroupsClient(subscriptionID string) (containerinstance.ContainerGroupsClient, error) {
containerGroupsClient := containerinstance.NewContainerGroupsClient(subscriptionID) containerGroupsClient := containerinstance.NewContainerGroupsClient(subscriptionID)
@ -45,7 +44,7 @@ func NewContainerGroupsClient(subscriptionID string) (containerinstance.Containe
} }
func setupClient(aciClient *autorest.Client) error { func setupClient(aciClient *autorest.Client) error {
aciClient.UserAgent = userAgent aciClient.UserAgent = internal.UserAgentName + "/" + internal.Version
auth, err := NewAuthorizerFromLogin() auth, err := NewAuthorizerFromLogin()
if err != nil { if err != nil {
return err return err

View File

@ -15,6 +15,8 @@
GOOS?=$(shell go env GOOS) GOOS?=$(shell go env GOOS)
GOARCH?=$(shell go env GOARCH) GOARCH?=$(shell go env GOARCH)
PKG_NAME := github.com/docker/compose-cli
PROTOS=$(shell find protos -name \*.proto) PROTOS=$(shell find protos -name \*.proto)
EXTENSION:= EXTENSION:=
@ -26,7 +28,7 @@ STATIC_FLAGS=CGO_ENABLED=0
GIT_TAG?=$(shell git describe --tags --match "v[0-9]*") 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) GO_BUILD=$(STATIC_FLAGS) go build -trimpath -ldflags=$(LDFLAGS)
BINARY?=bin/docker BINARY?=bin/docker

View File

@ -26,18 +26,19 @@ import (
"github.com/docker/compose-cli/cli/cmd/mobyflags" "github.com/docker/compose-cli/cli/cmd/mobyflags"
"github.com/docker/compose-cli/cli/mobycli" "github.com/docker/compose-cli/cli/mobycli"
"github.com/docker/compose-cli/formatter" "github.com/docker/compose-cli/formatter"
"github.com/docker/compose-cli/internal"
) )
const formatOpt = "format" const formatOpt = "format"
// VersionCommand command to display version // VersionCommand command to display version
func VersionCommand(version string) *cobra.Command { func VersionCommand() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "version", Use: "version",
Short: "Show the Docker version information", Short: "Show the Docker version information",
Args: cobra.MaximumNArgs(0), Args: cobra.MaximumNArgs(0),
Run: func(cmd *cobra.Command, _ []string) { Run: func(cmd *cobra.Command, _ []string) {
runVersion(cmd, version) runVersion(cmd)
}, },
} }
// define flags for backward compatibility with com.docker.cli // define flags for backward compatibility with com.docker.cli
@ -49,15 +50,15 @@ func VersionCommand(version string) *cobra.Command {
return cmd return cmd
} }
func runVersion(cmd *cobra.Command, version string) { func runVersion(cmd *cobra.Command) {
var versionString string var versionString string
format := strings.ToLower(strings.ReplaceAll(cmd.Flag(formatOpt).Value.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. // Replace is preferred in this case to keep the order.
switch format { switch format {
case formatter.PRETTY, "": case formatter.PRETTY, "":
versionString = strings.Replace(getOutFromMoby(cmd, fixedPrettyArgs(os.Args[1:])...), 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 case formatter.JSON, formatter.TemplateJSON: // Try to catch full JSON formats
versionString = strings.Replace(getOutFromMoby(cmd, fixedJSONArgs(os.Args[1:])...), versionString = strings.Replace(getOutFromMoby(cmd, fixedJSONArgs(os.Args[1:])...),
`"Version":`, fmt.Sprintf(`"CloudIntegration":%q,"Version":`, displayedVersion), 1) `"Version":`, fmt.Sprintf(`"CloudIntegration":%q,"Version":`, displayedVersion), 1)

View File

@ -37,7 +37,7 @@ import (
"github.com/docker/compose-cli/cli/cmd/login" "github.com/docker/compose-cli/cli/cmd/login"
"github.com/docker/compose-cli/cli/cmd/logout" "github.com/docker/compose-cli/cli/cmd/logout"
"github.com/docker/compose-cli/cli/cmd/run" "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" "github.com/docker/compose-cli/cli/mobycli"
cliopts "github.com/docker/compose-cli/cli/options" cliopts "github.com/docker/compose-cli/cli/options"
"github.com/docker/compose-cli/config" "github.com/docker/compose-cli/config"
@ -54,10 +54,6 @@ import (
_ "github.com/docker/compose-cli/local" _ "github.com/docker/compose-cli/local"
) )
var (
version = "dev"
)
var ( var (
contextAgnosticCommands = map[string]struct{}{ contextAgnosticCommands = map[string]struct{}{
"compose": {}, "compose": {},
@ -122,7 +118,7 @@ func main() {
cmd.InspectCommand(), cmd.InspectCommand(),
login.Command(), login.Command(),
logout.Command(), logout.Command(),
cmd.VersionCommand(version), cmd.VersionCommand(),
cmd.StopCommand(), cmd.StopCommand(),
cmd.KillCommand(), cmd.KillCommand(),
cmd.SecretCommand(), cmd.SecretCommand(),

View File

@ -28,6 +28,7 @@ import (
"github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/api/secrets" "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"
"github.com/aws/aws-sdk-go/aws/request" "github.com/aws/aws-sdk-go/aws/request"
@ -69,7 +70,7 @@ type sdk struct {
func newSDK(sess *session.Session) sdk { func newSDK(sess *session.Session) sdk {
sess.Handlers.Build.PushBack(func(r *request.Request) { sess.Handlers.Build.PushBack(func(r *request.Request) {
request.AddToUserAgent(r, "Docker CLI") request.AddToUserAgent(r, internal.ECSUserAgentName+"/"+internal.Version)
}) })
return sdk{ return sdk{
ECS: ecs.New(sess), ECS: ecs.New(sess),

29
internal/variables.go Normal file
View File

@ -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"
)