-
Notifications
You must be signed in to change notification settings - Fork 30
/
utils.go
42 lines (37 loc) · 1.04 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
package storage
import (
"github.com/pkg/errors"
"net/url"
"strings"
)
func JoinPath(elem ...string) string {
var res []string
for _, e := range elem {
if e != "" {
res = append(res, strings.Trim(e, "/"))
}
}
return strings.Join(res, "/")
}
func AddDelimiterToPath(path string) string {
if strings.HasSuffix(path, "/") || path == "" {
return path
}
return path + "/"
}
// TODO : unit tests
func GetPathFromPrefix(prefix string) (bucket, server string, err error) {
storageUrl, err := url.Parse(prefix)
if err != nil {
return "", "", errors.Wrapf(err, "failed to parse url '%s'", prefix)
}
if storageUrl.Scheme == "" || storageUrl.Host == "" {
return "", "", errors.Errorf("missing url scheme=%q and/or host=%q", storageUrl.Scheme, storageUrl.Host)
}
bucket = storageUrl.Host
server = strings.TrimPrefix(storageUrl.Path, "/")
// Allover the code this parameter is concatenated with '/'.
// TODO: Get rid of numerous string literals concatenated with this
server = strings.TrimSuffix(server, "/")
return bucket, server, nil
}