Browse Source

move progress to backend on "down"

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 4 years ago
parent
commit
dec9169198
6 changed files with 80 additions and 46 deletions
  1. 22 12
      aci/compose.go
  2. 1 1
      cli/cmd/compose/compose.go
  3. 23 31
      cli/cmd/compose/down.go
  4. 15 1
      ecs/down.go
  5. 13 1
      kube/compose.go
  6. 6 0
      local/compose/down.go

+ 22 - 12
aci/compose.go

@@ -22,6 +22,7 @@ import (
 	"net/http"
 
 	"github.com/compose-spec/compose-go/types"
+	"github.com/pkg/errors"
 	"github.com/sirupsen/logrus"
 
 	"github.com/docker/compose-cli/aci/convert"
@@ -29,6 +30,7 @@ import (
 	"github.com/docker/compose-cli/api/compose"
 	"github.com/docker/compose-cli/api/context/store"
 	"github.com/docker/compose-cli/api/errdefs"
+	"github.com/docker/compose-cli/api/progress"
 	"github.com/docker/compose-cli/utils/formatter"
 )
 
@@ -123,21 +125,29 @@ func (cs aciComposeService) warnKeepVolumeOnDown(ctx context.Context, projectNam
 }
 
 func (cs *aciComposeService) Down(ctx context.Context, projectName string, options compose.DownOptions) error {
-	logrus.Debugf("Down on projectName with name %q", projectName)
-
-	if err := cs.warnKeepVolumeOnDown(ctx, projectName); err != nil {
-		return err
-	}
-
-	cg, err := deleteACIContainerGroup(ctx, cs.ctx, projectName)
-	if err != nil {
-		return err
+	if options.Volumes {
+		return errors.Wrap(errdefs.ErrNotImplemented, "--volumes option is not supported on ACI")
 	}
-	if cg.IsHTTPStatus(http.StatusNoContent) {
-		return errdefs.ErrNotFound
+	if options.Images != "" {
+		return errors.Wrap(errdefs.ErrNotImplemented, "--rmi option is not supported on ACI")
 	}
+	return progress.Run(ctx, func(ctx context.Context) error {
+		logrus.Debugf("Down on project with name %q", projectName)
 
-	return err
+		if err := cs.warnKeepVolumeOnDown(ctx, projectName); err != nil {
+			return err
+		}
+
+		cg, err := deleteACIContainerGroup(ctx, cs.ctx, projectName)
+		if err != nil {
+			return err
+		}
+		if cg.IsHTTPStatus(http.StatusNoContent) {
+			return errdefs.ErrNotFound
+		}
+
+		return err
+	})
 }
 
 func (cs *aciComposeService) Ps(ctx context.Context, projectName string, options compose.PsOptions) ([]compose.ContainerSummary, error) {

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

@@ -218,7 +218,7 @@ func RootCommand(contextType string, backend compose.Service) *cobra.Command {
 
 	command.AddCommand(
 		upCommand(&opts, contextType, backend),
-		downCommand(&opts, contextType, backend),
+		downCommand(&opts, backend),
 		startCommand(&opts, backend),
 		restartCommand(&opts, backend),
 		stopCommand(&opts, backend),

+ 23 - 31
cli/cmd/compose/down.go

@@ -25,8 +25,6 @@ import (
 	"github.com/spf13/cobra"
 
 	"github.com/docker/compose-cli/api/compose"
-	"github.com/docker/compose-cli/api/context/store"
-	"github.com/docker/compose-cli/api/progress"
 )
 
 type downOptions struct {
@@ -38,7 +36,7 @@ type downOptions struct {
 	images        string
 }
 
-func downCommand(p *projectOptions, contextType string, backend compose.Service) *cobra.Command {
+func downCommand(p *projectOptions, backend compose.Service) *cobra.Command {
 	opts := downOptions{
 		projectOptions: p,
 	}
@@ -63,39 +61,33 @@ func downCommand(p *projectOptions, contextType string, backend compose.Service)
 	flags := downCmd.Flags()
 	flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
 	flags.IntVarP(&opts.timeout, "timeout", "t", 10, "Specify a shutdown timeout in seconds")
-
-	switch contextType {
-	case store.LocalContextType, store.DefaultContextType, store.EcsLocalSimulationContextType:
-		flags.BoolVarP(&opts.volumes, "volumes", "v", false, " Remove named volumes declared in the `volumes` section of the Compose file and anonymous volumes attached to containers.")
-		flags.StringVar(&opts.images, "rmi", "", `Remove images used by services. "local" remove only images that don't have a custom tag ("local"|"all")`)
-	}
+	flags.BoolVarP(&opts.volumes, "volumes", "v", false, " Remove named volumes declared in the `volumes` section of the Compose file and anonymous volumes attached to containers.")
+	flags.StringVar(&opts.images, "rmi", "", `Remove images used by services. "local" remove only images that don't have a custom tag ("local"|"all")`)
 	return downCmd
 }
 
 func runDown(ctx context.Context, backend compose.Service, opts downOptions) error {
-	return progress.Run(ctx, func(ctx context.Context) error {
-		name := opts.ProjectName
-		var project *types.Project
-		if opts.ProjectName == "" {
-			p, err := opts.toProject(nil)
-			if err != nil {
-				return err
-			}
-			project = p
-			name = p.Name
+	name := opts.ProjectName
+	var project *types.Project
+	if opts.ProjectName == "" {
+		p, err := opts.toProject(nil)
+		if err != nil {
+			return err
 		}
+		project = p
+		name = p.Name
+	}
 
-		var timeout *time.Duration
-		if opts.timeChanged {
-			timeoutValue := time.Duration(opts.timeout) * time.Second
-			timeout = &timeoutValue
-		}
-		return backend.Down(ctx, name, compose.DownOptions{
-			RemoveOrphans: opts.removeOrphans,
-			Project:       project,
-			Timeout:       timeout,
-			Images:        opts.images,
-			Volumes:       opts.volumes,
-		})
+	var timeout *time.Duration
+	if opts.timeChanged {
+		timeoutValue := time.Duration(opts.timeout) * time.Second
+		timeout = &timeoutValue
+	}
+	return backend.Down(ctx, name, compose.DownOptions{
+		RemoveOrphans: opts.removeOrphans,
+		Project:       project,
+		Timeout:       timeout,
+		Images:        opts.images,
+		Volumes:       opts.volumes,
 	})
 }

+ 15 - 1
ecs/down.go

@@ -19,12 +19,26 @@ package ecs
 import (
 	"context"
 
-	"github.com/docker/compose-cli/api/compose"
+	"github.com/pkg/errors"
 
+	"github.com/docker/compose-cli/api/compose"
+	"github.com/docker/compose-cli/api/errdefs"
 	"github.com/docker/compose-cli/api/progress"
 )
 
 func (b *ecsAPIService) Down(ctx context.Context, projectName string, options compose.DownOptions) error {
+	if options.Volumes {
+		return errors.Wrap(errdefs.ErrNotImplemented, "--volumes option is not supported on ECS")
+	}
+	if options.Images != "" {
+		return errors.Wrap(errdefs.ErrNotImplemented, "--rmi option is not supported on ECS")
+	}
+	return progress.Run(ctx, func(ctx context.Context) error {
+		return b.down(ctx, projectName)
+	})
+}
+
+func (b *ecsAPIService) down(ctx context.Context, projectName string) error {
 	resources, err := b.aws.ListStackResources(ctx, projectName)
 	if err != nil {
 		return err

+ 13 - 1
kube/compose.go

@@ -24,6 +24,7 @@ import (
 	"strings"
 
 	"github.com/compose-spec/compose-go/types"
+	"github.com/pkg/errors"
 
 	"github.com/docker/compose-cli/api/compose"
 	apicontext "github.com/docker/compose-cli/api/context"
@@ -110,8 +111,19 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
 
 // Down executes the equivalent to a `compose down`
 func (s *composeService) Down(ctx context.Context, projectName string, options compose.DownOptions) error {
-	w := progress.ContextWriter(ctx)
+	if options.Volumes {
+		return errors.Wrap(errdefs.ErrNotImplemented, "--volumes option is not supported on Kubernetes")
+	}
+	if options.Images != "" {
+		return errors.Wrap(errdefs.ErrNotImplemented, "--rmi option is not supported on Kubernetes")
+	}
+	return progress.Run(ctx, func(ctx context.Context) error {
+		return s.down(ctx, projectName, options)
+	})
+}
 
+func (s *composeService) down(ctx context.Context, projectName string, options compose.DownOptions) error {
+	w := progress.ContextWriter(ctx)
 	eventName := fmt.Sprintf("Remove %s", projectName)
 	w.Event(progress.CreatingEvent(eventName))
 

+ 6 - 0
local/compose/down.go

@@ -36,6 +36,12 @@ import (
 type downOp func() error
 
 func (s *composeService) Down(ctx context.Context, projectName string, options compose.DownOptions) error {
+	return progress.Run(ctx, func(ctx context.Context) error {
+		return s.down(ctx, projectName, options)
+	})
+}
+
+func (s *composeService) down(ctx context.Context, projectName string, options compose.DownOptions) error {
 	w := progress.ContextWriter(ctx)
 	resourceToRemove := false