Commit 775010f5 authored by Jack Greenfield's avatar Jack Greenfield

Merge pull request #467 from jackgr/repo-search

Fix repo prefix searchbug
parents edb6187e 20c1ed3f
......@@ -92,10 +92,12 @@ func (rs *inmemRepoService) GetRepoByChartURL(URL string) (IRepo, error) {
rs.RLock()
defer rs.RUnlock()
cSplit := strings.Split(URL, "/")
var found IRepo
for _, r := range rs.repositories {
rURL := r.GetURL()
if strings.HasPrefix(URL, rURL) {
rSplit := strings.Split(rURL, "/")
if hasPrefix(cSplit, rSplit) {
if found == nil || len(found.GetURL()) < len(rURL) {
found = r
}
......@@ -109,6 +111,20 @@ func (rs *inmemRepoService) GetRepoByChartURL(URL string) (IRepo, error) {
return found, nil
}
func hasPrefix(cSplit, rSplit []string) bool {
if len(rSplit) > len(cSplit) {
return false
}
for i := range rSplit {
if rSplit[i] != cSplit[i] {
return false
}
}
return true
}
// DeleteRepo removes a known repository from the list
func (rs *inmemRepoService) DeleteRepo(URL string) error {
rs.Lock()
......
......@@ -54,7 +54,7 @@ func TestService(t *testing.T) {
t.Fatalf("invalid repo returned; want: %#v, have %#v.", tr, r1)
}
URL := GCSPublicRepoURL + TestArchiveName
URL := GCSPublicRepoURL + "/" + TestArchiveName
r2, err := rs.GetRepoByChartURL(URL)
if err != nil {
t.Fatal(err)
......
......@@ -25,7 +25,6 @@ import (
"fmt"
"log"
"net/http"
"strings"
"sync"
)
......@@ -117,35 +116,21 @@ func (rp *repoProvider) createRepo(cr IChartRepo) (IChartRepo, error) {
return cr, nil
}
// GetRepoByChartURL returns the repository whose URL is a prefix of the given URL.
// GetRepoByChartURL returns the repository that backs a given chart URL.
func (rp *repoProvider) GetRepoByChartURL(URL string) (IChartRepo, error) {
rp.Lock()
defer rp.Unlock()
if r := rp.findRepoByChartURL(URL); r != nil {
return r, nil
}
cr, err := rp.rs.GetRepoByChartURL(URL)
if err != nil {
return nil, err
}
return rp.createRepoByType(cr)
}
func (rp *repoProvider) findRepoByChartURL(URL string) IChartRepo {
var found IChartRepo
for _, r := range rp.repos {
rURL := r.GetURL()
if strings.HasPrefix(URL, rURL) {
if found == nil || len(found.GetURL()) < len(rURL) {
found = r
}
}
if r, ok := rp.repos[cr.GetURL()]; ok {
return r, nil
}
return found
return rp.createRepoByType(cr)
}
// GetChartByReference maps the supplied chart reference into a fully qualified
......
......@@ -62,7 +62,7 @@ func TestRepoProvider(t *testing.T) {
}
wantRepo := haveRepo
URL := GCSPublicRepoURL + TestArchiveName
URL := GCSPublicRepoURL + "/" + TestArchiveName
haveRepo, err = rp.GetRepoByChartURL(URL)
if err != nil {
t.Fatal(err)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment