-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
join.go
47 lines (37 loc) · 930 Bytes
/
join.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
package urlx
import (
"net/url"
"path"
"github.com/ory/x/cmdx"
)
// MustJoin joins the paths of two URLs. Fatals if first is not a DSN.
func MustJoin(first string, parts ...string) string {
u, err := url.Parse(first)
if err != nil {
cmdx.Fatalf("Unable to parse %s: %s", first, err)
}
return AppendPaths(u, parts...).String()
}
// AppendPaths appends the provided paths to the url.
func AppendPaths(u *url.URL, paths ...string) (ep *url.URL) {
ep = Copy(u)
if len(paths) == 0 {
return ep
}
ep.Path = path.Join(append([]string{ep.Path}, paths...)...)
last := paths[len(paths)-1]
if last[len(last)-1] == '/' {
ep.Path = ep.Path + "/"
}
return ep
}
// SetQuery appends the provided url values to the DSN's query string.
func SetQuery(u *url.URL, query url.Values) (ep *url.URL) {
ep = Copy(u)
q := ep.Query()
for k := range query {
q.Set(k, query.Get(k))
}
ep.RawQuery = q.Encode()
return ep
}