/
api.go
49 lines (42 loc) · 1.18 KB
/
api.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
package store
import (
"io"
paths "path"
)
// File represents a filesystem entry
type File struct {
IsFolder bool
Name string
}
type ReadOnlyStore interface {
Read(path string, contents io.Writer) error
List(path string) ([]File, error)
}
// SimpleFileStore is a simplied interface over a file store
type SimpleFileStore interface {
Create(path string, contents io.Reader) error
Read(path string, contents io.Writer) error
Update(path string, contents io.Reader) error
Delete(path string) error
List(path string) ([]File, error)
TestPath(path string) (bool, error)
}
// define this to allow us to unit test the recursive ID getter
type idGetter interface {
GetID(name string, parentID string) (string, error)
}
// GetIDRecursive navigates up the directory tree
func GetIDRecursive(client idGetter, path string) (string, error) {
parentPath, name := paths.Dir(path), paths.Base(path)
if parentPath == "." {
// No more parent IDs to get
return client.GetID(name, "")
}
// Not root level. Find in parent
parentID, err := GetIDRecursive(client, parentPath)
if err != nil {
return "", err
}
return client.GetID(name, parentID)
}
// TODO: implement caching around ID getter