LoadState reads the state file for a source. Returns empty state if file doesn't exist.
(dir, sourceName string)
| 30 | |
| 31 | // LoadState reads the state file for a source. Returns empty state if file doesn't exist. |
| 32 | func LoadState(dir, sourceName string) (*SyncState, error) { |
| 33 | path := stateFilePath(dir, sourceName) |
| 34 | data, err := os.ReadFile(path) |
| 35 | if os.IsNotExist(err) { |
| 36 | return &SyncState{ |
| 37 | Source: sourceName, |
| 38 | Tasks: make(map[string]TaskState), |
| 39 | }, nil |
| 40 | } |
| 41 | if err != nil { |
| 42 | return nil, fmt.Errorf("failed to read state file: %w", err) |
| 43 | } |
| 44 | |
| 45 | var state SyncState |
| 46 | if err := yaml.Unmarshal(data, &state); err != nil { |
| 47 | return nil, fmt.Errorf("failed to parse state file: %w", err) |
| 48 | } |
| 49 | |
| 50 | if state.Tasks == nil { |
| 51 | state.Tasks = make(map[string]TaskState) |
| 52 | } |
| 53 | |
| 54 | return &state, nil |
| 55 | } |
| 56 | |
| 57 | // SaveState writes the state file for a source. |
| 58 | func SaveState(dir, sourceName string, state *SyncState) error { |