-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.go
More file actions
46 lines (37 loc) · 982 Bytes
/
Copy pathstorage.go
File metadata and controls
46 lines (37 loc) · 982 Bytes
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
package contexting
import (
"encoding/json"
"fmt"
"os"
"time"
)
type ContextIndex struct {
RootPath string `json:"root_path"`
GeneratedAt time.Time `json:"generated_at"`
Model string `json:"model,omitempty"`
Tree *Node `json:"tree"`
}
func SaveContextIndex(path string, index *ContextIndex) error {
if index == nil {
return fmt.Errorf("index is nil")
}
bytes, err := json.MarshalIndent(index, "", " ")
if err != nil {
return fmt.Errorf("marshal index: %w", err)
}
if err := writeFileAtomic(path, append(bytes, '\n'), 0o644); err != nil {
return fmt.Errorf("write index file: %w", err)
}
return nil
}
func LoadContextIndex(path string) (*ContextIndex, error) {
bytes, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read index file: %w", err)
}
var index ContextIndex
if err := json.Unmarshal(bytes, &index); err != nil {
return nil, fmt.Errorf("parse index file: %w", err)
}
return &index, nil
}