Download this file
| 1 | package docs |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "log/slog" |
| 7 | "os" |
| 8 | |
| 9 | "amadan.net/rastrillo/rastrillo/db" |
| 10 | "amadan.net/rastrillo/rastrillo/migrate" |
| 11 | ) |
| 12 | |
| 13 | type ContentMaintenanceConfig struct { |
| 14 | DBPath string |
| 15 | ContentKey string |
| 16 | ExpectedFingerprint string |
| 17 | Scrub bool |
| 18 | Logger *slog.Logger |
| 19 | } |
| 20 | |
| 21 | func MaintainContent(ctx context.Context, cfg ContentMaintenanceConfig) error { |
| 22 | c, err := newContentCipher(cfg.ContentKey, "production") |
| 23 | if err != nil { |
| 24 | return err |
| 25 | } |
| 26 | if cfg.ExpectedFingerprint == "" || cfg.ExpectedFingerprint != c.fingerprint { |
| 27 | return errContentKey |
| 28 | } |
| 29 | info, err := os.Stat(cfg.DBPath) |
| 30 | if err != nil { |
| 31 | return err |
| 32 | } |
| 33 | if !info.Mode().IsRegular() { |
| 34 | return errContentState |
| 35 | } |
| 36 | d, err := db.Open(cfg.DBPath, cfg.Logger) |
| 37 | if err != nil { |
| 38 | return err |
| 39 | } |
| 40 | if _, err := migrate.Apply(ctx, d, BootSchema); err != nil { |
| 41 | return errors.Join(err, d.Close()) |
| 42 | } |
| 43 | if err := convertContent(ctx, d.G, c); err != nil { |
| 44 | return errors.Join(err, d.Close()) |
| 45 | } |
| 46 | if cfg.Scrub { |
| 47 | if err := scrubContent(ctx, d, c); err != nil { |
| 48 | return errors.Join(err, d.Close()) |
| 49 | } |
| 50 | } |
| 51 | return d.Close() |
| 52 | } |
| 53 | |