github.com/dnephin/dobi@v0.15.0/tasks/image/utils.go (about)

     1  package image
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/dnephin/dobi/config"
     8  	"github.com/dnephin/dobi/logging"
     9  	"github.com/dnephin/dobi/tasks/context"
    10  	docker "github.com/fsouza/go-dockerclient"
    11  )
    12  
    13  const (
    14  	defaultRepo = "https://index.docker.io/v1/"
    15  )
    16  
    17  // GetImage returns the image created by an image config
    18  func GetImage(ctx *context.ExecuteContext, conf *config.ImageConfig) (*docker.Image, error) {
    19  	return ctx.Client.InspectImage(GetImageName(ctx, conf))
    20  }
    21  
    22  // GetImageName returns the image name for an image config
    23  func GetImageName(ctx *context.ExecuteContext, conf *config.ImageConfig) string {
    24  	return fmt.Sprintf("%s:%s", conf.Image, GetCanonicalTag(ctx, conf))
    25  }
    26  
    27  // GetCanonicalTag returns the canonical tag for an image config
    28  func GetCanonicalTag(ctx *context.ExecuteContext, conf *config.ImageConfig) string {
    29  	if len(conf.Tags) > 0 {
    30  		return conf.Tags[0]
    31  	}
    32  	return ctx.Env.Unique()
    33  }
    34  
    35  func parseAuthRepo(image string) string {
    36  	return splitHostname(image)
    37  }
    38  
    39  // Copied from github.com/docker/docker/reference/reference.go
    40  // That package is conflicting with other dependencies, so it can't be imported
    41  // at this time.
    42  func splitHostname(name string) string {
    43  	i := strings.IndexRune(name, '/')
    44  	if i == -1 || (!strings.ContainsAny(name[:i], ".:") && name[:i] != "localhost") {
    45  		logging.Log.Debugf("Using default registry %q", defaultRepo)
    46  		return defaultRepo
    47  	}
    48  	return name[:i]
    49  }