diff --git a/app/app.go b/app/app.go index 6e47e47..edacdde 100644 --- a/app/app.go +++ b/app/app.go @@ -4,10 +4,10 @@ import ( "strings" ) -// GenerateRegistryPrefix generates destination Docker registry prefix path from the source registry name -func GenerateRegistryPrefix(registry string) string { - allParts := strings.Split(registry, ":") - hostname := allParts[0] +// GeneratePathFromHostname generates "/"-delimited path from a hostname[:port] +func GeneratePathFromHostname(hostname string) string { + allParts := strings.Split(hostname, ":") + hostPart := allParts[0] - return "/" + strings.Replace(hostname, ".", "/", -1) + return "/" + strings.Replace(hostPart, ".", "/", -1) } diff --git a/app/app_test.go b/app/app_test.go new file mode 100644 index 0000000..565587f --- /dev/null +++ b/app/app_test.go @@ -0,0 +1,27 @@ +package app + +import ( + "testing" +) + +func TestGeneratePathFromHostname(t *testing.T) { + examples := map[string]string{ + "localhost": "/localhost", + "localhost:5000": "/localhost", + "registry.company.com": "/registry/company/com", + "dockerz.hipster.io:8443": "/dockerz/hipster/io", + } + + for input, expected := range examples { + output := GeneratePathFromHostname(input) + + if output != expected { + t.Fatalf( + "Unexpected path '%s' generated from hostname '%s' (expected: '%s')", + output, + input, + expected, + ) + } + } +} diff --git a/main.go b/main.go index daa6a2d..8bee9f3 100644 --- a/main.go +++ b/main.go @@ -316,7 +316,7 @@ func main() { for _, tg := range tags { prefix := o.PushPrefix if prefix == "" { - prefix = app.GenerateRegistryPrefix(registry) + prefix = app.GeneratePathFromHostname(registry) } srcRef := repo + ":" + tg.GetName()