-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresolver.go
88 lines (78 loc) · 1.93 KB
/
resolver.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package cwl
import (
"io/ioutil"
"net/http"
"net/url"
"path/filepath"
)
// Resolver describes a type which resolves docment
// by location, such as loading relative file paths
// referenced in a CWL document.
type Resolver interface {
// Resolve is given the current `base`,
// such as the directory of the root document,
// and the `location` referenced by the CWL document.
//
// Upon success, the document bytes and the new `base`
// should be returned.
Resolve(base, location string) (doc []byte, newBase string, err error)
}
// DefaultResolver is a document location resolver which
// resolves local file paths.
type DefaultResolver struct{}
func (DefaultResolver) Resolve(base, loc string) ([]byte, string, error) {
if u, ok := isHTTP(base, loc); ok {
b, err := httpGet(u)
if err != nil {
return nil, "", err
}
return b, u.String(), nil
}
if !filepath.IsAbs(loc) {
loc = filepath.Clean(filepath.Join(base, loc))
}
b, err := ioutil.ReadFile(loc)
if err != nil {
return nil, "", err
}
dir := filepath.Dir(loc)
return b, dir, nil
}
// NoResolve is a special case resolver which does not
// resolve documents, but instead creates `DocumentRef`
// instances in the document tree.
func NoResolve() Resolver {
return noResolver{}
}
type noResolver struct {
Resolver
}
func isHTTP(base, loc string) (*url.URL, bool) {
if base == "" {
base, loc = loc, base
}
b, err := url.Parse(base)
if err != nil {
return nil, false
}
l, err := url.Parse(loc)
if err != nil {
return nil, false
}
if !b.IsAbs() {
return nil, false
}
return b.ResolveReference(l), true
}
func httpGet(u *url.URL) ([]byte, error) {
resp, err := http.Get(u.String())
if err != nil {
return nil, errf(`failed to resolve HTTP URL %s: %s`, u.String(), err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errf(`failed to read HTTP response body for %s: %s`, u.String(), err)
}
return body, nil
}