From 376a4b671c97f9d2056338b41cb573f3e6471626 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Fri, 12 Jun 2020 17:54:14 +0200 Subject: [PATCH 1/2] version command adding azure integration beta version in textual output --- cli/cmd/context/create_test.go | 5 +++-- cli/cmd/version.go | 40 ++++++++++++++++++++++++++++++++++ cli/dockerclassic/exec.go | 6 +++++ cli/main.go | 2 ++ tests/e2e/e2e_test.go | 6 +++++ 5 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 cli/cmd/version.go diff --git a/cli/cmd/context/create_test.go b/cli/cmd/context/create_test.go index 2d9c06a35..620d2981d 100644 --- a/cli/cmd/context/create_test.go +++ b/cli/cmd/context/create_test.go @@ -6,10 +6,11 @@ import ( "github.com/docker/api/context/store" - _ "github.com/docker/api/example" - "github.com/docker/api/tests/framework" . "github.com/onsi/gomega" "github.com/stretchr/testify/suite" + + _ "github.com/docker/api/example" + "github.com/docker/api/tests/framework" ) type PsSuite struct { diff --git a/cli/cmd/version.go b/cli/cmd/version.go new file mode 100644 index 000000000..5bebac4fc --- /dev/null +++ b/cli/cmd/version.go @@ -0,0 +1,40 @@ +package cmd + +import ( + "fmt" + "strings" + + "github.com/spf13/cobra" + + "github.com/docker/api/cli/dockerclassic" +) + +const cliVersion = "0.1-beta" + +// VersionCommand command to display version +func VersionCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "version", + Short: "Show the Docker version information", + Args: cobra.MaximumNArgs(0), + RunE: runVersion, + } + // define flags for backward compatibility with docker-classic + flags := cmd.Flags() + flags.String("format", "", "Format the output using the given Go template") + flags.String("kubeconfig", "", "Kubernetes config file") + + return cmd +} + +func runVersion(cmd *cobra.Command, args []string) error { + versionResult, _ := dockerclassic.ExecSilent(cmd.Context()) + // we don't want to fail on error, there is an error if the engine is not available but it displays client version info + // Still, technically the [] byte versionResult could be nil, just let the original command display what it has to display + if versionResult == nil { + return dockerclassic.ExecCmd(cmd) + } + var s string = string(versionResult) + fmt.Print(strings.Replace(s, "\n Version:", "\n Azure integration "+cliVersion+"\n Version:", 1)) + return nil +} diff --git a/cli/dockerclassic/exec.go b/cli/dockerclassic/exec.go index 50411b458..434233f20 100644 --- a/cli/dockerclassic/exec.go +++ b/cli/dockerclassic/exec.go @@ -57,3 +57,9 @@ func IsDefaultContextCommand(dockerCommand string) bool { contains := strings.Contains(output, "Usage:\tdocker "+dockerCommand) return contains } + +// ExecSilent executes a command and do redirect output to stdOut, return output +func ExecSilent(ctx context.Context) ([]byte, error) { + cmd := exec.CommandContext(ctx, ClassicCliName, os.Args[1:]...) + return cmd.CombinedOutput() +} diff --git a/cli/main.go b/cli/main.go index 10e50f134..0af5bbfb1 100644 --- a/cli/main.go +++ b/cli/main.go @@ -64,6 +64,7 @@ var ( "context": {}, "login": {}, "serve": {}, + "version": {}, } ) @@ -119,6 +120,7 @@ func main() { cmd.RmCommand(), compose.Command(), login.Command(), + cmd.VersionCommand(), ) helpFunc := root.HelpFunc() diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index bd39c83e9..0ed7b973f 100644 --- a/tests/e2e/e2e_test.go +++ b/tests/e2e/e2e_test.go @@ -141,6 +141,12 @@ func (s *E2eSuite) TestDisplayFriendlyErrorMessageForLegacyCommands() { Expect(err).NotTo(BeNil()) } +func (s *E2eSuite) TestDisplaysAdditionalLineInDockerVersion() { + output := s.NewDockerCommand("version").ExecOrDie() + Expect(output).To(ContainSubstring(`Azure integration 0.1-beta + Version: `)) +} + func (s *E2eSuite) TestMockBackend() { It("creates a new test context to hardcoded example backend", func() { s.NewDockerCommand("context", "create", "test-example", "example").ExecOrDie() From afa205be9517829239a05044461146d6042bede0 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Mon, 15 Jun 2020 11:43:34 +0200 Subject: [PATCH 2/2] =?UTF-8?q?Server=20version=20:=20will=20be=201.0.0-be?= =?UTF-8?q?ta1,=20-beta2,=20=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cli/cmd/version.go | 2 +- tests/e2e/e2e_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/cmd/version.go b/cli/cmd/version.go index 5bebac4fc..53df5c75f 100644 --- a/cli/cmd/version.go +++ b/cli/cmd/version.go @@ -9,7 +9,7 @@ import ( "github.com/docker/api/cli/dockerclassic" ) -const cliVersion = "0.1-beta" +const cliVersion = "1.0.0-beta" // VersionCommand command to display version func VersionCommand() *cobra.Command { diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index 0ed7b973f..249e7df5c 100644 --- a/tests/e2e/e2e_test.go +++ b/tests/e2e/e2e_test.go @@ -143,7 +143,7 @@ func (s *E2eSuite) TestDisplayFriendlyErrorMessageForLegacyCommands() { func (s *E2eSuite) TestDisplaysAdditionalLineInDockerVersion() { output := s.NewDockerCommand("version").ExecOrDie() - Expect(output).To(ContainSubstring(`Azure integration 0.1-beta + Expect(output).To(ContainSubstring(`Azure integration 1.0.0-beta Version: `)) }