浏览代码

Add docker context inspect command relying on classic docker

Guillaume Tardif 5 年之前
父节点
当前提交
528d47ce08
共有 5 个文件被更改,包括 96 次插入28 次删除
  1. 1 0
      cli/cmd/context/context.go
  2. 47 0
      cli/cmd/context/inspect.go
  3. 36 0
      cli/dockerclassic/exec.go
  4. 5 28
      cli/main.go
  5. 7 0
      tests/e2e/e2e_test.go

+ 1 - 0
cli/cmd/context/context.go

@@ -47,6 +47,7 @@ func Command() *cobra.Command {
 		showCommand(),
 		useCommand(),
 		login.Command(),
+		inspectCommand(),
 	)
 
 	return cmd

+ 47 - 0
cli/cmd/context/inspect.go

@@ -0,0 +1,47 @@
+/*
+	Copyright (c) 2020 Docker Inc.
+
+	Permission is hereby granted, free of charge, to any person
+	obtaining a copy of this software and associated documentation
+	files (the "Software"), to deal in the Software without
+	restriction, including without limitation the rights to use, copy,
+	modify, merge, publish, distribute, sublicense, and/or sell copies
+	of the Software, and to permit persons to whom the Software is
+	furnished to do so, subject to the following conditions:
+
+	The above copyright notice and this permission notice shall be
+	included in all copies or substantial portions of the Software.
+
+	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+	EXPRESS OR IMPLIED,
+	INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+	IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+	HOLDERS BE LIABLE FOR ANY CLAIM,
+	DAMAGES OR OTHER LIABILITY,
+	WHETHER IN AN ACTION OF CONTRACT,
+	TORT OR OTHERWISE,
+	ARISING FROM, OUT OF OR IN CONNECTION WITH
+	THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+package context
+
+import (
+	"github.com/docker/api/cli/dockerclassic"
+
+	"github.com/spf13/cobra"
+)
+
+func inspectCommand() *cobra.Command {
+	cmd := &cobra.Command{
+		Use:   "inspect",
+		Short: "Display detailed information on one or more contexts",
+		Args:  cobra.ExactArgs(1),
+		RunE: func(cmd *cobra.Command, args []string) error {
+			dockerclassic.Exec(cmd.Context())
+			return nil
+		},
+	}
+	return cmd
+}

+ 36 - 0
cli/dockerclassic/exec.go

@@ -0,0 +1,36 @@
+package dockerclassic
+
+import (
+	"context"
+	"fmt"
+	"os"
+	"os/exec"
+
+	apicontext "github.com/docker/api/context"
+	"github.com/docker/api/context/store"
+)
+
+// Exec will delegate the cli command to docker-classic
+func Exec(ctx context.Context) {
+	currentContext := apicontext.CurrentContext(ctx)
+	s := store.ContextStore(ctx)
+
+	_, err := s.Get(currentContext)
+	// Only run original docker command if the current context is not
+	// ours.
+	if err != nil {
+		cmd := exec.CommandContext(ctx, "docker-classic", os.Args[1:]...)
+		cmd.Stdin = os.Stdin
+		cmd.Stdout = os.Stdout
+		cmd.Stderr = os.Stderr
+		if err := cmd.Run(); err != nil {
+			if exiterr, ok := err.(*exec.ExitError); ok {
+				fmt.Fprintln(os.Stderr, exiterr.Error())
+				os.Exit(exiterr.ExitCode())
+			}
+			fmt.Fprintln(os.Stderr, err)
+			os.Exit(1)
+		}
+		os.Exit(0)
+	}
+}

+ 5 - 28
cli/main.go

@@ -32,12 +32,13 @@ import (
 	"fmt"
 	"math/rand"
 	"os"
-	"os/exec"
 	"os/signal"
 	"path/filepath"
 	"syscall"
 	"time"
 
+	"github.com/docker/api/cli/dockerclassic"
+
 	"github.com/pkg/errors"
 	"github.com/sirupsen/logrus"
 	"github.com/spf13/cobra"
@@ -95,7 +96,7 @@ func main() {
 		PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
 			runningOwnCommand = isOwnCommand(cmd)
 			if !runningOwnCommand {
-				execMoby(cmd.Context())
+				dockerclassic.Exec(cmd.Context())
 			}
 			return nil
 		},
@@ -119,7 +120,7 @@ func main() {
 	root.SetHelpFunc(func(cmd *cobra.Command, args []string) {
 		runningOwnCommand = isOwnCommand(cmd)
 		if !runningOwnCommand {
-			execMoby(cmd.Context())
+			dockerclassic.Exec(cmd.Context())
 		}
 		helpFunc(cmd, args)
 	})
@@ -161,7 +162,7 @@ func main() {
 			fmt.Fprintln(os.Stderr, err)
 			os.Exit(1)
 		}
-		execMoby(ctx)
+		dockerclassic.Exec(ctx)
 		fmt.Println(err)
 		os.Exit(1)
 	}
@@ -178,30 +179,6 @@ func newSigContext() (context.Context, func()) {
 	return ctx, cancel
 }
 
-func execMoby(ctx context.Context) {
-	currentContext := apicontext.CurrentContext(ctx)
-	s := store.ContextStore(ctx)
-
-	_, err := s.Get(currentContext)
-	// Only run original docker command if the current context is not
-	// ours.
-	if err != nil {
-		cmd := exec.CommandContext(ctx, "docker-classic", os.Args[1:]...)
-		cmd.Stdin = os.Stdin
-		cmd.Stdout = os.Stdout
-		cmd.Stderr = os.Stderr
-		if err := cmd.Run(); err != nil {
-			if exiterr, ok := err.(*exec.ExitError); ok {
-				fmt.Fprintln(os.Stderr, exiterr.Error())
-				os.Exit(exiterr.ExitCode())
-			}
-			fmt.Fprintln(os.Stderr, err)
-			os.Exit(1)
-		}
-		os.Exit(0)
-	}
-}
-
 func determineCurrentContext(flag string, configDir string) (string, error) {
 	res := flag
 	if res == "" {

+ 7 - 0
tests/e2e/e2e_test.go

@@ -69,6 +69,13 @@ func (s *E2eSuite) TestContextDefault() {
 	})
 }
 
+func (s *E2eSuite) TestContextLegacy() {
+	It("should inspect default", func() {
+		output := s.NewDockerCommand("context", "inspect", "default").ExecOrDie()
+		Expect(output).To(ContainSubstring(`"Name": "default"`))
+	})
+}
+
 func (s *E2eSuite) TestSetupError() {
 	It("should display an error if cannot shell out to docker-classic", func() {
 		err := os.Setenv("PATH", s.BinDir)