|
| 1 | +package chatarchivecmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/CodeMonkeyCybersecurity/eos/pkg/chatarchive" |
| 10 | + "github.com/CodeMonkeyCybersecurity/eos/pkg/eos_io" |
| 11 | + "github.com/spf13/cobra" |
| 12 | + "github.com/uptrace/opentelemetry-go-extra/otelzap" |
| 13 | + "go.uber.org/zap" |
| 14 | +) |
| 15 | + |
| 16 | +func BindFlags(cmd *cobra.Command) { |
| 17 | + cmd.Flags().StringSlice("source", chatarchive.DefaultSources(), "Source directories to scan") |
| 18 | + cmd.Flags().String("dest", chatarchive.DefaultDest(), "Destination archive directory") |
| 19 | + cmd.Flags().StringSlice("exclude", nil, "Path substrings to exclude from discovery (e.g. --exclude conversation-api)") |
| 20 | + cmd.Flags().Bool("dry-run", false, "Show what would be archived without copying files") |
| 21 | +} |
| 22 | + |
| 23 | +func Run(rc *eos_io.RuntimeContext, cmd *cobra.Command) error { |
| 24 | + sources, _ := cmd.Flags().GetStringSlice("source") |
| 25 | + dest, _ := cmd.Flags().GetString("dest") |
| 26 | + excludes, _ := cmd.Flags().GetStringSlice("exclude") |
| 27 | + dryRun, _ := cmd.Flags().GetBool("dry-run") |
| 28 | + |
| 29 | + result, err := chatarchive.Archive(rc, chatarchive.Options{ |
| 30 | + Sources: sources, |
| 31 | + Dest: dest, |
| 32 | + Excludes: excludes, |
| 33 | + DryRun: dryRun, |
| 34 | + }) |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + |
| 39 | + logger := otelzap.Ctx(rc.Ctx) |
| 40 | + writeSummary(cmd.OutOrStdout(), result, dryRun, logger) |
| 41 | + logger.Info("Chat archive summary", |
| 42 | + zap.Int("sources_requested", result.SourcesRequested), |
| 43 | + zap.Int("sources_scanned", result.SourcesScanned), |
| 44 | + zap.Int("sources_missing", len(result.MissingSources)), |
| 45 | + zap.Int("skipped_symlinks", result.SkippedSymlinks), |
| 46 | + zap.Int("unreadable_entries", result.UnreadableEntries), |
| 47 | + zap.Int("unique_files", result.UniqueFiles), |
| 48 | + zap.Int("duplicates", result.Duplicates), |
| 49 | + zap.Int("already_archived", result.Skipped), |
| 50 | + zap.Int("empty_files", result.EmptyFiles), |
| 51 | + zap.Int("failures", result.FailureCount), |
| 52 | + zap.Duration("duration", result.Duration), |
| 53 | + zap.Bool("dry_run", dryRun)) |
| 54 | + for _, failure := range result.Failures { |
| 55 | + logger.Warn("Chat archive file failure", |
| 56 | + zap.String("path", failure.Path), |
| 57 | + zap.String("stage", failure.Stage), |
| 58 | + zap.String("reason", failure.Reason)) |
| 59 | + } |
| 60 | + |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func formatSummary(result *chatarchive.Result, dryRun bool) string { |
| 65 | + lines := []string{ |
| 66 | + statusLine(dryRun), |
| 67 | + fmt.Sprintf("Sources scanned: %d/%d", result.SourcesScanned, result.SourcesRequested), |
| 68 | + fmt.Sprintf("Unique files: %d", result.UniqueFiles), |
| 69 | + fmt.Sprintf("Duplicates in this run: %d", result.Duplicates), |
| 70 | + fmt.Sprintf("Already archived: %d", result.Skipped), |
| 71 | + fmt.Sprintf("Empty files ignored: %d", result.EmptyFiles), |
| 72 | + fmt.Sprintf("File failures: %d", result.FailureCount), |
| 73 | + fmt.Sprintf("Unreadable entries skipped: %d", result.UnreadableEntries), |
| 74 | + fmt.Sprintf("Symlinks skipped: %d", result.SkippedSymlinks), |
| 75 | + fmt.Sprintf("Duration: %s", result.Duration.Round(10*time.Millisecond)), |
| 76 | + } |
| 77 | + |
| 78 | + if result.ManifestPath != "" { |
| 79 | + lines = append(lines, fmt.Sprintf("Manifest: %s", result.ManifestPath)) |
| 80 | + } |
| 81 | + if result.RecoveredManifestPath != "" { |
| 82 | + lines = append(lines, fmt.Sprintf("Recovered corrupt manifest: %s", result.RecoveredManifestPath)) |
| 83 | + } |
| 84 | + if len(result.MissingSources) > 0 { |
| 85 | + lines = append(lines, fmt.Sprintf("Unavailable sources: %s", strings.Join(result.MissingSources, ", "))) |
| 86 | + } |
| 87 | + if result.FailureCount > len(result.Failures) { |
| 88 | + lines = append(lines, fmt.Sprintf("Additional failures not shown: %d", result.FailureCount-len(result.Failures))) |
| 89 | + } |
| 90 | + |
| 91 | + return strings.Join(lines, "\n") |
| 92 | +} |
| 93 | + |
| 94 | +func statusLine(dryRun bool) string { |
| 95 | + if dryRun { |
| 96 | + return "Dry run complete." |
| 97 | + } |
| 98 | + return "Archive complete." |
| 99 | +} |
| 100 | + |
| 101 | +func writeSummary(w io.Writer, result *chatarchive.Result, dryRun bool, logger otelzap.LoggerWithCtx) { |
| 102 | + if _, err := fmt.Fprintln(w, formatSummary(result, dryRun)); err != nil { |
| 103 | + logger.Warn("Failed to write chat archive summary", zap.Error(err)) |
| 104 | + } |
| 105 | +} |
0 commit comments