This repository has been archived by the owner on Aug 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
fetch.go
73 lines (67 loc) · 2.07 KB
/
fetch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package manifest
import (
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
"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
// if "repo" is a relative path like "./repo", that file
// the file "library/repo"
// (repoName, tagName, man, err)
func Fetch(library, repo string) (string, string, *Manifest2822, error) {
repoName := filepath.Base(repo)
tagName := ""
if tagIndex := strings.IndexRune(repoName, ':'); tagIndex > 0 {
tagName = repoName[tagIndex+1:]
repoName = repoName[:tagIndex]
repo = strings.TrimSuffix(repo, ":"+tagName)
}
u, err := url.Parse(repo)
if err == nil && u.IsAbs() && (u.Scheme == "http" || u.Scheme == "https") {
// must be remote URL!
resp, err := http.Get(repo)
if err != nil {
return repoName, tagName, nil, err
}
defer resp.Body.Close()
man, err := Parse(resp.Body)
if err != nil {
return repoName, tagName, man, err
}
return repoName, tagName, man, validateTagName(man, repoName, tagName)
}
// try file paths
filePaths := []string{}
if filepath.IsAbs(repo) || strings.IndexRune(repo, filepath.Separator) >= 0 || strings.IndexRune(repo, '/') >= 0 {
filePaths = append(filePaths, repo)
}
if !filepath.IsAbs(repo) {
filePaths = append(filePaths, filepath.Join(library, repo))
}
for _, fileName := range filePaths {
f, err := os.Open(fileName)
if err != nil && !os.IsNotExist(err) {
return repoName, tagName, nil, err
}
if err == nil {
defer f.Close()
man, err := Parse(f)
if err != nil {
return repoName, tagName, man, err
}
return repoName, tagName, man, validateTagName(man, repoName, tagName)
}
}
return repoName, tagName, nil, fmt.Errorf("unable to find a manifest named %q (in %q or as a remote URL)", repo, library)
}