diff --git a/main.go b/main.go index 50e01e8f7..ca295acd7 100644 --- a/main.go +++ b/main.go @@ -103,7 +103,7 @@ func main() { } func createRegistryClient(domain string) (*registry.Registry, error) { - // Use the auth-url domain if provided + // Use the auth-url domain if provided. authDomain := authURL if authDomain == "" { authDomain = domain @@ -119,7 +119,8 @@ func createRegistryClient(domain string) (*registry.Registry, error) { } // Create the registry client. - return registry.New(domain, auth, registry.Opt{ + return registry.New(auth, registry.Opt{ + Domain: domain, Insecure: insecure, Debug: debug, SkipPing: skipPing, diff --git a/registry/digest_test.go b/registry/digest_test.go index 3aeacfd86..af3fea752 100644 --- a/registry/digest_test.go +++ b/registry/digest_test.go @@ -12,7 +12,7 @@ func TestDigestFromDockerHub(t *testing.T) { t.Fatalf("Could not get auth config: %s", err) } - r, err := New(auth.ServerAddress, auth, Opt{}) + r, err := New(auth, Opt{}) if err != nil { t.Fatalf("Could not create registry instance: %s", err) } @@ -33,7 +33,7 @@ func TestDigestFromGCR(t *testing.T) { t.Fatalf("Could not get auth config: %s", err) } - r, err := New(auth.ServerAddress, auth, Opt{}) + r, err := New(auth, Opt{}) if err != nil { t.Fatalf("Could not create registry instance: %s", err) } diff --git a/registry/registry.go b/registry/registry.go index 577a43ba3..ba63727e5 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -41,6 +41,7 @@ func Log(format string, args ...interface{}) { // Opt holds the options for a new registry. type Opt struct { + Domain string Insecure bool Debug bool SkipPing bool @@ -50,7 +51,7 @@ type Opt struct { } // New creates a new Registry struct with the given URL and credentials. -func New(domain string, auth types.AuthConfig, opt Opt) (*Registry, error) { +func New(auth types.AuthConfig, opt Opt) (*Registry, error) { transport := http.DefaultTransport if opt.Insecure { @@ -61,11 +62,14 @@ func New(domain string, auth types.AuthConfig, opt Opt) (*Registry, error) { } } - return newFromTransport(domain, auth, transport, opt) + return newFromTransport(auth, transport, opt) } -func newFromTransport(domain string, auth types.AuthConfig, transport http.RoundTripper, opt Opt) (*Registry, error) { - url := strings.TrimSuffix(domain, "/") +func newFromTransport(auth types.AuthConfig, transport http.RoundTripper, opt Opt) (*Registry, error) { + if len(opt.Domain) < 1 { + opt.Domain = auth.ServerAddress + } + url := strings.TrimSuffix(opt.Domain, "/") authURL := strings.TrimSuffix(auth.ServerAddress, "/") if !reProtocol.MatchString(url) { @@ -75,8 +79,13 @@ func newFromTransport(domain string, auth types.AuthConfig, transport http.Round url = "http://" + url } } + if !reProtocol.MatchString(authURL) { - authURL = "https://" + authURL + if !opt.NonSSL { + authURL = "https://" + authURL + } else { + authURL = "http://" + authURL + } } tokenTransport := &TokenTransport{ diff --git a/registry/tokentransport_test.go b/registry/tokentransport_test.go index f7fded9c1..11c718412 100644 --- a/registry/tokentransport_test.go +++ b/registry/tokentransport_test.go @@ -25,7 +25,7 @@ func TestErrBasicAuth(t *testing.T) { Password: "ss3j", ServerAddress: ts.URL, } - r, err := New(authConfig.ServerAddress, authConfig, Opt{Insecure: true, Debug: true}) + r, err := New(authConfig, Opt{Insecure: true, Debug: true}) if err != nil { t.Fatalf("expected no error creating client, got %v", err) } @@ -78,7 +78,7 @@ func TestBothTokenAndAccessTokenWork(t *testing.T) { ServerAddress: ts.URL, } authConfig.Email = "me@email.com" - r, err := New(ts.URL, authConfig, Opt{Insecure: true, Debug: true}) + r, err := New(authConfig, Opt{Insecure: true, Debug: true}) if err != nil { t.Fatalf("expected no error creating client, got %v", err) }