forked from cloudfoundry-incubator/cflocal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.go
39 lines (35 loc) · 793 Bytes
/
url.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
package version
import (
"bytes"
"errors"
"io/ioutil"
"net/http"
"text/template"
)
type URL struct {
Client *http.Client
}
var (
ErrNetwork = errors.New("no network connection")
ErrUnavailable = errors.New("version unavailable")
)
func (u *URL) Build(tmplURL, versionURL string) (string, error) {
resp, err := u.Client.Get(versionURL)
if err != nil {
return "", ErrNetwork
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return "", ErrUnavailable
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
urlBuf := &bytes.Buffer{}
tmpl := template.Must(template.New("").Parse(tmplURL))
if err := tmpl.Execute(urlBuf, string(bytes.TrimSpace(body))); err != nil {
return "", err
}
return urlBuf.String(), nil
}