|
| 1 | +package chatarchivecmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/CodeMonkeyCybersecurity/eos/pkg/chatarchive" |
| 9 | + "github.com/CodeMonkeyCybersecurity/eos/pkg/eos_io" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "github.com/uptrace/opentelemetry-go-extra/otelzap" |
| 12 | + "go.uber.org/zap" |
| 13 | +) |
| 14 | + |
| 15 | +func BindFlags(cmd *cobra.Command) { |
| 16 | + cmd.Flags().StringSlice("source", chatarchive.DefaultSources(), "Source directories to scan") |
| 17 | + cmd.Flags().String("dest", chatarchive.DefaultDest(), "Destination archive directory") |
| 18 | + cmd.Flags().Bool("dry-run", false, "Show what would be archived without copying files") |
| 19 | +} |
| 20 | + |
| 21 | +func Run(rc *eos_io.RuntimeContext, cmd *cobra.Command) error { |
| 22 | + sources, _ := cmd.Flags().GetStringSlice("source") |
| 23 | + dest, _ := cmd.Flags().GetString("dest") |
| 24 | + dryRun, _ := cmd.Flags().GetBool("dry-run") |
| 25 | + |
| 26 | + result, err := chatarchive.Archive(rc, chatarchive.Options{ |
| 27 | + Sources: sources, |
| 28 | + Dest: dest, |
| 29 | + DryRun: dryRun, |
| 30 | + }) |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + |
| 35 | + logger := otelzap.Ctx(rc.Ctx) |
| 36 | + logger.Info("terminal prompt:", zap.String("output", formatSummary(result, dryRun))) |
| 37 | + for _, failure := range result.Failures { |
| 38 | + logger.Warn("Chat archive file failure", |
| 39 | + zap.String("path", failure.Path), |
| 40 | + zap.String("stage", failure.Stage), |
| 41 | + zap.String("reason", failure.Reason)) |
| 42 | + } |
| 43 | + |
| 44 | + return nil |
| 45 | +} |
| 46 | + |
| 47 | +func formatSummary(result *chatarchive.Result, dryRun bool) string { |
| 48 | + lines := []string{ |
| 49 | + statusLine(dryRun), |
| 50 | + fmt.Sprintf("Unique files: %d", result.UniqueFiles), |
| 51 | + fmt.Sprintf("Duplicates in this run: %d", result.Duplicates), |
| 52 | + fmt.Sprintf("Already archived: %d", result.Skipped), |
| 53 | + fmt.Sprintf("Empty files ignored: %d", result.EmptyFiles), |
| 54 | + fmt.Sprintf("File failures: %d", result.FailureCount), |
| 55 | + fmt.Sprintf("Duration: %s", result.Duration.Round(10*time.Millisecond)), |
| 56 | + } |
| 57 | + |
| 58 | + if result.ManifestPath != "" { |
| 59 | + lines = append(lines, fmt.Sprintf("Manifest: %s", result.ManifestPath)) |
| 60 | + } |
| 61 | + if result.RecoveredManifestPath != "" { |
| 62 | + lines = append(lines, fmt.Sprintf("Recovered corrupt manifest: %s", result.RecoveredManifestPath)) |
| 63 | + } |
| 64 | + if result.FailureCount > len(result.Failures) { |
| 65 | + lines = append(lines, fmt.Sprintf("Additional failures not shown: %d", result.FailureCount-len(result.Failures))) |
| 66 | + } |
| 67 | + |
| 68 | + return strings.Join(lines, "\n") |
| 69 | +} |
| 70 | + |
| 71 | +func statusLine(dryRun bool) string { |
| 72 | + if dryRun { |
| 73 | + return "Dry run complete." |
| 74 | + } |
| 75 | + return "Archive complete." |
| 76 | +} |
0 commit comments