From 86bd046deb2c91e370282053641f8f42f0b7e182 Mon Sep 17 00:00:00 2001 From: ubergesundheit Date: Tue, 7 Feb 2017 11:42:49 +0100 Subject: [PATCH] add ps style filtering of containers --- container/client.go | 25 ++++++++++++++++++++++--- main.go | 8 +++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/container/client.go b/container/client.go index 9824b81..7fe12b4 100644 --- a/container/client.go +++ b/container/client.go @@ -3,10 +3,12 @@ package container import ( "fmt" "io/ioutil" + "strings" "time" log "github.com/Sirupsen/logrus" "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/network" dockerclient "github.com/docker/docker/client" "golang.org/x/net/context" @@ -37,19 +39,20 @@ type Client interface { // * DOCKER_HOST the docker-engine host to send api requests to // * DOCKER_TLS_VERIFY whether to verify tls certificates // * DOCKER_API_VERSION the minimum docker api version to work with -func NewClient(pullImages bool) Client { +func NewClient(pullImages bool, filtersSlice []string) Client { cli, err := dockerclient.NewEnvClient() if err != nil { log.Fatalf("Error instantiating Docker client: %s", err) } - return dockerClient{api: cli, pullImages: pullImages} + return dockerClient{api: cli, pullImages: pullImages, rawFilters: filtersSlice} } type dockerClient struct { api *dockerclient.Client pullImages bool + rawFilters []string } func (client dockerClient) ListContainers(fn Filter) ([]Container, error) { @@ -58,9 +61,25 @@ func (client dockerClient) ListContainers(fn Filter) ([]Container, error) { log.Debug("Retrieving running containers") + listOptions := types.ContainerListOptions{} + + if len(client.rawFilters) != 0 { + // try to construct filters from the string slice + filters := filters.NewArgs() + for _, filter := range client.rawFilters { + // split by first equals sign + filterParts := strings.SplitN(filter, "=", 2) + // now add the filter + filters.Add(filterParts[0], filterParts[1]) + } + listOptions.All = true + listOptions.Filters = filters + } + runningContainers, err := client.api.ContainerList( bg, - types.ContainerListOptions{}) + listOptions, + ) if err != nil { return nil, err } diff --git a/main.go b/main.go index e97c4d8..f1463a5 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,7 @@ var ( scheduleSpec string cleanup bool noRestart bool + filters []string ) func init() { @@ -82,6 +83,10 @@ func main() { Name: "debug", Usage: "enable debug mode with verbose logging", }, + cli.StringSliceFlag{ + Name: "ps-filter", + Usage: "docker ps filters", + }, } if err := app.Run(os.Args); err != nil { @@ -96,6 +101,7 @@ func before(c *cli.Context) error { pollingSet := c.IsSet("interval") cronSet := c.IsSet("schedule") + filters = c.GlobalStringSlice("ps-filter") if pollingSet && cronSet { log.Fatal("Only schedule or interval can be defined, not both.") @@ -114,7 +120,7 @@ func before(c *cli.Context) error { return err } - client = container.NewClient(!c.GlobalBool("no-pull")) + client = container.NewClient(!c.GlobalBool("no-pull"), filters) return nil }