use Trim functions to avoid index out of range panic
This commit is contained in:
parent
a8da259d52
commit
cfb097f407
@ -55,7 +55,7 @@ func main() {
|
||||
}
|
||||
|
||||
if create {
|
||||
err = ensureRepoExists(svc, getRepoName(repo))
|
||||
err = ensureRepoExists(svc, trimHostname(repo, registry))
|
||||
if err != nil {
|
||||
log.Fatal(fmt.Sprintf("error creating ECR repo: %v", err))
|
||||
}
|
||||
@ -75,8 +75,10 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func getRepoName(repo string) string {
|
||||
return strings.SplitN(repo, "/", 2)[1]
|
||||
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) {
|
||||
|
@ -2,16 +2,17 @@ package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestGetRepoName(t *testing.T) {
|
||||
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{
|
||||
"000000000000.dkr.ecr.us-east-1.amazonaws.com/repo": "repo",
|
||||
"000000000000.dkr.ecr.us-east-1.amazonaws.com/namespace/repo": "namespace/repo",
|
||||
"000000000000.dkr.ecr.us-east-1.amazonaws.com/namespace/namespace/repo": "namespace/namespace/repo",
|
||||
registry + "/repo": "repo",
|
||||
registry + "/namespace/repo": "namespace/repo",
|
||||
registry + "/namespace/namespace/repo": "namespace/namespace/repo",
|
||||
}
|
||||
|
||||
for repo, name := range repos {
|
||||
splitName := getRepoName(repo)
|
||||
splitName := trimHostname(repo, registry)
|
||||
if splitName != name {
|
||||
t.Errorf("%s is not equal to %s.", splitName, name)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user