From 56ab2b992bf0a0c113a4e59918061319ff7f9259 Mon Sep 17 00:00:00 2001 From: Nate Jones Date: Thu, 21 May 2015 13:12:10 -0700 Subject: [PATCH] support for direct client connect in containers too --- containers.go | 60 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/containers.go b/containers.go index ca131dc..e537b48 100644 --- a/containers.go +++ b/containers.go @@ -1,6 +1,8 @@ package main import ( + "github.com/fsouza/go-dockerclient" + "bytes" "encoding/json" "fmt" @@ -28,15 +30,47 @@ var containersCommand ContainersCommand func (x *ContainersCommand) Execute(args []string) error { - // read in stdin - stdin, err := ioutil.ReadAll(os.Stdin) + var containers *[]Container + + stat, err := os.Stdin.Stat() if err != nil { - return fmt.Errorf("error reading all input", err) + return fmt.Errorf("error reading stdin stat", err) } - containers, err := parseContainersJSON(stdin) - if err != nil { - return err + if (stat.Mode() & os.ModeCharDevice) == 0 { + // read in stdin + stdin, err := ioutil.ReadAll(os.Stdin) + if err != nil { + return fmt.Errorf("error reading all input", err) + } + + containers, err = parseContainersJSON(stdin) + if err != nil { + return err + } + } else { + + client, err := connect() + + clientContainers, err := client.ListContainers(docker.ListContainersOptions{All: true}) + if err != nil { + return err + } + + var conts []Container + for _, container := range clientContainers { + conts = append(conts, Container{ + container.ID, + container.Image, + container.Names, + apiPortToMap(container.Ports), + container.Created, + container.Status, + container.Command, + }) + } + + containers = &conts } if containersCommand.Dot { @@ -48,6 +82,20 @@ func (x *ContainersCommand) Execute(args []string) error { return nil } +func apiPortToMap(ports []docker.APIPort) []map[string]interface{} { + result := make([]map[string]interface{}, 2) + for _, port := range ports { + intPort := map[string]interface{}{ + "IP": port.IP, + "Type": port.Type, + "PrivatePort": port.PrivatePort, + "PublicPort": port.PublicPort, + } + result = append(result, intPort) + } + return result +} + func parseContainersJSON(rawJSON []byte) (*[]Container, error) { var containers []Container