Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit da6d2ea

Browse files
authored
Merge pull request #320 from joshwget/stopped-container-refactoring
Move stopped container handling to cli/app package
2 parents 0843a75 + e66bf40 commit da6d2ea

File tree

5 files changed

+22
-29
lines changed

5 files changed

+22
-29
lines changed

cli/app/app.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -234,18 +234,22 @@ func ProjectDelete(p project.APIProject, c *cli.Context) error {
234234
RemoveVolume: c.Bool("v"),
235235
}
236236
if !c.Bool("force") {
237-
options.BeforeDeleteCallback = func(stoppedContainers []string) bool {
238-
fmt.Printf("Going to remove %v\nAre you sure? [yN]\n", strings.Join(stoppedContainers, ", "))
239-
var answer string
240-
_, err := fmt.Scanln(&answer)
241-
if err != nil {
242-
logrus.Error(err)
243-
return false
244-
}
245-
if answer != "y" && answer != "Y" {
246-
return false
247-
}
248-
return true
237+
stoppedContainers, err := p.ListStoppedContainers(context.Background(), c.Args()...)
238+
if err != nil {
239+
return cli.NewExitError(err.Error(), 1)
240+
}
241+
if len(stoppedContainers) == 0 {
242+
fmt.Println("No stopped containers")
243+
return nil
244+
}
245+
fmt.Printf("Going to remove %v\nAre you sure? [yN]\n", strings.Join(stoppedContainers, ", "))
246+
var answer string
247+
_, err = fmt.Scanln(&answer)
248+
if err != nil {
249+
return cli.NewExitError(err.Error(), 1)
250+
}
251+
if answer != "y" && answer != "Y" {
252+
return nil
249253
}
250254
}
251255
err := p.Delete(context.Background(), options, c.Args()...)

docker/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ func (s *Service) Kill(ctx context.Context, signal string) error {
556556
func (s *Service) Delete(ctx context.Context, options options.Delete) error {
557557
return s.collectContainersAndDo(ctx, func(c *Container) error {
558558
running, _ := c.IsRunning(ctx)
559-
if !running {
559+
if !running || options.RemoveRunning {
560560
return c.Remove(ctx, options.RemoveVolume)
561561
}
562562
return nil

project/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type APIProject interface {
3737
CreateService(name string) (Service, error)
3838
AddConfig(name string, config *config.ServiceConfig) error
3939
Load(bytes []byte) error
40+
ListStoppedContainers(ctx context.Context, services ...string) ([]string, error)
4041
}
4142

4243
// RuntimeProject defines runtime-specific methods for a libcompose implementation.

project/options/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ type Build struct {
99

1010
// Delete holds options of compose rm.
1111
type Delete struct {
12-
RemoveVolume bool
13-
BeforeDeleteCallback func([]string) bool
12+
RemoveVolume bool
13+
RemoveRunning bool
1414
}
1515

1616
// Down holds options of compose down.

project/project.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -496,8 +496,8 @@ func (p *Project) Pull(ctx context.Context, services ...string) error {
496496
}), nil)
497497
}
498498

499-
// listStoppedContainers lists the stopped containers for the specified services.
500-
func (p *Project) listStoppedContainers(ctx context.Context, services ...string) ([]string, error) {
499+
// ListStoppedContainers lists the stopped containers for the specified services.
500+
func (p *Project) ListStoppedContainers(ctx context.Context, services ...string) ([]string, error) {
501501
stoppedContainers := []string{}
502502
err := p.forEach(services, wrapperAction(func(wrapper *serviceWrapper, wrappers map[string]*serviceWrapper) {
503503
wrapper.Do(nil, events.NoEvent, events.NoEvent, func(service Service) error {
@@ -531,18 +531,6 @@ func (p *Project) listStoppedContainers(ctx context.Context, services ...string)
531531

532532
// Delete removes the specified services (like docker rm).
533533
func (p *Project) Delete(ctx context.Context, options options.Delete, services ...string) error {
534-
stoppedContainers, err := p.listStoppedContainers(ctx, services...)
535-
if err != nil {
536-
return err
537-
}
538-
if len(stoppedContainers) == 0 {
539-
p.Notify(events.ProjectDeleteDone, "", nil)
540-
fmt.Println("No stopped containers")
541-
return nil
542-
}
543-
if options.BeforeDeleteCallback != nil && !options.BeforeDeleteCallback(stoppedContainers) {
544-
return nil
545-
}
546534
return p.perform(events.ProjectDeleteStart, events.ProjectDeleteDone, services, wrapperAction(func(wrapper *serviceWrapper, wrappers map[string]*serviceWrapper) {
547535
wrapper.Do(nil, events.ServiceDeleteStart, events.ServiceDelete, func(service Service) error {
548536
return service.Delete(ctx, options)

0 commit comments

Comments
 (0)