From 7e50189a05d4ff8233197dc948cc8fb11a780e33 Mon Sep 17 00:00:00 2001 From: Tianon Gravi Date: Mon, 28 Jan 2019 16:03:21 -0800 Subject: [PATCH] Use pointers more consistently, allow "fetch" tag validation to check SharedTags too --- manifest/fetch.go | 19 +++++++++++++------ manifest/rfc2822.go | 12 ++++++------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/manifest/fetch.go b/manifest/fetch.go index 93b61f6..dc105ed 100644 --- a/manifest/fetch.go +++ b/manifest/fetch.go @@ -9,6 +9,13 @@ import ( "strings" ) +func validateTagName(man *Manifest2822, repoName, tagName string) error { + if tagName != "" && (man.GetTag(tagName) == nil && len(man.GetSharedTag(tagName)) == 0) { + return fmt.Errorf("tag not found in manifest for %q: %q", repoName, tagName) + } + return nil +} + // "library" is the default "library directory" // returns the parsed version of (in order): // if "repo" is a URL, the remote contents of that URL @@ -33,10 +40,10 @@ func Fetch(library, repo string) (string, string, *Manifest2822, error) { } defer resp.Body.Close() man, err := Parse(resp.Body) - if tagName != "" && man.GetTag(tagName) == nil { - return repoName, tagName, man, fmt.Errorf("tag not found in manifest for %q: %q", repoName, tagName) + if err != nil { + return repoName, tagName, man, err } - return repoName, tagName, man, err + return repoName, tagName, man, validateTagName(man, repoName, tagName) } // try file paths @@ -55,10 +62,10 @@ func Fetch(library, repo string) (string, string, *Manifest2822, error) { if err == nil { defer f.Close() man, err := Parse(f) - if tagName != "" && man.GetTag(tagName) == nil { - return repoName, tagName, man, fmt.Errorf("tag not found in manifest for %q: %q", repoName, tagName) + if err != nil { + return repoName, tagName, man, err } - return repoName, tagName, man, err + return repoName, tagName, man, validateTagName(man, repoName, tagName) } } diff --git a/manifest/rfc2822.go b/manifest/rfc2822.go index fe83a0f..4176029 100644 --- a/manifest/rfc2822.go +++ b/manifest/rfc2822.go @@ -308,20 +308,20 @@ func (entry Manifest2822Entry) HasArchitecture(arch string) bool { } func (manifest Manifest2822) GetTag(tag string) *Manifest2822Entry { - for _, entry := range manifest.Entries { + for i, entry := range manifest.Entries { if entry.HasTag(tag) { - return &entry + return &manifest.Entries[i] } } return nil } // GetSharedTag returns a list of entries with the given tag in entry.SharedTags (or the empty list if there are no entries with the given tag). -func (manifest Manifest2822) GetSharedTag(tag string) []Manifest2822Entry { - ret := []Manifest2822Entry{} - for _, entry := range manifest.Entries { +func (manifest Manifest2822) GetSharedTag(tag string) []*Manifest2822Entry { + ret := []*Manifest2822Entry{} + for i, entry := range manifest.Entries { if entry.HasSharedTag(tag) { - ret = append(ret, entry) + ret = append(ret, &manifest.Entries[i]) } } return ret