readJSONL reads a file of JSON objects, one per line. A missing file has none.
(path string)
| 249 | // readJSONL reads a file of JSON objects, one per line. A missing file has |
| 250 | // none. |
| 251 | func readJSONL[T any](path string) ([]T, error) { |
| 252 | f, err := os.Open(path) |
| 253 | if errors.Is(err, fs.ErrNotExist) { |
| 254 | return nil, nil |
| 255 | } |
| 256 | if err != nil { |
| 257 | return nil, err |
| 258 | } |
| 259 | defer f.Close() |
| 260 | |
| 261 | var items []T |
| 262 | decoder := json.NewDecoder(f) |
| 263 | for { |
| 264 | var item T |
| 265 | if err := decoder.Decode(&item); errors.Is(err, io.EOF) { |
| 266 | return items, nil |
| 267 | } else if err != nil { |
| 268 | return nil, fmt.Errorf("reading %s: %w", path, err) |
| 269 | } |
| 270 | items = append(items, item) |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | func (s *state) reset() error { |
| 275 | for _, p := range []string{s.responsesPath(), s.logPath(), s.runsPath()} { |