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