support for direct client connect in containers too

This commit is contained in:
Nate Jones 2015-05-21 13:12:10 -07:00
parent 837d45a0c3
commit 56ab2b992b

View file

@ -1,6 +1,8 @@
package main package main
import ( import (
"github.com/fsouza/go-dockerclient"
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -28,16 +30,48 @@ var containersCommand ContainersCommand
func (x *ContainersCommand) Execute(args []string) error { func (x *ContainersCommand) Execute(args []string) error {
var containers *[]Container
stat, err := os.Stdin.Stat()
if err != nil {
return fmt.Errorf("error reading stdin stat", err)
}
if (stat.Mode() & os.ModeCharDevice) == 0 {
// read in stdin // read in stdin
stdin, err := ioutil.ReadAll(os.Stdin) stdin, err := ioutil.ReadAll(os.Stdin)
if err != nil { if err != nil {
return fmt.Errorf("error reading all input", err) return fmt.Errorf("error reading all input", err)
} }
containers, err := parseContainersJSON(stdin) containers, err = parseContainersJSON(stdin)
if err != nil { if err != nil {
return err 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 { if containersCommand.Dot {
fmt.Printf(jsonContainersToDot(containers)) fmt.Printf(jsonContainersToDot(containers))
@ -48,6 +82,20 @@ func (x *ContainersCommand) Execute(args []string) error {
return nil 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) { func parseContainersJSON(rawJSON []byte) (*[]Container, error) {
var containers []Container var containers []Container