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

Commit e66bf40

Browse files
committed
Move stopped container handling to cli/app package
Signed-off-by: Josh Curl <[email protected]>
1 parent 9d16004 commit e66bf40

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
@@ -557,7 +557,7 @@ func (s *Service) Kill(ctx context.Context, signal string) error {
557557
func (s *Service) Delete(ctx context.Context, options options.Delete) error {
558558
return s.collectContainersAndDo(ctx, func(c *Container) error {
559559
running, _ := c.IsRunning(ctx)
560-
if !running {
560+
if !running || options.RemoveRunning {
561561
return c.Remove(ctx, options.RemoveVolume)
562562
}
563563
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
@@ -494,8 +494,8 @@ func (p *Project) Pull(ctx context.Context, services ...string) error {
494494
}), nil)
495495
}
496496

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

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

0 commit comments

Comments
 (0)