소스 검색

Merge pull request #207 from docker/version_cli

version command adding azure integration beta version in textual output
Guillaume Tardif 5 년 전
부모
커밋
336df897c1
5개의 변경된 파일57개의 추가작업 그리고 2개의 파일을 삭제
  1. 3 2
      cli/cmd/context/create_test.go
  2. 40 0
      cli/cmd/version.go
  3. 6 0
      cli/dockerclassic/exec.go
  4. 2 0
      cli/main.go
  5. 6 0
      tests/e2e/e2e_test.go

+ 3 - 2
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 {

+ 40 - 0
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
+}

+ 6 - 0
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()
+}

+ 2 - 0
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()

+ 6 - 0
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()