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

Commit e1a83f7

Browse files
authored
Merge pull request #338 from vdemeester/project-containers
Replace ListStoppedContainers by Containers
2 parents ced6fdd + 01c2c23 commit e1a83f7

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

cli/app/app.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ func ProjectDelete(p project.APIProject, c *cli.Context) error {
234234
RemoveVolume: c.Bool("v"),
235235
}
236236
if !c.Bool("force") {
237-
stoppedContainers, err := p.ListStoppedContainers(context.Background(), c.Args()...)
237+
stoppedContainers, err := p.Containers(context.Background(), project.Filter{
238+
State: project.Stopped,
239+
}, c.Args()...)
238240
if err != nil {
239241
return cli.NewExitError(err.Error(), 1)
240242
}

project/interface.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,24 @@ 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)
40+
Containers(ctx context.Context, filter Filter, services ...string) ([]string, error)
4141
}
4242

43+
// Filter holds filter element to filter containers
44+
type Filter struct {
45+
State State
46+
}
47+
48+
// State defines the supported state you can filter on
49+
type State string
50+
51+
// Definitions of filter states
52+
const (
53+
AnyState = State("")
54+
Running = State("running")
55+
Stopped = State("stopped")
56+
)
57+
4358
// RuntimeProject defines runtime-specific methods for a libcompose implementation.
4459
type RuntimeProject interface {
4560
RemoveOrphans(ctx context.Context, projectName string, serviceConfigs *config.ServiceConfigs) error

project/project.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -496,37 +496,50 @@ 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) {
501-
stoppedContainers := []string{}
499+
// Containers lists the containers for the specified services. Can be filter using
500+
// the Filter struct.
501+
func (p *Project) Containers(ctx context.Context, filter Filter, services ...string) ([]string, error) {
502+
containers := []string{}
502503
err := p.forEach(services, wrapperAction(func(wrapper *serviceWrapper, wrappers map[string]*serviceWrapper) {
503504
wrapper.Do(nil, events.NoEvent, events.NoEvent, func(service Service) error {
504-
containers, innerErr := service.Containers(ctx)
505+
serviceContainers, innerErr := service.Containers(ctx)
505506
if innerErr != nil {
506507
return innerErr
507508
}
508509

509-
for _, container := range containers {
510+
for _, container := range serviceContainers {
510511
running, innerErr := container.IsRunning(ctx)
511512
if innerErr != nil {
512513
log.Error(innerErr)
513514
}
514-
if !running {
515-
containerID, innerErr := container.ID()
516-
if innerErr != nil {
517-
log.Error(innerErr)
515+
switch filter.State {
516+
case Running:
517+
if !running {
518+
continue
518519
}
519-
stoppedContainers = append(stoppedContainers, containerID)
520+
case Stopped:
521+
if running {
522+
continue
523+
}
524+
case AnyState:
525+
// Don't do a thing
526+
default:
527+
// Invalid state filter
528+
return fmt.Errorf("Invalid container filter: %s", filter.State)
529+
}
530+
containerID, innerErr := container.ID()
531+
if innerErr != nil {
532+
log.Error(innerErr)
520533
}
534+
containers = append(containers, containerID)
521535
}
522-
523536
return nil
524537
})
525538
}), nil)
526539
if err != nil {
527540
return nil, err
528541
}
529-
return stoppedContainers, nil
542+
return containers, nil
530543
}
531544

532545
// Delete removes the specified services (like docker rm).

0 commit comments

Comments
 (0)