mirror of
https://github.com/moby/moby.git
synced 2026-01-11 10:41:43 +00:00
The format for filters changed in93d1dd8036(docker v1.10 / API v1.22). As part of that implementation, the daemon would parse the new format, and fall back to parsing the old format if this failed. This fallback was not based on API version, so any version of the API released since would continue to accept both the legacy and curent format. For the client, the change in format caused a regression when connecting to an older daemon; a `ToParamWithVersion` utility was introduced in [docker/engine-api@81388f0] to produce the old format when the client was connected to a docker v1.9 or older daemon, using an old API version. Given that any version of docker 1.10 or above would support both formats, regardless of the API version used, and API v1.22 is no longer supported, it should be safe to assume we can drop the version-specific format in the client. Even if the client would be using API v1.22 (or older), the format would only be necessary for an actual docker v1.9 daemon, which would be very unlikely, and a daemon that's 9 Years old. [docker/engine-api@81388f0]:81388f00ddSigned-off-by: Sebastiaan van Stijn <github@gone.nl>
66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/url"
|
|
"strconv"
|
|
|
|
"github.com/moby/moby/api/types/container"
|
|
"github.com/moby/moby/api/types/filters"
|
|
)
|
|
|
|
// ContainerListOptions holds parameters to list containers with.
|
|
type ContainerListOptions struct {
|
|
Size bool
|
|
All bool
|
|
Latest bool
|
|
Since string
|
|
Before string
|
|
Limit int
|
|
Filters filters.Args
|
|
}
|
|
|
|
// ContainerList returns the list of containers in the docker host.
|
|
func (cli *Client) ContainerList(ctx context.Context, options ContainerListOptions) ([]container.Summary, error) {
|
|
query := url.Values{}
|
|
|
|
if options.All {
|
|
query.Set("all", "1")
|
|
}
|
|
|
|
if options.Limit > 0 {
|
|
query.Set("limit", strconv.Itoa(options.Limit))
|
|
}
|
|
|
|
if options.Since != "" {
|
|
query.Set("since", options.Since)
|
|
}
|
|
|
|
if options.Before != "" {
|
|
query.Set("before", options.Before)
|
|
}
|
|
|
|
if options.Size {
|
|
query.Set("size", "1")
|
|
}
|
|
|
|
if options.Filters.Len() > 0 {
|
|
filterJSON, err := filters.ToJSON(options.Filters)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
query.Set("filters", filterJSON)
|
|
}
|
|
|
|
resp, err := cli.get(ctx, "/containers/json", query, nil)
|
|
defer ensureReaderClosed(resp)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var containers []container.Summary
|
|
err = json.NewDecoder(resp.Body).Decode(&containers)
|
|
return containers, err
|
|
}
|