|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "log/slog" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/allisson/secrets/internal/app" |
| 11 | + "github.com/allisson/secrets/internal/config" |
| 12 | +) |
| 13 | + |
| 14 | +// RunCleanAuditLogs deletes audit logs older than the specified number of days. |
| 15 | +// Supports dry-run mode to preview deletion count and both text/JSON output formats. |
| 16 | +// |
| 17 | +// Requirements: Database must be migrated and accessible. |
| 18 | +func RunCleanAuditLogs(ctx context.Context, days int, dryRun bool, format string) error { |
| 19 | + // Validate days parameter |
| 20 | + if days < 0 { |
| 21 | + return fmt.Errorf("days must be a positive number, got: %d", days) |
| 22 | + } |
| 23 | + |
| 24 | + // Load configuration |
| 25 | + cfg := config.Load() |
| 26 | + |
| 27 | + // Create DI container |
| 28 | + container := app.NewContainer(cfg) |
| 29 | + |
| 30 | + // Get logger from container |
| 31 | + logger := container.Logger() |
| 32 | + logger.Info("cleaning audit logs", |
| 33 | + slog.Int("days", days), |
| 34 | + slog.Bool("dry_run", dryRun), |
| 35 | + ) |
| 36 | + |
| 37 | + // Ensure cleanup on exit |
| 38 | + defer closeContainer(container, logger) |
| 39 | + |
| 40 | + // Get audit log use case from container |
| 41 | + auditLogUseCase, err := container.AuditLogUseCase() |
| 42 | + if err != nil { |
| 43 | + return fmt.Errorf("failed to initialize audit log use case: %w", err) |
| 44 | + } |
| 45 | + |
| 46 | + // Execute deletion or count operation |
| 47 | + count, err := auditLogUseCase.DeleteOlderThan(ctx, days, dryRun) |
| 48 | + if err != nil { |
| 49 | + return fmt.Errorf("failed to delete audit logs: %w", err) |
| 50 | + } |
| 51 | + |
| 52 | + // Output result based on format |
| 53 | + if format == "json" { |
| 54 | + outputCleanJSON(count, days, dryRun) |
| 55 | + } else { |
| 56 | + outputCleanText(count, days, dryRun) |
| 57 | + } |
| 58 | + |
| 59 | + logger.Info("cleanup completed", |
| 60 | + slog.Int64("count", count), |
| 61 | + slog.Int("days", days), |
| 62 | + slog.Bool("dry_run", dryRun), |
| 63 | + ) |
| 64 | + |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +// outputCleanText outputs the result in human-readable text format. |
| 69 | +func outputCleanText(count int64, days int, dryRun bool) { |
| 70 | + if dryRun { |
| 71 | + fmt.Printf("Dry-run mode: Would delete %d audit log(s) older than %d day(s)\n", count, days) |
| 72 | + } else { |
| 73 | + fmt.Printf("Successfully deleted %d audit log(s) older than %d day(s)\n", count, days) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// outputCleanJSON outputs the result in JSON format for machine consumption. |
| 78 | +func outputCleanJSON(count int64, days int, dryRun bool) { |
| 79 | + result := map[string]interface{}{ |
| 80 | + "count": count, |
| 81 | + "days": days, |
| 82 | + "dry_run": dryRun, |
| 83 | + } |
| 84 | + |
| 85 | + jsonBytes, err := json.MarshalIndent(result, "", " ") |
| 86 | + if err != nil { |
| 87 | + fmt.Fprintf(os.Stderr, "failed to marshal JSON: %v\n", err) |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + fmt.Println(string(jsonBytes)) |
| 92 | +} |
0 commit comments