dryrun to collect resources to be removed, then remove

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-02-15 10:46:56 +01:00
parent 20b83aa237
commit 5cb2533faa
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
9 changed files with 61 additions and 46 deletions

View File

@ -211,6 +211,6 @@ func (cs *aciComposeService) RunOneOffContainer(ctx context.Context, project *ty
return 0, errdefs.ErrNotImplemented return 0, errdefs.ErrNotImplemented
} }
func (cs *aciComposeService) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) error { func (cs *aciComposeService) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) ([]string, error) {
return errdefs.ErrNotImplemented return nil, errdefs.ErrNotImplemented
} }

View File

@ -84,6 +84,6 @@ func (c *composeService) RunOneOffContainer(ctx context.Context, project *types.
return 0, errdefs.ErrNotImplemented return 0, errdefs.ErrNotImplemented
} }
func (c *composeService) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) error { func (c *composeService) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) ([]string, error) {
return errdefs.ErrNotImplemented return nil, errdefs.ErrNotImplemented
} }

View File

@ -54,7 +54,7 @@ type Service interface {
// RunOneOffContainer creates a service oneoff container and starts its dependencies // RunOneOffContainer creates a service oneoff container and starts its dependencies
RunOneOffContainer(ctx context.Context, project *types.Project, opts RunOptions) (int, error) RunOneOffContainer(ctx context.Context, project *types.Project, opts RunOptions) (int, error)
// Remove executes the equivalent to a `compose rm` // Remove executes the equivalent to a `compose rm`
Remove(ctx context.Context, project *types.Project, options RemoveOptions) error Remove(ctx context.Context, project *types.Project, options RemoveOptions) ([]string, error)
} }
// CreateOptions group options of the Create API // CreateOptions group options of the Create API
@ -101,6 +101,8 @@ type KillOptions struct {
// RemoveOptions group options of the Remove API // RemoveOptions group options of the Remove API
type RemoveOptions struct { type RemoveOptions struct {
// DryRun just list removable resources
DryRun bool
// Volumes remove anonymous volumes // Volumes remove anonymous volumes
Volumes bool Volumes bool
// Force don't ask to confirm removal // Force don't ask to confirm removal

View File

@ -18,10 +18,13 @@ package compose
import ( import (
"context" "context"
"fmt"
"strings"
"github.com/docker/compose-cli/api/client" "github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/api/progress"
"github.com/docker/compose-cli/utils/prompt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -68,17 +71,46 @@ func runRemove(ctx context.Context, opts removeOptions, services []string) error
return err return err
} }
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) { if opts.stop {
if opts.stop { _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
err := c.ComposeService().Stop(ctx, project) err := c.ComposeService().Stop(ctx, project)
if err != nil { return "", err
return "", err })
} if err != nil {
return err
} }
return "", c.ComposeService().Remove(ctx, project, compose.RemoveOptions{ }
reosurces, err := c.ComposeService().Remove(ctx, project, compose.RemoveOptions{
DryRun: true,
})
if err != nil {
return err
}
if len(reosurces) == 0 {
fmt.Println("No stopped containers")
return nil
}
msg := fmt.Sprintf("Going to remove %s", strings.Join(reosurces, ", "))
if opts.force {
fmt.Println(msg)
} else {
confirm, err := prompt.User{}.Confirm(msg, false)
if err != nil {
return err
}
if !confirm {
return nil
}
}
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
_, err = c.ComposeService().Remove(ctx, project, compose.RemoveOptions{
Volumes: opts.volumes, Volumes: opts.volumes,
Force: opts.force, Force: opts.force,
}) })
return "", err
}) })
return err return err
} }

View File

@ -176,6 +176,6 @@ func (e ecsLocalSimulation) RunOneOffContainer(ctx context.Context, project *typ
return 0, errors.Wrap(errdefs.ErrNotImplemented, "use docker-compose run") return 0, errors.Wrap(errdefs.ErrNotImplemented, "use docker-compose run")
} }
func (e ecsLocalSimulation) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) error { func (e ecsLocalSimulation) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) ([]string, error) {
return e.compose.Remove(ctx, project, options) return e.compose.Remove(ctx, project, options)
} }

View File

@ -29,6 +29,6 @@ func (b *ecsAPIService) RunOneOffContainer(ctx context.Context, project *types.P
return 0, errdefs.ErrNotImplemented return 0, errdefs.ErrNotImplemented
} }
func (b *ecsAPIService) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) error { func (b *ecsAPIService) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) ([]string, error) {
return errdefs.ErrNotImplemented return nil, errdefs.ErrNotImplemented
} }

View File

@ -198,6 +198,6 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.
return 0, errdefs.ErrNotImplemented return 0, errdefs.ErrNotImplemented
} }
func (s *composeService) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) error { func (s *composeService) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) ([]string, error) {
return errdefs.ErrNotImplemented return nil, errdefs.ErrNotImplemented
} }

View File

@ -23,31 +23,27 @@ import (
"strings" "strings"
"github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/utils/prompt" "github.com/docker/compose-cli/api/errdefs"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
moby "github.com/docker/docker/api/types" moby "github.com/docker/docker/api/types"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/sanathkr/go-yaml" "github.com/sanathkr/go-yaml"
errdefs2 "github.com/docker/compose-cli/api/errdefs"
) )
// NewComposeService create a local implementation of the compose.Service API // NewComposeService create a local implementation of the compose.Service API
func NewComposeService(apiClient client.APIClient) compose.Service { func NewComposeService(apiClient client.APIClient) compose.Service {
return &composeService{ return &composeService{
apiClient: apiClient, apiClient: apiClient,
ui: prompt.User{},
} }
} }
type composeService struct { type composeService struct {
apiClient client.APIClient apiClient client.APIClient
ui prompt.UI
} }
func (s *composeService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error { func (s *composeService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {
return errdefs2.ErrNotImplemented return errdefs.ErrNotImplemented
} }
func getCanonicalContainerName(c moby.Container) string { func getCanonicalContainerName(c moby.Container) string {

View File

@ -18,22 +18,20 @@ package compose
import ( import (
"context" "context"
"fmt"
"strings"
"github.com/compose-spec/compose-go/types"
moby "github.com/docker/docker/api/types"
"golang.org/x/sync/errgroup"
"github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/api/progress"
status "github.com/docker/compose-cli/local/moby" status "github.com/docker/compose-cli/local/moby"
"github.com/compose-spec/compose-go/types"
moby "github.com/docker/docker/api/types"
"golang.org/x/sync/errgroup"
) )
func (s *composeService) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) error { func (s *composeService) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) ([]string, error) {
containers, err := s.getContainers(ctx, project, oneOffInclude) containers, err := s.getContainers(ctx, project, oneOffInclude)
if err != nil { if err != nil {
return err return nil, err
} }
stoppedContainers := containers.filter(func(c moby.Container) bool { stoppedContainers := containers.filter(func(c moby.Container) bool {
@ -45,21 +43,8 @@ func (s *composeService) Remove(ctx context.Context, project *types.Project, opt
names = append(names, getCanonicalContainerName(c)) names = append(names, getCanonicalContainerName(c))
}) })
if len(stoppedContainers) == 0 { if options.DryRun {
fmt.Println("No stopped containers") return names, nil
return nil
}
msg := fmt.Sprintf("Going to remove %s", strings.Join(names, ", "))
if options.Force {
fmt.Println(msg)
} else {
confirm, err := s.ui.Confirm(msg, false)
if err != nil {
return err
}
if !confirm {
return nil
}
} }
w := progress.ContextWriter(ctx) w := progress.ContextWriter(ctx)
@ -79,5 +64,5 @@ func (s *composeService) Remove(ctx context.Context, project *types.Project, opt
return err return err
}) })
} }
return eg.Wait() return nil, eg.Wait()
} }