github.com/bdwilliams/libcompose@v0.3.1-0.20160826154243-d81a9bdacff0/docker/container/functions.go (about)

     1  package container
     2  
     3  import (
     4  	"golang.org/x/net/context"
     5  
     6  	"github.com/docker/engine-api/client"
     7  	"github.com/docker/engine-api/types"
     8  	"github.com/docker/engine-api/types/filters"
     9  )
    10  
    11  // ListByFilter looks up the hosts containers with the specified filters and
    12  // returns a list of container matching it, or an error.
    13  func ListByFilter(ctx context.Context, clientInstance client.ContainerAPIClient, containerFilters ...map[string][]string) ([]types.Container, error) {
    14  	filterArgs := filters.NewArgs()
    15  
    16  	// FIXME(vdemeester) I don't like 3 for loops >_<
    17  	for _, filter := range containerFilters {
    18  		for key, filterValue := range filter {
    19  			for _, value := range filterValue {
    20  				filterArgs.Add(key, value)
    21  			}
    22  		}
    23  	}
    24  
    25  	return clientInstance.ContainerList(ctx, types.ContainerListOptions{
    26  		All:    true,
    27  		Filter: filterArgs,
    28  	})
    29  }
    30  
    31  // Get looks up the hosts containers with the specified ID
    32  // or name and returns it, or an error.
    33  func Get(ctx context.Context, clientInstance client.ContainerAPIClient, id string) (*types.ContainerJSON, error) {
    34  	container, err := clientInstance.ContainerInspect(ctx, id)
    35  	if err != nil {
    36  		if client.IsErrContainerNotFound(err) {
    37  			return nil, nil
    38  		}
    39  		return nil, err
    40  	}
    41  	return &container, nil
    42  }