-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstorage_key.go
88 lines (71 loc) · 1.86 KB
/
storage_key.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"fmt"
"path/filepath"
"strings"
"time"
)
const (
unknownKeyVersion = "unknown"
storageKeyVersionTsFormat = "20060102T150405"
)
type storageKeyVersion struct {
key string
ts time.Time
}
func newStorageKeyVersion(key string, ts time.Time) *storageKeyVersion {
return &storageKeyVersion{
key: replaceSlashesToUnderscores(key),
ts: ts,
}
}
func parseStorageKeyVersion(s string) (*storageKeyVersion, error) {
p := strings.Split(s, "_")
if len(p) < 2 {
return nil, fmt.Errorf("wrong storage key version: %s", s)
}
pts := p[len(p)-1]
ts, err := time.Parse(storageKeyVersionTsFormat, pts)
if err != nil {
return nil, fmt.Errorf("wrong storage key version: %s: %s", s, err)
}
return &storageKeyVersion{
key: replaceSlashesToUnderscores(strings.TrimSuffix(s, "_"+pts)),
ts: ts,
}, nil
}
func (v *storageKeyVersion) String() string {
return fmt.Sprintf("%s_%s", v.key, v.ts.Format(storageKeyVersionTsFormat))
}
type storageKey struct {
group string
project string
version *storageKeyVersion
}
func newStorageKey(group, project string, version *storageKeyVersion) *storageKey {
return &storageKey{
group: replaceSlashesToUnderscores(group),
project: replaceSlashesToUnderscores(project),
version: version,
}
}
func parseStorageKey(s string) (*storageKey, error) {
p := strings.SplitN(strings.Trim(s, "/"), "/", 3)
if len(p) < 3 {
return nil, fmt.Errorf("wrong storage key: %s", s)
}
version, err := parseStorageKeyVersion(p[2])
if err != nil {
return nil, fmt.Errorf("wrong storage key: %s: %s", s, err)
}
return &storageKey{p[0], p[1], version}, nil
}
func (k *storageKey) getPath() string {
if k.version == nil {
k.version = newStorageKeyVersion(unknownKeyVersion, time.Now())
}
return filepath.Join(k.group, k.project, k.version.String())
}
func (k *storageKey) String() string {
return k.getPath()
}