Skip to content

Commit

Permalink
Extract Docker registry from image name
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanilves committed Sep 22, 2017
1 parent c126de3 commit 595e423
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 13 deletions.
57 changes: 44 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"strings"

"github.com/jessevdk/go-flags"

Expand All @@ -15,14 +16,14 @@ import (
)

type options struct {
Registry string `short:"r" long:"registry" default:"registry.hub.docker.com" description:"Docker registry to use" env:"REGISTRY"`
Username string `short:"u" long:"username" default:"" description:"Docker registry username" env:"USERNAME"`
Password string `short:"p" long:"password" default:"" description:"Docker registry password" env:"PASSWORD"`
DockerJSON string `shord:"j" long:"docker-json" default:"~/.docker/config.json" env:"DOCKER_JSON"`
Concurrency int `short:"c" long:"concurrency" default:"32" description:"Concurrent request limit while querying registry" env:"CONCURRENCY"`
TraceRequests bool `short:"T" long:"trace-requests" description:"Trace registry HTTP requests" env:"TRACE_REQUESTS"`
Version bool `short:"V" long:"version" description:"Show version and exit"`
Positional struct {
DefaultRegistry string `short:"r" long:"default-registry" default:"registry.hub.docker.com" description:"Docker registry to use by default" env:"DEFAULT_REGISTRY"`
Username string `short:"u" long:"username" default:"" description:"Docker registry username" env:"USERNAME"`
Password string `short:"p" long:"password" default:"" description:"Docker registry password" env:"PASSWORD"`
DockerJSON string `shord:"j" long:"docker-json" default:"~/.docker/config.json" env:"DOCKER_JSON"`
Concurrency int `short:"c" long:"concurrency" default:"32" description:"Concurrent request limit while querying registry" env:"CONCURRENCY"`
TraceRequests bool `short:"T" long:"trace-requests" description:"Trace registry HTTP requests" env:"TRACE_REQUESTS"`
Version bool `short:"V" long:"version" description:"Show version and exit"`
Positional struct {
Repository string `positional-arg-name:"REPOSITORY" description:"Docker repository to list tags from"`
} `positional-args:"yes"`
}
Expand All @@ -36,6 +37,32 @@ func getVersion() string {
return VERSION
}

func isHostname(s string) bool {
if strings.Contains(s, ".") {
return true
}

if strings.Contains(s, ":") {
return true
}

if s == "localhost" {
return true
}

return false
}

func getRegistryName(repository, defaultRegistry string) string {
r := strings.Split(repository, "/")[0]

if isHostname(r) {
return r
}

return defaultRegistry
}

func assignCredentials(registry, passedUsername, passedPassword, dockerJSON string) (string, string, error) {
useDefaultDockerJSON := dockerJSON == "~/.docker/config.json"
areCredentialsPassed := passedUsername != "" && passedPassword != ""
Expand Down Expand Up @@ -75,23 +102,27 @@ func main() {
if o.Positional.Repository == "" {
suicide(errors.New("You should provide a repository name, e.g. 'nginx' or 'mesosphere/chronos'"))
}

registry.TraceRequests = o.TraceRequests
repoRegistryName := registry.FormatRepoName(o.Positional.Repository, o.Registry)
repoLocalName := local.FormatRepoName(o.Positional.Repository, o.Registry)

username, password, err := assignCredentials(o.Registry, o.Username, o.Password, o.DockerJSON)
registryName := getRegistryName(o.Positional.Repository, o.DefaultRegistry)

repoRegistryName := registry.FormatRepoName(o.Positional.Repository, registryName)
repoLocalName := local.FormatRepoName(o.Positional.Repository, registryName)

username, password, err := assignCredentials(registryName, o.Username, o.Password, o.DockerJSON)
if err != nil {
suicide(err)
}

tresp, err := auth.NewToken(o.Registry, repoRegistryName, username, password)
tresp, err := auth.NewToken(registryName, repoRegistryName, username, password)
if err != nil {
suicide(err)
}

authorization := getAuthorization(tresp)

registryTags, err := registry.FetchTags(o.Registry, repoRegistryName, authorization, o.Concurrency)
registryTags, err := registry.FetchTags(registryName, repoRegistryName, authorization, o.Concurrency)
if err != nil {
suicide(err)
}
Expand Down
23 changes: 23 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,29 @@ func TestGetAuthorization(t *testing.T) {
}
}

func TestGetRegistryName(t *testing.T) {
expected := map[string]string{
"mesosphere/marathon": dockerHub,
"bogohost/my/inner/troll": dockerHub,
"registry.hipsta.io/hype/hotshit": "registry.hipsta.io",
"localhost/my/image": "localhost",
"bogohost:5000/mymymy/img": "bogohost:5000",
}

for repo, expectedRegistryName := range expected {
registryName := getRegistryName(repo, dockerHub)

if registryName != expectedRegistryName {
t.Fatalf(
"Got unexpected Docker registry name '%s' from repo '%s' (expected: '%s')",
registryName,
repo,
expectedRegistryName,
)
}
}
}

func TestAssignCredentialsWithPassedUsernameAndPassword(t *testing.T) {
flag.Parse()
if *runIntegrationTests {
Expand Down

0 comments on commit 595e423

Please sign in to comment.