Pārlūkot izejas kodu

`compose up` and other compose commands running on “Moby” context type.

Signed-off-by: Guillaume Tardif <[email protected]>
Guillaume Tardif 5 gadi atpakaļ
vecāks
revīzija
ec5afcfd4d

+ 15 - 1
api/client/client.go

@@ -32,6 +32,15 @@ import (
 
 // New returns a backend client associated with current context
 func New(ctx context.Context) (*Client, error) {
+	return newWithDefaultBackend(ctx, "")
+}
+
+// NewWithDefaultLocalBackend returns a backend client associated with current context or local backend if on default context type
+func NewWithDefaultLocalBackend(ctx context.Context) (*Client, error) {
+	return newWithDefaultBackend(ctx, store.LocalContextType)
+}
+
+func newWithDefaultBackend(ctx context.Context, defaultBackend string) (*Client, error) {
 	currentContext := apicontext.CurrentContext(ctx)
 	s := store.ContextStore(ctx)
 
@@ -40,7 +49,12 @@ func New(ctx context.Context) (*Client, error) {
 		return nil, err
 	}
 
-	service, err := backend.Get(ctx, cc.Type())
+	backendName := cc.Type()
+	if backendName == store.DefaultContextType && defaultBackend != "" {
+		backendName = defaultBackend
+	}
+
+	service, err := backend.Get(ctx, backendName)
 	if err != nil {
 		return nil, err
 	}

+ 1 - 1
cli/cmd/compose/build.go

@@ -45,7 +45,7 @@ func buildCommand() *cobra.Command {
 }
 
 func runBuild(ctx context.Context, opts buildOptions, services []string) error {
-	c, err := client.New(ctx)
+	c, err := client.NewWithDefaultLocalBackend(ctx)
 	if err != nil {
 		return err
 	}

+ 1 - 17
cli/cmd/compose/compose.go

@@ -17,16 +17,12 @@
 package compose
 
 import (
-	"context"
-
 	"github.com/compose-spec/compose-go/cli"
 	"github.com/compose-spec/compose-go/types"
 	"github.com/spf13/cobra"
 	"github.com/spf13/pflag"
 
-	"github.com/docker/compose-cli/api/client"
 	"github.com/docker/compose-cli/context/store"
-	"github.com/docker/compose-cli/errdefs"
 )
 
 type composeOptions struct {
@@ -76,9 +72,6 @@ func Command(contextType string) *cobra.Command {
 	command := &cobra.Command{
 		Short: "Docker Compose",
 		Use:   "compose",
-		PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
-			return checkComposeSupport(cmd.Context())
-		},
 	}
 
 	command.AddCommand(
@@ -90,7 +83,7 @@ func Command(contextType string) *cobra.Command {
 		convertCommand(),
 	)
 
-	if contextType == store.LocalContextType {
+	if contextType == store.LocalContextType || contextType == store.DefaultContextType {
 		command.AddCommand(
 			buildCommand(),
 			pushCommand(),
@@ -101,15 +94,6 @@ func Command(contextType string) *cobra.Command {
 	return command
 }
 
-func checkComposeSupport(ctx context.Context) error {
-	_, err := client.New(ctx)
-	if errdefs.IsNotFoundError(err) {
-		return errdefs.ErrNotImplemented
-	}
-
-	return err
-}
-
 //
 func filter(project *types.Project, services []string) error {
 	if len(services) == 0 {

+ 1 - 1
cli/cmd/compose/convert.go

@@ -46,7 +46,7 @@ func convertCommand() *cobra.Command {
 
 func runConvert(ctx context.Context, opts composeOptions) error {
 	var json []byte
-	c, err := client.New(ctx)
+	c, err := client.NewWithDefaultLocalBackend(ctx)
 	if err != nil {
 		return err
 	}

+ 1 - 1
cli/cmd/compose/down.go

@@ -41,7 +41,7 @@ func downCommand() *cobra.Command {
 }
 
 func runDown(ctx context.Context, opts composeOptions) error {
-	c, err := client.New(ctx)
+	c, err := client.NewWithDefaultLocalBackend(ctx)
 	if err != nil {
 		return err
 	}

+ 1 - 1
cli/cmd/compose/list.go

@@ -43,7 +43,7 @@ func listCommand() *cobra.Command {
 }
 
 func runList(ctx context.Context, opts composeOptions) error {
-	c, err := client.New(ctx)
+	c, err := client.NewWithDefaultLocalBackend(ctx)
 	if err != nil {
 		return err
 	}

+ 1 - 1
cli/cmd/compose/logs.go

@@ -41,7 +41,7 @@ func logsCommand() *cobra.Command {
 }
 
 func runLogs(ctx context.Context, opts composeOptions) error {
-	c, err := client.New(ctx)
+	c, err := client.NewWithDefaultLocalBackend(ctx)
 	if err != nil {
 		return err
 	}

+ 1 - 1
cli/cmd/compose/ps.go

@@ -45,7 +45,7 @@ func psCommand() *cobra.Command {
 }
 
 func runPs(ctx context.Context, opts composeOptions) error {
-	c, err := client.New(ctx)
+	c, err := client.NewWithDefaultLocalBackend(ctx)
 	if err != nil {
 		return err
 	}

+ 1 - 1
cli/cmd/compose/pull.go

@@ -46,7 +46,7 @@ func pullCommand() *cobra.Command {
 }
 
 func runPull(ctx context.Context, opts pullOptions, services []string) error {
-	c, err := client.New(ctx)
+	c, err := client.NewWithDefaultLocalBackend(ctx)
 	if err != nil {
 		return err
 	}

+ 1 - 1
cli/cmd/compose/push.go

@@ -46,7 +46,7 @@ func pushCommand() *cobra.Command {
 }
 
 func runPush(ctx context.Context, opts pushOptions, services []string) error {
-	c, err := client.New(ctx)
+	c, err := client.NewWithDefaultLocalBackend(ctx)
 	if err != nil {
 		return err
 	}

+ 2 - 2
cli/cmd/compose/up.go

@@ -38,7 +38,7 @@ func upCommand(contextType string) *cobra.Command {
 		Use: "up [SERVICE...]",
 		RunE: func(cmd *cobra.Command, args []string) error {
 			switch contextType {
-			case store.LocalContextType:
+			case store.LocalContextType, store.DefaultContextType:
 				return runCreateStart(cmd.Context(), opts, args)
 			default:
 				return runUp(cmd.Context(), opts, args)
@@ -100,7 +100,7 @@ func runCreateStart(ctx context.Context, opts composeOptions, services []string)
 }
 
 func setup(ctx context.Context, opts composeOptions, services []string) (*client.Client, *types.Project, error) {
-	c, err := client.New(ctx)
+	c, err := client.NewWithDefaultLocalBackend(ctx)
 	if err != nil {
 		return nil, nil, err
 	}

+ 4 - 8
local/e2e/compose_test.go

@@ -30,8 +30,6 @@ import (
 
 func TestLocalComposeUp(t *testing.T) {
 	c := NewParallelE2eCLI(t, binDir)
-	c.RunDockerCmd("context", "create", "local", "test-context").Assert(t, icmd.Success)
-	c.RunDockerCmd("context", "use", "test-context").Assert(t, icmd.Success)
 
 	const projectName = "compose-e2e-demo"
 
@@ -54,12 +52,12 @@ func TestLocalComposeUp(t *testing.T) {
 		output := HTTPGetWithRetry(t, endpoint+"/words/noun", http.StatusOK, 2*time.Second, 20*time.Second)
 		assert.Assert(t, strings.Contains(output, `"word":`))
 
-		res = c.RunDockerCmd("--context", "default", "network", "ls")
+		res = c.RunDockerCmd("network", "ls")
 		res.Assert(t, icmd.Expected{Out: projectName + "_default"})
 	})
 
 	t.Run("check compose labels", func(t *testing.T) {
-		res := c.RunDockerCmd("--context", "default", "inspect", projectName+"_web_1")
+		res := c.RunDockerCmd("inspect", projectName+"_web_1")
 		res.Assert(t, icmd.Expected{Out: `"com.docker.compose.container-number": "1"`})
 		res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project": "compose-e2e-demo"`})
 		res.Assert(t, icmd.Expected{Out: `"com.docker.compose.oneoff": "False",`})
@@ -69,7 +67,7 @@ func TestLocalComposeUp(t *testing.T) {
 		res.Assert(t, icmd.Expected{Out: `"com.docker.compose.service": "web"`})
 		res.Assert(t, icmd.Expected{Out: `"com.docker.compose.version":`})
 
-		res = c.RunDockerCmd("--context", "default", "network", "inspect", projectName+"_default")
+		res = c.RunDockerCmd("network", "inspect", projectName+"_default")
 		res.Assert(t, icmd.Expected{Out: `"com.docker.compose.network": "default"`})
 		res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project": `})
 		res.Assert(t, icmd.Expected{Out: `"com.docker.compose.version": `})
@@ -85,15 +83,13 @@ func TestLocalComposeUp(t *testing.T) {
 	})
 
 	t.Run("check networks after down", func(t *testing.T) {
-		res := c.RunDockerCmd("--context", "default", "network", "ls")
+		res := c.RunDockerCmd("network", "ls")
 		assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
 	})
 }
 
 func TestLocalComposeVolume(t *testing.T) {
 	c := NewParallelE2eCLI(t, binDir)
-	c.RunDockerCmd("context", "create", "local", "test-context").Assert(t, icmd.Success)
-	c.RunDockerCmd("context", "use", "test-context").Assert(t, icmd.Success)
 
 	const projectName = "compose-e2e-volume"