From 528d47ce08be040bd3bf2c0969f36f2c2f294f6f Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Fri, 29 May 2020 12:02:07 +0200 Subject: [PATCH] Add docker context inspect command relying on classic docker --- cli/cmd/context/context.go | 1 + cli/cmd/context/inspect.go | 47 ++++++++++++++++++++++++++++++++++++++ cli/dockerclassic/exec.go | 36 +++++++++++++++++++++++++++++ cli/main.go | 33 ++++---------------------- tests/e2e/e2e_test.go | 7 ++++++ 5 files changed, 96 insertions(+), 28 deletions(-) create mode 100644 cli/cmd/context/inspect.go create mode 100644 cli/dockerclassic/exec.go diff --git a/cli/cmd/context/context.go b/cli/cmd/context/context.go index 1d344dca8..ef524d12f 100644 --- a/cli/cmd/context/context.go +++ b/cli/cmd/context/context.go @@ -47,6 +47,7 @@ func Command() *cobra.Command { showCommand(), useCommand(), login.Command(), + inspectCommand(), ) return cmd diff --git a/cli/cmd/context/inspect.go b/cli/cmd/context/inspect.go new file mode 100644 index 000000000..1b4efa783 --- /dev/null +++ b/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 +} diff --git a/cli/dockerclassic/exec.go b/cli/dockerclassic/exec.go new file mode 100644 index 000000000..ab2eec7a4 --- /dev/null +++ b/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) + } +} diff --git a/cli/main.go b/cli/main.go index 72897239d..7684448dd 100644 --- a/cli/main.go +++ b/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 == "" { diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index bef31a8a0..fe4355c85 100644 --- a/tests/e2e/e2e_test.go +++ b/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)