From a8db3be02b823f19179519393ae6f46df9f650e9 Mon Sep 17 00:00:00 2001 From: Igor Zibarev Date: Mon, 18 Jun 2018 15:23:29 +0300 Subject: [PATCH] ref(helm): expose Get for repository file Get method is a useful shortcut that can be used by other apps that use helm repo package. --- pkg/repo/repo.go | 14 +++++++++---- pkg/repo/repo_test.go | 47 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/pkg/repo/repo.go b/pkg/repo/repo.go index 194eace79..dfe2d9ed2 100644 --- a/pkg/repo/repo.go +++ b/pkg/repo/repo.go @@ -116,12 +116,18 @@ func (r *RepoFile) Update(re ...*Entry) { // Has returns true if the given name is already a repository name. func (r *RepoFile) Has(name string) bool { - for _, rf := range r.Repositories { - if rf.Name == name { - return true + _, ok := r.Get(name) + return ok +} + +// Get returns entry by the given name if it exists. +func (r *RepoFile) Get(name string) (*Entry, bool) { + for _, entry := range r.Repositories { + if entry.Name == name { + return entry, true } } - return false + return nil, false } // Remove removes the entry from the list of repositories. diff --git a/pkg/repo/repo_test.go b/pkg/repo/repo_test.go index 4b5bcdbf5..a7716102e 100644 --- a/pkg/repo/repo_test.go +++ b/pkg/repo/repo_test.go @@ -16,10 +16,12 @@ limitations under the License. package repo -import "testing" -import "io/ioutil" -import "os" -import "strings" +import ( + "io/ioutil" + "os" + "strings" + "testing" +) const testRepositoriesFile = "testdata/repositories.yaml" @@ -120,6 +122,43 @@ func TestNewPreV1RepositoriesFile(t *testing.T) { } } +func TestRepoFile_Get(t *testing.T) { + repo := NewRepoFile() + repo.Add( + &Entry{ + Name: "first", + URL: "https://example.com/first", + Cache: "first-index.yaml", + }, + &Entry{ + Name: "second", + URL: "https://example.com/second", + Cache: "second-index.yaml", + }, + &Entry{ + Name: "third", + URL: "https://example.com/third", + Cache: "third-index.yaml", + }, + &Entry{ + Name: "fourth", + URL: "https://example.com/fourth", + Cache: "fourth-index.yaml", + }, + ) + + name := "second" + + entry, ok := repo.Get(name) + if !ok { + t.Fatalf("Expected repo entry %q to be found", name) + } + + if entry.URL != "https://example.com/second" { + t.Fatalf("Expected repo URL to be %q but got %q", "https://example.com/second", entry.URL) + } +} + func TestRemoveRepository(t *testing.T) { sampleRepository := NewRepoFile() sampleRepository.Add(