-
Notifications
You must be signed in to change notification settings - Fork 5
/
db.go
51 lines (44 loc) · 1.05 KB
/
db.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
package main
import (
"io/ioutil"
"os"
"path/filepath"
)
type DB struct {
path string
}
func NewDB(path string) *DB {
absPath, _ := filepath.Abs(path)
_ = os.MkdirAll(absPath, os.ModePerm)
return &DB{path: absPath}
}
func (db *DB) Get(key string) (string, error) {
content, err := ioutil.ReadFile(filepath.Join(db.path, key))
if err != nil {
return "", err
}
return string(content), nil
}
func (db *DB) Set(key, val string) error {
return ioutil.WriteFile(filepath.Join(db.path, key), []byte(val), 0644)
}
type DBs struct {
memory *DB
logs *DB
identity *DB
input *DB
workspace *DB
}
func NewDBs(rootPath string) *DBs {
inputPath := filepath.Join(rootPath, "example")
memoryPath := filepath.Join(inputPath, "memory")
workspacePath := filepath.Join(inputPath, "workspace")
identityPath := filepath.Join(rootPath, "identity")
return &DBs{
memory: NewDB(memoryPath),
logs: NewDB(filepath.Join(memoryPath, "logs")),
identity: NewDB(identityPath),
input: NewDB(inputPath),
workspace: NewDB(workspacePath),
}
}