/
parse.go
68 lines (61 loc) · 1.57 KB
/
parse.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package barf
import (
"errors"
"strings"
)
var (
errorBadPath = errors.New("Unable to parse provided path.")
errorUnsupportedProtocol = errors.New("Unsupported filesystem.")
errorNoBucket = errors.New("No bucket specified.")
)
func parseSrc(srcPath string) (string, string, error) {
// first check if empty
if srcPath == "" {
return "", "", errorBadPath
}
parts := strings.Split(srcPath, "://")
if len(parts) == 2 {
// ok, we have a supplied protocol!
// now need to check if protocol we support.
switch parts[0] {
case "s3":
return "s3", parts[1], nil
case "fs":
return "fs", parts[1], nil
default:
return "", "", errorUnsupportedProtocol
}
} else if len(parts) < 2 {
// so no protocol specified, so assume filesystem
return "fs", parts[0], nil
} else {
return "", "", errorBadPath
}
}
func parseBucket(bucketPath string) (string, string, error) {
if bucketPath == "" {
return "", "", errorNoBucket
}
parts := strings.Split(bucketPath, "/")
if parts[0] == "" {
return "", "", errorNoBucket
}
// return prefix if we have it
if len(parts) > 1 {
return parts[0], strings.Join(parts[1:], "/"), nil
}
return parts[0], "", nil
}
// parses compression from s3 file keys
func parseCompression(key string) string {
possible := []string{".gz", ".bz2"}
for _, ending := range possible {
// does key end in one of possible endings
i := strings.Index(key, ending)
if i == len(key)-len(ending) {
return strings.TrimPrefix(ending, ".") // return file ending but without "."
}
}
// no compression detected
return ""
}