github.com/bdwilliams/libcompose@v0.3.1-0.20160826154243-d81a9bdacff0/project/project_containers.go (about)

     1  package project
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"golang.org/x/net/context"
     7  
     8  	log "github.com/Sirupsen/logrus"
     9  	"github.com/docker/libcompose/project/events"
    10  )
    11  
    12  // Containers lists the containers for the specified services. Can be filter using
    13  // the Filter struct.
    14  func (p *Project) Containers(ctx context.Context, filter Filter, services ...string) ([]string, error) {
    15  	containers := []string{}
    16  	err := p.forEach(services, wrapperAction(func(wrapper *serviceWrapper, wrappers map[string]*serviceWrapper) {
    17  		wrapper.Do(nil, events.NoEvent, events.NoEvent, func(service Service) error {
    18  			serviceContainers, innerErr := service.Containers(ctx)
    19  			if innerErr != nil {
    20  				return innerErr
    21  			}
    22  
    23  			for _, container := range serviceContainers {
    24  				running, innerErr := container.IsRunning(ctx)
    25  				if innerErr != nil {
    26  					log.Error(innerErr)
    27  				}
    28  				switch filter.State {
    29  				case Running:
    30  					if !running {
    31  						continue
    32  					}
    33  				case Stopped:
    34  					if running {
    35  						continue
    36  					}
    37  				case AnyState:
    38  					// Don't do a thing
    39  				default:
    40  					// Invalid state filter
    41  					return fmt.Errorf("Invalid container filter: %s", filter.State)
    42  				}
    43  				containerID, innerErr := container.ID()
    44  				if innerErr != nil {
    45  					log.Error(innerErr)
    46  				}
    47  				containers = append(containers, containerID)
    48  			}
    49  			return nil
    50  		})
    51  	}), nil)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	return containers, nil
    56  }