Skip to content

Commit

Permalink
feat(docker): Adding support for any registry by url
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed May 22, 2021
1 parent fe14935 commit 547ba49
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions pkg/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"flag"
"fmt"
"net/http"
"net/url"
"runtime"
"strings"
Expand Down Expand Up @@ -72,16 +73,14 @@ func (a app) LatestVersions(repository string, patterns []string) (map[string]se
return nil, fmt.Errorf("unable to prepare pattern matching: %s", err)
}

if !strings.Contains(repository, "/") {
repository = fmt.Sprintf("library/%s", repository)
}
var resp *http.Response

bearerToken, err := a.login(ctx, repository)
if err != nil {
return nil, fmt.Errorf("unable to login to registry: %s", err)
if registryURL, parseErr := url.Parse(repository); parseErr == nil && len(registryURL.Host) == 0 {
resp, err = a.dockerRegistry(ctx, repository)
} else {
resp, err = request.New().Get(fmt.Sprintf("%s/tags/list", repository)).Send(ctx, nil)
}

resp, err := request.New().Get(fmt.Sprintf("%s%s/tags/list", a.registryURL, repository)).Header("Authorization", fmt.Sprintf("Bearer %s", bearerToken)).Send(ctx, nil)
if err != nil {
return nil, fmt.Errorf("unable to fetch tags: %s", err)
}
Expand Down Expand Up @@ -114,6 +113,24 @@ func (a app) LatestVersions(repository string, patterns []string) (map[string]se
return versions, nil
}

func (a app) dockerRegistry(ctx context.Context, repository string) (*http.Response, error) {
if !strings.Contains(repository, "/") {
repository = fmt.Sprintf("library/%s", repository)
}

bearerToken, err := a.login(ctx, repository)
if err != nil {
return nil, fmt.Errorf("unable to login to registry: %s", err)
}

resp, err := request.New().Get(fmt.Sprintf("%s%s/tags/list", a.registryURL, repository)).Header("Authorization", fmt.Sprintf("Bearer %s", bearerToken)).Send(ctx, nil)
if err != nil {
return nil, fmt.Errorf("unable to fetch tags: %s", err)
}

return resp, nil
}

func (a app) login(ctx context.Context, repository string) (string, error) {
values := url.Values{}
values.Set("grant_type", "password")
Expand Down

0 comments on commit 547ba49

Please sign in to comment.