Skip to content

Commit

Permalink
This puts the image parsing code (mostly) back to how it was before. The
Browse files Browse the repository at this point in the history
regex solution is extremely complex, which makes it hard to debug and
understand; the original switches and
commenting lay out the various cases in a straightforward fashion. Plus,
implementing namespace/repo support in the original code was a simple
strings.Join call.
  • Loading branch information
jefferai committed Jun 12, 2015
1 parent 380f3ce commit 0558763
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions builtin/providers/docker/resource_docker_image_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package docker
import (
"fmt"
"strings"
"regexp"

dc "github.com/fsouza/go-dockerclient"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -76,28 +75,36 @@ func pullImage(data *Data, client *dc.Client, image string) error {

pullOpts := dc.PullImageOptions{}

//
// Breaks apart an image string into host:port, repo, and tag components
regex := "^(?:(?P<host>(?:[\\w-]+(?:\\.[\\w-]+)+)(?::[\\d]+)?)/)?(?P<repo>[\\w.-]+(?:/[\\w.-]*)*)*(?::(?P<tag>[\\w.-]*))?"
r, _ := regexp.Compile(regex)

// Result is in form [[image, host, repo, tag]], so we get the head of the
// outer list to pass the inner list to result
result := r.FindAllStringSubmatch(image, -1)[0]

// If the host is not an empty string, then the image is using a private registry
if (result[1] != "") {
pullOpts.Registry = result[1]
// The repository for a private registry should take the form of host/repo rather than just repo
pullOpts.Repository = result[1] + "/" + result[2]
} else if (result[2] != "") {
// Local registries, or the main docker registry will have an image named as just 'repo'
pullOpts.Repository = result[2]
}
splitImageName := strings.Split(image, ":")
switch len(splitImageName) {

// It's in registry:port/username/repo:tag or registry:port/repo:tag format
case 3:
splitPortRepo := strings.Split(splitImageName[1], "/")
pullOpts.Registry = splitImageName[0] + ":" + splitPortRepo[0]
pullOpts.Tag = splitImageName[2]
pullOpts.Repository = strings.Join(splitPortRepo[1:], "/")

// It's either registry:port/username/repo, registry:port/repo,
// or repo:tag with default registry
case 2:
splitPortRepo := strings.Split(splitImageName[1], "/")
switch len(splitPortRepo) {
// repo:tag
case 1:
pullOpts.Repository = splitImageName[0]
pullOpts.Tag = splitImageName[1]

// registry:port/username/repo or registry:port/repo
default:
pullOpts.Registry = splitImageName[0] + ":" + splitPortRepo[0]
pullOpts.Repository = strings.Join(splitPortRepo[1:], "/")
pullOpts.Tag = "latest"
}

// If there was a tag specified, then set it
if (result[3] != "") {
pullOpts.Tag = result[3]
// Plain username/repo or repo
default:
pullOpts.Repository = image
}

if err := client.PullImage(pullOpts, dc.AuthConfiguration{}); err != nil {
Expand Down

0 comments on commit 0558763

Please sign in to comment.