extract dockerclient connection

This commit is contained in:
Nate Jones 2015-05-21 12:48:06 -07:00
parent 8c98858fcb
commit 837d45a0c3
2 changed files with 36 additions and 22 deletions

View file

@ -8,7 +8,6 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path"
"strings" "strings"
) )
@ -53,27 +52,7 @@ func (x *ImagesCommand) Execute(args []string) error {
} else { } else {
// grab directly from docker daemon client, err := connect()
endpoint := os.Getenv("DOCKER_HOST")
if len(endpoint) == 0 {
endpoint = "unix:///var/run/docker.sock"
}
var client *docker.Client
if dockerCertPath := os.Getenv("DOCKER_CERT_PATH"); len(dockerCertPath) > 0 {
cert := path.Join(dockerCertPath, "cert.pem")
key := path.Join(dockerCertPath, "key.pem")
ca := path.Join(dockerCertPath, "ca.pem")
client, err = docker.NewTLSClient(endpoint, cert, key, ca)
if err != nil {
return err
}
} else {
client, err = docker.NewClient(endpoint)
if err != nil {
return err
}
}
clientImages, err := client.ListImages(docker.ListImagesOptions{All: true}) clientImages, err := client.ListImages(docker.ListImagesOptions{All: true})
if err != nil { if err != nil {

35
util.go Normal file
View file

@ -0,0 +1,35 @@
package main
import (
"github.com/fsouza/go-dockerclient"
"os"
"path"
)
func connect() (*docker.Client, error) {
// grab directly from docker daemon
endpoint := os.Getenv("DOCKER_HOST")
if len(endpoint) == 0 {
endpoint = "unix:///var/run/docker.sock"
}
var client *docker.Client
var err error
if dockerCertPath := os.Getenv("DOCKER_CERT_PATH"); len(dockerCertPath) > 0 {
cert := path.Join(dockerCertPath, "cert.pem")
key := path.Join(dockerCertPath, "key.pem")
ca := path.Join(dockerCertPath, "ca.pem")
client, err = docker.NewTLSClient(endpoint, cert, key, ca)
if err != nil {
return nil, err
}
} else {
client, err = docker.NewClient(endpoint)
if err != nil {
return nil, err
}
}
return client, nil
}