-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.go
74 lines (60 loc) 路 1.54 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package webindexer
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/charmbracelet/log"
)
type LocalBackend struct {
path string
cfg Config
}
func (l *LocalBackend) Read(path string) ([]Item, error) {
var items []Item
log.Debugf("Listing files in %s", path)
files, err := os.ReadDir(path)
if err != nil {
return nil, fmt.Errorf("unable to read source path %s: %w", path, err)
}
for _, file := range files {
if shouldSkip(file.Name(), l.cfg.IndexFile, l.cfg.Skips) {
continue
}
stat, err := os.Stat(filepath.Join(path, file.Name()))
if err != nil {
return nil, fmt.Errorf("unable to stat file %s: %w", file.Name(), err)
}
size := humanizeBytes(stat.Size())
modified := stat.ModTime().Format(l.cfg.DateFormat)
itemName := file.Name()
item := Item{
Name: itemName,
Size: size,
LastModified: modified,
IsDir: file.IsDir(),
}
items = append(items, item)
}
return items, nil
}
func (l *LocalBackend) Write(data Data, content string) error {
prefix := data.RelativePath
prefix = strings.TrimPrefix(prefix, l.cfg.BasePath)
localPath := filepath.Join(l.cfg.Target, prefix)
if err := os.MkdirAll(localPath, 0o750); err != nil {
return fmt.Errorf("failed to create directory %s: %w", localPath, err)
}
filePath := filepath.Join(localPath, l.cfg.IndexFile)
file, err := os.Create(filePath) // #nosec
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString(content)
if err != nil {
return err
}
log.Infof("Generated %s", filePath)
return nil
}