github.com/schwarzm/garden-linux@v0.0.0-20150507151835-33bca2147c47/old/repository_fetcher/repository_provider.go (about)

     1  package repository_fetcher
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  //go:generate counterfeiter . RegistryProvider
     9  type RegistryProvider interface {
    10  	ProvideRegistry(hostname string) (Registry, error)
    11  	ApplyDefaultHostname(hostname string) string
    12  }
    13  
    14  type InsecureRegistryError struct {
    15  	Cause              error
    16  	Endpoint           string
    17  	InsecureRegistries []string
    18  }
    19  
    20  func (err InsecureRegistryError) Error() string {
    21  	return fmt.Sprintf(
    22  		"Registry %s is missing from -insecureDockerRegistryList (%v)",
    23  		err.Endpoint,
    24  		err.InsecureRegistries,
    25  	)
    26  }
    27  
    28  type registryProvider struct {
    29  	DefaultHostname    string
    30  	InsecureRegistries []string
    31  }
    32  
    33  func (rp registryProvider) ApplyDefaultHostname(hostname string) string {
    34  	if hostname == "" {
    35  		return rp.DefaultHostname
    36  	}
    37  	return hostname
    38  }
    39  
    40  func (rp registryProvider) ProvideRegistry(hostname string) (Registry, error) {
    41  	var err error
    42  
    43  	hostname = rp.ApplyDefaultHostname(hostname)
    44  
    45  	endpoint, err := RegistryNewEndpoint(hostname, rp.InsecureRegistries)
    46  	if err != nil && strings.Contains(err.Error(), "--insecure-registry") {
    47  		return nil, &InsecureRegistryError{
    48  			Cause:              err,
    49  			Endpoint:           hostname,
    50  			InsecureRegistries: rp.InsecureRegistries,
    51  		}
    52  	} else if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	return RegistryNewSession(nil, nil, endpoint, true)
    57  }
    58  
    59  func NewRepositoryProvider(defaultHostname string, insecureRegistries []string) RegistryProvider {
    60  	return &registryProvider{DefaultHostname: defaultHostname, InsecureRegistries: insecureRegistries}
    61  }