gunk-dev/the-valley

homedir_store.go

source6a6791929c36main's published tip history
4.2 KiB
1package run
2
3import (
4 "errors"
5 "fmt"
6 "os"
7 "path/filepath"
8 "strings"
9
10 "the-valley/flow/internal/location"
11)
12
13// HomeDirStore implements StateStore using a session's directory, under
14// either sessions root (location's SessionRoots). Everything of a session
15// stays in its directory.
16type HomeDirStore struct {
17 baseDir string // the session's directory
18}
19
20// OpenSession returns the store of the existing session id, under either
21// sessions root. An ID no root has is an error: looking a session up never
22// creates one.
23func OpenSession(id string) (*HomeDirStore, error) {
24 l, err := location.Host()
25 if err != nil {
26 return nil, err
27 }
28 dir, err := l.Session(id)
29 if err != nil {
30 return nil, err
31 }
32 return &HomeDirStore{baseDir: dir}, nil
33}
34
35// CreateSession returns the store of the new session id, in the directory
36// location allocates for it, which it creates.
37func CreateSession(id string) (*HomeDirStore, error) {
38 l, err := location.Host()
39 if err != nil {
40 return nil, err
41 }
42 dir, err := l.NewSession(id)
43 if err != nil {
44 return nil, err
45 }
46 s := &HomeDirStore{baseDir: dir}
47 if err := s.EnsureDirs(); err != nil {
48 return nil, err
49 }
50 return s, nil
51}
52
53// Sessions lists every session directory under either root, by ID, with a
54// location.ConflictError for each ID both roots have.
55func Sessions() ([]location.Session, error) {
56 l, err := location.Host()
57 if err != nil {
58 return nil, err
59 }
60 return l.Sessions()
61}
62
63// NewHomeDirStoreFromPath creates a HomeDirStore with an explicit base directory.
64// Useful for testing.
65func NewHomeDirStoreFromPath(baseDir string) *HomeDirStore {
66 return &HomeDirStore{baseDir: baseDir}
67}
68
69func (s *HomeDirStore) StateDir() string {
70 return filepath.Join(s.baseDir, "runs")
71}
72
73func (s *HomeDirStore) LogDir() string {
74 return filepath.Join(s.baseDir, "logs")
75}
76
77func (s *HomeDirStore) BaseDir() string {
78 return s.baseDir
79}
80
81func (s *HomeDirStore) EnsureDirs() error {
82 if err := os.MkdirAll(s.StateDir(), 0o755); err != nil {
83 return fmt.Errorf("creating state dir: %w", err)
84 }
85 if err := os.MkdirAll(s.LogDir(), 0o755); err != nil {
86 return fmt.Errorf("creating log dir: %w", err)
87 }
88 return nil
89}
90
91func (s *HomeDirStore) Save(st *State) error {
92 return saveState(s.StateDir(), st)
93}
94
95func (s *HomeDirStore) Load(id string) (*State, error) {
96 return loadState(s.StateDir(), id)
97}
98
99func (s *HomeDirStore) List() ([]*State, error) {
100 return listStates(s.StateDir())
101}
102
103// Scan lists the run states as List does, and names the records List
104// skips because they don't parse, as one caught while it is rewritten.
105func (s *HomeDirStore) Scan() ([]*State, []string, error) {
106 states, unparsed, _, err := scanStates(s.StateDir())
107 return states, unparsed, err
108}
109
110// Audit is Scan for a reader that must account for every run: it also
111// returns the read errors of the records Scan skips because they can't be
112// read.
113func (s *HomeDirStore) Audit() (states []*State, unparsed []string, unreadable []error, err error) {
114 return scanStates(s.StateDir())
115}
116
117func (s *HomeDirStore) Delete(id string) error {
118 return deleteState(s.StateDir(), id)
119}
120
121// ListAllSessions returns the run states of every session, under either
122// root. A session ID both roots have is left out, and its error returned
123// with the rest.
124func ListAllSessions() ([]*State, error) {
125 sessions, err := Sessions()
126 var all []*State
127 for _, sess := range sessions {
128 states, listErr := NewHomeDirStoreFromPath(sess.Dir).List()
129 if listErr != nil {
130 continue
131 }
132 all = append(all, states...)
133 }
134 return all, err
135}
136
137// ErrNoSessions is the absence of any session, under either root.
138var ErrNoSessions = errors.New("no sessions found")
139
140// MostRecentSession returns the ID of the most recent session, under
141// either root. Session IDs embed timestamps (session-YYYYMMDD-HHMM-XXXX),
142// so lexicographic order is chronological. With none it returns
143// ErrNoSessions; any other error, such as a session ID both roots have, is
144// one it could not look past.
145func MostRecentSession() (string, error) {
146 sessions, err := Sessions()
147 if err != nil {
148 return "", err
149 }
150 var latest string
151 for _, sess := range sessions {
152 if strings.HasPrefix(sess.ID, "session-") && len(sess.ID) > len("session-") && sess.ID > latest {
153 latest = sess.ID
154 }
155 }
156 if latest == "" {
157 return "", ErrNoSessions
158 }
159 return latest, nil
160}