SaveState writes the state file for a source.
(dir, sourceName string, state *SyncState)
| 56 | |
| 57 | // SaveState writes the state file for a source. |
| 58 | func SaveState(dir, sourceName string, state *SyncState) error { |
| 59 | stateDir := filepath.Join(dir, stateSubDir) |
| 60 | if err := os.MkdirAll(stateDir, 0755); err != nil { |
| 61 | return fmt.Errorf("failed to create state directory: %w", err) |
| 62 | } |
| 63 | |
| 64 | data, err := yaml.Marshal(state) |
| 65 | if err != nil { |
| 66 | return fmt.Errorf("failed to marshal state: %w", err) |
| 67 | } |
| 68 | |
| 69 | path := stateFilePath(dir, sourceName) |
| 70 | if err := os.WriteFile(path, data, 0644); err != nil { |
| 71 | return fmt.Errorf("failed to write state file: %w", err) |
| 72 | } |
| 73 | |
| 74 | return nil |
| 75 | } |
| 76 | |
| 77 | func stateFilePath(dir, sourceName string) string { |
| 78 | return filepath.Join(dir, stateSubDir, sourceName+".yaml") |