gunk-dev/the-valley

homedir_store.go

source68d12192bbd3main's published tip history
4.1 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
74func (s *HomeDirStore) Delete(id string) error {
75 return deleteState(s.StateDir(), id)
76}
77
78// ListAllSessions scans ~/.klaus/sessions/ and returns a combined list of
79// all run states across all session directories.
80func ListAllSessions() ([]*State, error) {
81 dir, err := SessionsDir()
82 if err != nil {
83 return nil, err
84 }
85 return listAllSessionsIn(dir)
86}
87
88func listAllSessionsIn(sessionsDir string) ([]*State, error) {
89 entries, err := os.ReadDir(sessionsDir)
90 if err != nil {
91 if os.IsNotExist(err) {
92 return nil, nil
93 }
94 return nil, fmt.Errorf("reading sessions dir: %w", err)
95 }
96
97 var all []*State
98 for _, e := range entries {
99 if !e.IsDir() {
100 continue
101 }
102 store := NewHomeDirStoreFromPath(filepath.Join(sessionsDir, e.Name()))
103 states, err := store.List()
104 if err != nil {
105 continue
106 }
107 all = append(all, states...)
108 }
109 return all, nil
110}
111
112// FindMostRecentSession returns the ID of the most recent session directory
113// under the given sessions base dir. Session IDs embed timestamps
114// (session-YYYYMMDD-HHMM-XXXX), so lexicographic sort gives chronological order.
115func FindMostRecentSession(sessionsDir string) (string, error) {
116 entries, err := os.ReadDir(sessionsDir)
117 if err != nil {
118 if os.IsNotExist(err) {
119 return "", fmt.Errorf("no sessions found")
120 }
121 return "", fmt.Errorf("reading sessions dir: %w", err)
122 }
123
124 var latest string
125 for _, e := range entries {
126 if !e.IsDir() {
127 continue
128 }
129 name := e.Name()
130 if len(name) > len("session-") && name[:8] == "session-" {
131 latest = name
132 }
133 }
134 if latest == "" {
135 return "", fmt.Errorf("no sessions found")
136 }
137 return latest, nil
138}
139
140// FindStateInSessions searches across all session directories for a run with the given ID.
141// Returns the state and its store if found.
142func FindStateInSessions(homeDir string, id string) (*State, StateStore, error) {
143 sessionsDir := filepath.Join(homeDir, ".klaus", "sessions")
144 entries, err := os.ReadDir(sessionsDir)
145 if err != nil {
146 if os.IsNotExist(err) {
147 return nil, nil, fmt.Errorf("no run found with id: %s", id)
148 }
149 return nil, nil, fmt.Errorf("reading sessions dir: %w", err)
150 }
151
152 for _, e := range entries {
153 if !e.IsDir() {
154 continue
155 }
156 store := NewHomeDirStoreFromPath(filepath.Join(sessionsDir, e.Name()))
157 st, err := store.Load(id)
158 if err == nil {
159 return st, store, nil
160 }
161 }
162 return nil, nil, fmt.Errorf("no run found with id: %s", id)
163}