forked from CiscoCloud/toscalib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver.go
44 lines (38 loc) · 886 Bytes
/
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
package toscalib
import (
"io/ioutil"
"net/http"
"net/url"
)
// Resolver defines a function spec that the Parser will use to resolve
// remote Imports.
type Resolver func(string) ([]byte, error)
// DefaultResolver provides a basic implementation for retrieving imports that reference
// remote locations. The file will be downloaded over HTTP(s) and the contents are returned.
func defaultResolver(location string) ([]byte, error) {
var r []byte
u, err := url.Parse(location)
if err != nil {
return r, err
}
switch u.Scheme {
case "http", "https":
var res *http.Response
res, err = http.Get(u.String())
if err != nil {
return r, err
}
defer res.Body.Close()
r, err = ioutil.ReadAll(res.Body)
if err != nil {
return r, err
}
return r, nil
default:
r, err = ioutil.ReadFile(location)
if err != nil {
return r, err
}
return r, nil
}
}