From bb3500d77bff6f740a6cfc371ed89c905603932e Mon Sep 17 00:00:00 2001 From: aiordache Date: Mon, 3 Aug 2020 17:59:20 +0200 Subject: [PATCH] check with client if context is supported (has a backend) Signed-off-by: aiordache --- cli/cmd/compose/compose.go | 41 +++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/cli/cmd/compose/compose.go b/cli/cmd/compose/compose.go index 6beac425..3bcbb387 100644 --- a/cli/cmd/compose/compose.go +++ b/cli/cmd/compose/compose.go @@ -17,9 +17,12 @@ package compose import ( + "context" + "github.com/pkg/errors" "github.com/spf13/cobra" + "github.com/docker/api/client" apicontext "github.com/docker/api/context" "github.com/docker/api/context/store" "github.com/docker/api/errdefs" @@ -31,20 +34,7 @@ func 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()) - } + return runCompose(cmd.Context()) }, } @@ -55,3 +45,26 @@ func Command() *cobra.Command { return command } + +func runCompose(ctx context.Context) error { + c, err := client.New(ctx) + if err == nil { + composeService := c.ComposeService() + if composeService == nil { + return errors.New("compose not implemented in current context") + } + return nil + } + currentContext := apicontext.CurrentContext(ctx) + s := store.ContextStore(ctx) + cc, err := s.Get(currentContext) + if err != nil { + return err + } + switch cc.Type() { + 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()) + } +}