forked from solo-io/gloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uri.go
43 lines (36 loc) · 919 Bytes
/
uri.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
package cliutil
import (
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/solo-io/go-utils/errors"
)
// Get the resource identified by the given URI.
// The URI can either be an http(s) address or a relative/absolute file path.
func GetResource(uri string) (io.ReadCloser, error) {
var file io.ReadCloser
if strings.HasPrefix(uri, "http://") || strings.HasPrefix(uri, "https://") {
resp, err := http.Get(uri)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, errors.Errorf("http GET returned status %d", resp.StatusCode)
}
file = resp.Body
} else {
path, err := filepath.Abs(uri)
if err != nil {
return nil, errors.Wrapf(err, "getting absolute path for %v", uri)
}
f, err := os.Open(path)
if err != nil {
return nil, errors.Wrapf(err, "opening file %v", path)
}
file = f
}
// Write the body to file
return file, nil
}