Skip to content

Commit

Permalink
Merge pull request drone-plugins#169 from colinhoglund/fix_registry_s…
Browse files Browse the repository at this point in the history
…tring_trimming

Fix ECR registry string trimming
  • Loading branch information
bradrydzewski authored Feb 21, 2018
2 parents 938e42c + 047967e commit aa9e40e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pipeline:
commands:
- go get -u github.com/golang/dep/cmd/dep
- dep ensure
- go vet ./...
- go test -cover ./...
- sh .drone.sh

publish:
Expand Down
10 changes: 8 additions & 2 deletions cmd/drone-docker-ecr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ func main() {
}

if create {
err = ensureRepoExists(svc, strings.TrimPrefix(repo, registry))
err = ensureRepoExists(svc, trimHostname(repo, registry))
if err != nil {
os.Exit(1)
log.Fatal(fmt.Sprintf("error creating ECR repo: %v", err))
}
}

Expand All @@ -75,6 +75,12 @@ func main() {
}
}

func trimHostname(repo, registry string) string {
repo = strings.TrimPrefix(repo, registry)
repo = strings.TrimLeft(repo, "/")
return repo
}

func ensureRepoExists(svc *ecr.ECR, name string) (err error) {
input := &ecr.CreateRepositoryInput{}
input.SetRepositoryName(name)
Expand Down
20 changes: 20 additions & 0 deletions cmd/drone-docker-ecr/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import "testing"

func TestTrimHostname(t *testing.T) {
registry := "000000000000.dkr.ecr.us-east-1.amazonaws.com"
// map full repo path to expected repo name
repos := map[string]string{
registry + "/repo": "repo",
registry + "/namespace/repo": "namespace/repo",
registry + "/namespace/namespace/repo": "namespace/namespace/repo",
}

for repo, name := range repos {
splitName := trimHostname(repo, registry)
if splitName != name {
t.Errorf("%s is not equal to %s.", splitName, name)
}
}
}

0 comments on commit aa9e40e

Please sign in to comment.