-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathlocal.go
57 lines (49 loc) · 1.01 KB
/
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
50
51
52
53
54
55
56
57
package local
import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"github.com/k1LoW/octocov/report"
)
type Local struct {
root string
}
func New(root string) (*Local, error) {
p, err := filepath.Abs(root)
if err != nil {
return nil, err
}
fi, err := os.Stat(p)
if err != nil {
return nil, err
}
if !fi.IsDir() {
return nil, fmt.Errorf("%s is not directory", root)
}
return &Local{
root: root,
}, nil
}
func (l *Local) Root() string {
return l.root
}
func (l *Local) StoreReport(ctx context.Context, r *report.Report) error {
path := fmt.Sprintf("%s/report.json", r.Repository)
return l.Put(ctx, path, r.Bytes())
}
func (l *Local) Put(ctx context.Context, path string, content []byte) error {
p := filepath.Join(l.root, path)
dir := filepath.Dir(p)
if _, err := os.Stat(dir); err != nil {
err := os.MkdirAll(dir, 0755) // #nosec
if err != nil {
return err
}
}
return os.WriteFile(p, content, os.ModePerm) // #nosec
}
func (l *Local) FS() (fs.FS, error) {
return os.DirFS(l.root), nil
}