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..53df5c75f --- /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 = "1.0.0-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 6b63306b9..c097aaba0 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..249e7df5c 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 1.0.0-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()