From 837d45a0c37941fdbacf5674d84f66053bba3a76 Mon Sep 17 00:00:00 2001 From: Nate Jones Date: Thu, 21 May 2015 12:48:06 -0700 Subject: [PATCH] extract dockerclient connection --- images.go | 23 +---------------------- util.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 util.go diff --git a/images.go b/images.go index ad01cb9..2140533 100644 --- a/images.go +++ b/images.go @@ -8,7 +8,6 @@ import ( "fmt" "io/ioutil" "os" - "path" "strings" ) @@ -53,27 +52,7 @@ func (x *ImagesCommand) Execute(args []string) error { } else { - // grab directly from docker daemon - 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 - } - } + client, err := connect() clientImages, err := client.ListImages(docker.ListImagesOptions{All: true}) if err != nil { diff --git a/util.go b/util.go new file mode 100644 index 0000000..c1f84a0 --- /dev/null +++ b/util.go @@ -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 +}