@@ -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