github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/api/client/lib/container_list.go (about)

     1  package lib
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/url"
     6  	"strconv"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	"github.com/docker/docker/api/types/filters"
    10  )
    11  
    12  // ContainerList returns the list of containers in the docker host.
    13  func (cli *Client) ContainerList(options types.ContainerListOptions) ([]types.Container, error) {
    14  	query := url.Values{}
    15  
    16  	if options.All {
    17  		query.Set("all", "1")
    18  	}
    19  
    20  	if options.Limit != -1 {
    21  		query.Set("limit", strconv.Itoa(options.Limit))
    22  	}
    23  
    24  	if options.Since != "" {
    25  		query.Set("since", options.Since)
    26  	}
    27  
    28  	if options.Before != "" {
    29  		query.Set("before", options.Before)
    30  	}
    31  
    32  	if options.Size {
    33  		query.Set("size", "1")
    34  	}
    35  
    36  	if options.Filter.Len() > 0 {
    37  		filterJSON, err := filters.ToParam(options.Filter)
    38  		if err != nil {
    39  			return nil, err
    40  		}
    41  
    42  		query.Set("filters", filterJSON)
    43  	}
    44  
    45  	resp, err := cli.get("/containers/json", query, nil)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	defer ensureReaderClosed(resp)
    50  
    51  	var containers []types.Container
    52  	err = json.NewDecoder(resp.body).Decode(&containers)
    53  	return containers, err
    54  }