From 854919fae8782cbb4d005959b8cfeecdd1ef50ce Mon Sep 17 00:00:00 2001 From: Taylor Thomas Date: Mon, 7 Oct 2019 14:18:33 -0600 Subject: [PATCH] feat(repo): Ports repo file `Get` method from v2 This is a port of #3478 with some slight refactors to make it a bit more friendly. It is technically a breaking change as it is changing the method signature from v2 Signed-off-by: Taylor Thomas --- pkg/repo/repo.go | 14 ++++++++++---- pkg/repo/repo_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/pkg/repo/repo.go b/pkg/repo/repo.go index b73efdd9b..6f1e90dad 100644 --- a/pkg/repo/repo.go +++ b/pkg/repo/repo.go @@ -81,12 +81,18 @@ func (r *File) update(e *Entry) { // Has returns true if the given name is already a repository name. func (r *File) Has(name string) bool { - for _, rf := range r.Repositories { - if rf.Name == name { - return true + entry := r.Get(name) + return entry != nil +} + +// Get returns an entry with the given name if it exists, otherwise returns nil +func (r *File) Get(name string) *Entry { + for _, entry := range r.Repositories { + if entry.Name == name { + return entry } } - return false + return nil } // Remove removes the entry from the list of repositories. diff --git a/pkg/repo/repo_test.go b/pkg/repo/repo_test.go index 1ed3fb298..f87d2c202 100644 --- a/pkg/repo/repo_test.go +++ b/pkg/repo/repo_test.go @@ -91,6 +91,44 @@ func TestNewFile(t *testing.T) { } } +func TestRepoFile_Get(t *testing.T) { + repo := NewFile() + repo.Add( + &Entry{ + Name: "first", + URL: "https://example.com/first", + }, + &Entry{ + Name: "second", + URL: "https://example.com/second", + }, + &Entry{ + Name: "third", + URL: "https://example.com/third", + }, + &Entry{ + Name: "fourth", + URL: "https://example.com/fourth", + }, + ) + + name := "second" + + entry := repo.Get(name) + if entry == nil { + t.Fatalf("Expected repo entry %q to be found", name) + } + + if entry.URL != "https://example.com/second" { + t.Errorf("Expected repo URL to be %q but got %q", "https://example.com/second", entry.URL) + } + + entry = repo.Get("nonexistent") + if entry != nil { + t.Errorf("Got unexpected entry %+v", entry) + } +} + func TestRemoveRepository(t *testing.T) { sampleRepository := NewFile() sampleRepository.Add(