From cfd6d2f5d1c2d5abd38228127f7c95c53f1a1812 Mon Sep 17 00:00:00 2001 From: aiordache Date: Tue, 28 Jul 2020 16:30:08 +0200 Subject: [PATCH 1/3] Error out for contexts not implementing compose command Signed-off-by: aiordache --- cli/mobycli/exec.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cli/mobycli/exec.go b/cli/mobycli/exec.go index ec4c89ec..33ce7140 100644 --- a/cli/mobycli/exec.go +++ b/cli/mobycli/exec.go @@ -18,6 +18,7 @@ package mobycli import ( "context" + "errors" "fmt" "os" "os/exec" @@ -51,6 +52,12 @@ func mustDelegateToMoby(ctxType string) bool { // Exec delegates to com.docker.cli if on moby context func Exec(ctx context.Context) { + if os.Args[1] == "compose" { + // command is not implemented for moby or aws context + fmt.Fprintln(os.Stderr, errors.New("'compose' command is not implemented for the context in use")) + os.Exit(1) + } + cmd := exec.CommandContext(ctx, ComDockerCli, os.Args[1:]...) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout From 5a26b257099844f46fdc53b3b94e399032027e1c Mon Sep 17 00:00:00 2001 From: aiordache Date: Wed, 29 Jul 2020 11:58:09 +0200 Subject: [PATCH 2/3] handle compose on all context types Signed-off-by: aiordache --- cli/cmd/compose/compose.go | 21 +++++++++++++++++++++ cli/main.go | 6 +++--- cli/mobycli/exec.go | 7 ------- context/store/store.go | 3 +++ 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/cli/cmd/compose/compose.go b/cli/cmd/compose/compose.go index 6b274728..b8c9ea67 100644 --- a/cli/cmd/compose/compose.go +++ b/cli/cmd/compose/compose.go @@ -17,6 +17,11 @@ package compose import ( + apicontext "github.com/docker/api/context" + "github.com/docker/api/context/store" + "github.com/docker/api/errdefs" + "github.com/pkg/errors" + "github.com/spf13/cobra" ) @@ -25,6 +30,22 @@ func Command() *cobra.Command { command := &cobra.Command{ Short: "Docker Compose", Use: "compose", + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + currentContext := apicontext.CurrentContext(cmd.Context()) + s := store.ContextStore(cmd.Context()) + cc, err := s.Get(currentContext) + if err != nil { + return err + } + switch cc.Type() { + case store.AciContextType: + return nil + case store.AwsContextType: + return errors.New("use 'docker ecs compose' on context type " + cc.Type()) + default: + return errors.Wrapf(errdefs.ErrNotImplemented, "compose command not supported on context type %q", cc.Type()) + } + }, } command.AddCommand( diff --git a/cli/main.go b/cli/main.go index 03a73698..b61622da 100644 --- a/cli/main.go +++ b/cli/main.go @@ -59,6 +59,7 @@ var ( var ( ownCommands = map[string]struct{}{ + "compose": {}, "context": {}, "login": {}, "logout": {}, @@ -181,7 +182,6 @@ func main() { if errors.Is(ctx.Err(), context.Canceled) { os.Exit(130) } - // Context should always be handled by new CLI requiredCmd, _, _ := root.Find(os.Args[1:]) if requiredCmd != nil && isOwnCommand(requiredCmd) { @@ -196,7 +196,7 @@ func main() { func exit(err error) { if errors.Is(err, errdefs.ErrLoginRequired) { - fmt.Fprintln(os.Stderr, fmt.Errorf("%v", err)) + fmt.Fprintln(os.Stderr, err) os.Exit(errdefs.ExitCodeLoginRequired) } fatal(err) @@ -242,6 +242,6 @@ func determineCurrentContext(flag string, configDir string) string { } func fatal(err error) { - fmt.Fprint(os.Stderr, err) + fmt.Fprintln(os.Stderr, err) os.Exit(1) } diff --git a/cli/mobycli/exec.go b/cli/mobycli/exec.go index 33ce7140..ec4c89ec 100644 --- a/cli/mobycli/exec.go +++ b/cli/mobycli/exec.go @@ -18,7 +18,6 @@ package mobycli import ( "context" - "errors" "fmt" "os" "os/exec" @@ -52,12 +51,6 @@ func mustDelegateToMoby(ctxType string) bool { // Exec delegates to com.docker.cli if on moby context func Exec(ctx context.Context) { - if os.Args[1] == "compose" { - // command is not implemented for moby or aws context - fmt.Fprintln(os.Stderr, errors.New("'compose' command is not implemented for the context in use")) - os.Exit(1) - } - cmd := exec.CommandContext(ctx, ComDockerCli, os.Args[1:]...) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout diff --git a/context/store/store.go b/context/store/store.go index 88bca079..a91cb0bc 100644 --- a/context/store/store.go +++ b/context/store/store.go @@ -140,6 +140,9 @@ func New(opts ...Opt) (Store, error) { // Get returns the context with the given name func (s *store) Get(name string) (*DockerContext, error) { + if name == "default" { + return dockerDefaultContext() + } meta := filepath.Join(s.root, contextsDir, metadataDir, contextDirOf(name), metaFile) m, err := read(meta) if os.IsNotExist(err) { From 648f0139df3ba23111fce431e4379ee431cc831b Mon Sep 17 00:00:00 2001 From: aiordache Date: Wed, 29 Jul 2020 14:43:41 +0200 Subject: [PATCH 3/3] add e2e test for compose up on default context Signed-off-by: aiordache --- cli/cmd/compose/compose.go | 6 +++--- tests/e2e/e2e_test.go | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/cli/cmd/compose/compose.go b/cli/cmd/compose/compose.go index b8c9ea67..6beac425 100644 --- a/cli/cmd/compose/compose.go +++ b/cli/cmd/compose/compose.go @@ -17,12 +17,12 @@ package compose import ( + "github.com/pkg/errors" + "github.com/spf13/cobra" + apicontext "github.com/docker/api/context" "github.com/docker/api/context/store" "github.com/docker/api/errdefs" - "github.com/pkg/errors" - - "github.com/spf13/cobra" ) // Command returns the compose command with its child commands diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index 1dd9f875..f85f0783 100644 --- a/tests/e2e/e2e_test.go +++ b/tests/e2e/e2e_test.go @@ -86,6 +86,15 @@ func (s *E2eSuite) TestContextLsFormat() { Expect(output).To(ContainSubstring(`"Name":"default"`)) } +func (s *E2eSuite) TestComposeOnDefaultContext() { + s.NewDockerCommand("context", "use", "default").ExecOrDie() + output := s.NewDockerCommand("context", "inspect").ExecOrDie() + Expect(output).To(ContainSubstring(`"Name": "default"`)) + output, err := s.NewDockerCommand("compose", "up").Exec() + Expect(err).NotTo(BeNil()) + Expect(output).To(ContainSubstring(`compose command not supported on context type`)) +} + func (s *E2eSuite) TestContextCreateParseErrorDoesNotDelegateToLegacy() { s.Step("should dispay new cli error when parsing context create flags", func() { _, err := s.NewDockerCommand("context", "create", "aci", "--subscription-id", "titi").Exec()