-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.go
49 lines (38 loc) · 954 Bytes
/
local.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 (
"context"
"io/ioutil"
"os"
"path"
"github.com/mlhamel/trieugene/pkg/config"
)
type Local struct {
cfg *config.Config
prefix string
}
func NewLocal(cfg *config.Config) Store {
return &Local{cfg: cfg}
}
func (l *Local) Setup(ctx context.Context) error {
err := os.MkdirAll(l.cfg.LocalPrefix(), os.ModePerm)
if err != nil {
return err
}
return nil
}
func (l *Local) Persist(ctx context.Context, filename string, data string) error {
dataAsByte := []byte(data)
filenameWithPrefix := path.Join(l.cfg.LocalPrefix(), filename)
dirName := path.Dir(filenameWithPrefix)
l.cfg.Logger().Debug().Str("dirname", dirName).Msg("Creating directory")
err := os.MkdirAll(dirName, os.ModePerm)
if err != nil {
return err
}
l.cfg.Logger().Debug().Str("filenameWithPrefix", dirName).Msg("Persisting file")
err = ioutil.WriteFile(filenameWithPrefix, dataAsByte, 0644)
if err != nil {
return err
}
return nil
}