forked from virtual-kubelet/virtual-kubelet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
51 lines (43 loc) · 1.18 KB
/
utils.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
package api
import (
"bytes"
"fmt"
"net/url"
"strings"
"text/template"
)
// ResolveRelative combines a url base with a relative path.
func ResolveRelative(basestr, relstr string) string {
u, _ := url.Parse(basestr)
rel, _ := url.Parse(relstr)
u = u.ResolveReference(rel)
us := u.String()
us = strings.Replace(us, "%7B", "{", -1)
us = strings.Replace(us, "%7D", "}", -1)
return us
}
// ExpandURL subsitutes any {{encoded}} strings in the URL passed in using
// the map supplied.
func ExpandURL(u *url.URL, expansions map[string]string) error {
t, err := template.New("url").Parse(u.Path)
if err != nil {
return fmt.Errorf("Parsing template for url path %q failed: %v", u.Path, err)
}
var b bytes.Buffer
if err := t.Execute(&b, expansions); err != nil {
return fmt.Errorf("Executing template for url path failed: %v", err)
}
// set the parameters
u.Path = b.String()
// escape the expansions
for k, v := range expansions {
expansions[k] = url.QueryEscape(v)
}
var bt bytes.Buffer
if err := t.Execute(&bt, expansions); err != nil {
return fmt.Errorf("Executing template for url path failed: %v", err)
}
// set the parameters
u.RawPath = bt.String()
return nil
}