1
0
Эх сурвалжийг харах

Put each compose command in own file

Djordje Lukic 5 жил өмнө
parent
commit
0765b08309

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

@@ -2,9 +2,6 @@ package compose
 
 import (
 	"github.com/spf13/cobra"
-
-	"github.com/docker/api/client"
-	"github.com/docker/api/compose"
 )
 
 // Command returns the compose command with its child commands
@@ -13,48 +10,11 @@ func Command() *cobra.Command {
 		Short: "Docker Compose",
 		Use:   "compose",
 	}
+
 	command.AddCommand(
 		upCommand(),
 		downCommand(),
 	)
-	return command
-}
-
-func upCommand() *cobra.Command {
-	opts := &compose.ProjectOptions{}
-	upCmd := &cobra.Command{
-		Use: "up",
-		RunE: func(cmd *cobra.Command, args []string) error {
-			c, err := client.New(cmd.Context())
-			if err != nil {
-				return err
-			}
-			return c.ComposeService().Up(cmd.Context(), *opts)
-		},
-	}
-	upCmd.Flags().StringVar(&opts.Name, "name", "", "Project name")
-	upCmd.Flags().StringVar(&opts.WorkDir, "workdir", ".", "Work dir")
-	upCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
-	upCmd.Flags().StringArrayVarP(&opts.Environment, "environment", "e", []string{}, "Environment variables")
 
-	return upCmd
-}
-
-func downCommand() *cobra.Command {
-	opts := &compose.ProjectOptions{}
-	downCmd := &cobra.Command{
-		Use: "down",
-		RunE: func(cmd *cobra.Command, args []string) error {
-			c, err := client.New(cmd.Context())
-			if err != nil {
-				return err
-			}
-			return c.ComposeService().Down(cmd.Context(), *opts)
-		},
-	}
-	downCmd.Flags().StringVar(&opts.Name, "name", "", "Project name")
-	downCmd.Flags().StringVar(&opts.WorkDir, "workdir", ".", "Work dir")
-	downCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
-
-	return downCmd
+	return command
 }

+ 40 - 0
cli/cmd/compose/down.go

@@ -0,0 +1,40 @@
+package compose
+
+import (
+	"context"
+	"errors"
+
+	"github.com/spf13/cobra"
+
+	"github.com/docker/api/client"
+	"github.com/docker/api/compose"
+)
+
+func downCommand() *cobra.Command {
+	opts := compose.ProjectOptions{}
+	downCmd := &cobra.Command{
+		Use: "down",
+		RunE: func(cmd *cobra.Command, args []string) error {
+			return runDown(cmd.Context(), opts)
+		},
+	}
+	downCmd.Flags().StringVar(&opts.Name, "name", "", "Project name")
+	downCmd.Flags().StringVar(&opts.WorkDir, "workdir", ".", "Work dir")
+	downCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
+
+	return downCmd
+}
+
+func runDown(ctx context.Context, opts compose.ProjectOptions) error {
+	c, err := client.New(ctx)
+	if err != nil {
+		return err
+	}
+
+	composeService := c.ComposeService()
+	if composeService == nil {
+		return errors.New("compose not implemented in current context")
+	}
+
+	return composeService.Down(ctx, opts)
+}

+ 41 - 0
cli/cmd/compose/up.go

@@ -0,0 +1,41 @@
+package compose
+
+import (
+	"context"
+	"errors"
+
+	"github.com/spf13/cobra"
+
+	"github.com/docker/api/client"
+	"github.com/docker/api/compose"
+)
+
+func upCommand() *cobra.Command {
+	opts := compose.ProjectOptions{}
+	upCmd := &cobra.Command{
+		Use: "up",
+		RunE: func(cmd *cobra.Command, args []string) error {
+			return runUp(cmd.Context(), opts)
+		},
+	}
+	upCmd.Flags().StringVar(&opts.Name, "name", "", "Project name")
+	upCmd.Flags().StringVar(&opts.WorkDir, "workdir", ".", "Work dir")
+	upCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
+	upCmd.Flags().StringArrayVarP(&opts.Environment, "environment", "e", []string{}, "Environment variables")
+
+	return upCmd
+}
+
+func runUp(ctx context.Context, opts compose.ProjectOptions) error {
+	c, err := client.New(ctx)
+	if err != nil {
+		return err
+	}
+
+	composeService := c.ComposeService()
+	if composeService == nil {
+		return errors.New("compose not implemented in current context")
+	}
+
+	return composeService.Up(ctx, opts)
+}