gunk-dev/the-valley

homedir_store.go

sourcecaaa5bee5e11main's published tip history
4.4 KiB
1package run
2
3import (
4 "fmt"
5 "os"
6 "path/filepath"
7)
8
9// SessionsDir returns the base directory for all sessions (~/.klaus/sessions).
10func SessionsDir() (string, error) {
11 home, err := os.UserHomeDir()
12 if err != nil {
13 return "", fmt.Errorf("resolving home dir: %w", err)
14 }
15 return filepath.Join(home, ".klaus", "sessions"), nil
16}
17
18// HomeDirStore implements StateStore using ~/.klaus/sessions/{session-id}/.
19type HomeDirStore struct {
20 baseDir string // e.g. ~/.klaus/sessions/{session-id}
21}
22
23// NewHomeDirStore creates a new HomeDirStore for the given session ID.
24// It resolves ~/.klaus via os.UserHomeDir().
25func NewHomeDirStore(sessionID string) (*HomeDirStore, error) {
26 dir, err := SessionsDir()
27 if err != nil {
28 return nil, err
29 }
30 baseDir := filepath.Join(dir, sessionID)
31 return &HomeDirStore{baseDir: baseDir}, nil
32}
33
34// NewHomeDirStoreFromPath creates a HomeDirStore with an explicit base directory.
35// Useful for testing.
36func NewHomeDirStoreFromPath(baseDir string) *HomeDirStore {
37 return &HomeDirStore{baseDir: baseDir}
38}
39
40func (s *HomeDirStore) StateDir() string {
41 return filepath.Join(s.baseDir, "runs")
42}
43
44func (s *HomeDirStore) LogDir() string {
45 return filepath.Join(s.baseDir, "logs")
46}
47
48func (s *HomeDirStore) BaseDir() string {
49 return s.baseDir
50}
51
52func (s *HomeDirStore) EnsureDirs() error {
53 if err := os.MkdirAll(s.StateDir(), 0o755); err != nil {
54 return fmt.Errorf("creating state dir: %w", err)
55 }
56 if err := os.MkdirAll(s.LogDir(), 0o755); err != nil {
57 return fmt.Errorf("creating log dir: %w", err)
58 }
59 return nil
60}
61
62func (s *HomeDirStore) Save(st *State) error {
63 return saveState(s.StateDir(), st)
64}
65
66func (s *HomeDirStore) Load(id string) (*State, error) {
67 return loadState(s.StateDir(), id)
68}
69
70func (s *HomeDirStore) List() ([]*State, error) {
71 return listStates(s.StateDir())
72}
73
74// Scan lists the run states as List does, and names the records List
75// skips because they don't parse, as one caught while it is rewritten.
76func (s *HomeDirStore) Scan() ([]*State, []string, error) {
77 return scanStates(s.StateDir())
78}
79
80func (s *HomeDirStore) Delete(id string) error {
81 return deleteState(s.StateDir(), id)
82}
83
84// ListAllSessions scans ~/.klaus/sessions/ and returns a combined list of
85// all run states across all session directories.
86func ListAllSessions() ([]*State, error) {
87 dir, err := SessionsDir()
88 if err != nil {
89 return nil, err
90 }
91 return listAllSessionsIn(dir)
92}
93
94func listAllSessionsIn(sessionsDir string) ([]*State, error) {
95 entries, err := os.ReadDir(sessionsDir)
96 if err != nil {
97 if os.IsNotExist(err) {
98 return nil, nil
99 }
100 return nil, fmt.Errorf("reading sessions dir: %w", err)
101 }
102
103 var all []*State
104 for _, e := range entries {
105 if !e.IsDir() {
106 continue
107 }
108 store := NewHomeDirStoreFromPath(filepath.Join(sessionsDir, e.Name()))
109 states, err := store.List()
110 if err != nil {
111 continue
112 }
113 all = append(all, states...)
114 }
115 return all, nil
116}
117
118// FindMostRecentSession returns the ID of the most recent session directory
119// under the given sessions base dir. Session IDs embed timestamps
120// (session-YYYYMMDD-HHMM-XXXX), so lexicographic sort gives chronological order.
121func FindMostRecentSession(sessionsDir string) (string, error) {
122 entries, err := os.ReadDir(sessionsDir)
123 if err != nil {
124 if os.IsNotExist(err) {
125 return "", fmt.Errorf("no sessions found")
126 }
127 return "", fmt.Errorf("reading sessions dir: %w", err)
128 }
129
130 var latest string
131 for _, e := range entries {
132 if !e.IsDir() {
133 continue
134 }
135 name := e.Name()
136 if len(name) > len("session-") && name[:8] == "session-" {
137 latest = name
138 }
139 }
140 if latest == "" {
141 return "", fmt.Errorf("no sessions found")
142 }
143 return latest, nil
144}
145
146// FindStateInSessions searches across all session directories for a run with the given ID.
147// Returns the state and its store if found.
148func FindStateInSessions(homeDir string, id string) (*State, StateStore, error) {
149 sessionsDir := filepath.Join(homeDir, ".klaus", "sessions")
150 entries, err := os.ReadDir(sessionsDir)
151 if err != nil {
152 if os.IsNotExist(err) {
153 return nil, nil, fmt.Errorf("no run found with id: %s", id)
154 }
155 return nil, nil, fmt.Errorf("reading sessions dir: %w", err)
156 }
157
158 for _, e := range entries {
159 if !e.IsDir() {
160 continue
161 }
162 store := NewHomeDirStoreFromPath(filepath.Join(sessionsDir, e.Name()))
163 st, err := store.Load(id)
164 if err == nil {
165 return st, store, nil
166 }
167 }
168 return nil, nil, fmt.Errorf("no run found with id: %s", id)
169}