|
| 1 | +// cmd/backup/chats.go |
| 2 | +// Command orchestration for AI chat data backup. |
| 3 | +// Business logic lives in pkg/chats/. |
| 4 | + |
| 5 | +package backup |
| 6 | + |
| 7 | +import ( |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/CodeMonkeyCybersecurity/eos/pkg/backup" |
| 14 | + "github.com/CodeMonkeyCybersecurity/eos/pkg/chats" |
| 15 | + eos "github.com/CodeMonkeyCybersecurity/eos/pkg/eos_cli" |
| 16 | + "github.com/CodeMonkeyCybersecurity/eos/pkg/eos_err" |
| 17 | + "github.com/CodeMonkeyCybersecurity/eos/pkg/eos_io" |
| 18 | + "github.com/spf13/cobra" |
| 19 | + "github.com/uptrace/opentelemetry-go-extra/otelzap" |
| 20 | + "go.uber.org/zap" |
| 21 | +) |
| 22 | + |
| 23 | +var chatsCmd = &cobra.Command{ |
| 24 | + Use: "chats", |
| 25 | + Short: "Back up AI coding assistant chat data for analysis", |
| 26 | + Long: `Back up conversation history from AI coding assistants to a restic repository. |
| 27 | +
|
| 28 | +Automatically discovers data from supported tools: |
| 29 | + - Claude Code (~/.claude/) |
| 30 | + - OpenAI Codex (~/.codex/) |
| 31 | + - Windsurf/Codeium (~/.codeium/) |
| 32 | + - Cursor (~/.cursor/) |
| 33 | + - Continue.dev (~/.continue/) |
| 34 | + - GitHub Copilot (~/.config/github-copilot/) |
| 35 | + - Gemini (~/.gemini/) |
| 36 | + - Aider (~/.aider/) |
| 37 | +
|
| 38 | +Data is backed up with tags for easy filtering and restore. |
| 39 | +Requires an existing restic repository (see: eos backup create repository). |
| 40 | +
|
| 41 | +Examples: |
| 42 | + # Back up all discovered chat data |
| 43 | + eos backup chats |
| 44 | +
|
| 45 | + # Dry run to see what would be backed up |
| 46 | + eos backup chats --dry-run |
| 47 | +
|
| 48 | + # Back up only specific tools |
| 49 | + eos backup chats --tool claude-code --tool codex |
| 50 | +
|
| 51 | + # Back up a specific user's data |
| 52 | + eos backup chats --user henry |
| 53 | +
|
| 54 | + # List chat backup snapshots |
| 55 | + eos backup list snapshots --tag chat-backup`, |
| 56 | + |
| 57 | + RunE: eos.Wrap(func(rc *eos_io.RuntimeContext, cmd *cobra.Command, args []string) error { |
| 58 | + logger := otelzap.Ctx(rc.Ctx) |
| 59 | + |
| 60 | + // Parse flags |
| 61 | + dryRun, _ := cmd.Flags().GetBool("dry-run") |
| 62 | + targetUser, _ := cmd.Flags().GetString("user") |
| 63 | + toolFilter, _ := cmd.Flags().GetStringSlice("tool") |
| 64 | + |
| 65 | + // Resolve repository |
| 66 | + repoName, repoConfig, err := resolveChatsBackupRepository(rc) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + logger.Info("Using repository for chat backup", |
| 72 | + zap.String("repository", repoName), |
| 73 | + zap.String("backend", repoConfig.Backend), |
| 74 | + zap.String("url", repoConfig.URL)) |
| 75 | + |
| 76 | + // Delegate to pkg/chats business logic |
| 77 | + config := &chats.BackupConfig{ |
| 78 | + RepoName: repoName, |
| 79 | + User: targetUser, |
| 80 | + Tools: toolFilter, |
| 81 | + DryRun: dryRun, |
| 82 | + } |
| 83 | + |
| 84 | + result, err := chats.RunBackup(rc, config) |
| 85 | + if err != nil { |
| 86 | + if errors.Is(err, backup.ErrResticNotInstalled) { |
| 87 | + logger.Info("terminal prompt:", zap.String("output", |
| 88 | + "Restic is not installed. Install with: sudo apt-get install restic")) |
| 89 | + return eos_err.NewExpectedError(rc.Ctx, eos_err.DependencyError("restic", "back up chat data", err)) |
| 90 | + } |
| 91 | + if errors.Is(err, backup.ErrRepositoryNotInitialized) { |
| 92 | + logger.Info("terminal prompt:", zap.String("output", |
| 93 | + "Restic repository not initialized. Create one with:\n"+ |
| 94 | + " eos backup create repository local --path /var/lib/eos/backups")) |
| 95 | + return eos_err.NewExpectedError(rc.Ctx, err) |
| 96 | + } |
| 97 | + return err |
| 98 | + } |
| 99 | + |
| 100 | + // Report results |
| 101 | + logger.Info("terminal prompt:", zap.String("output", string(result.Output))) |
| 102 | + |
| 103 | + action := "Backed up" |
| 104 | + if dryRun { |
| 105 | + action = "Would back up" |
| 106 | + } |
| 107 | + logger.Info("terminal prompt:", zap.String("output", |
| 108 | + fmt.Sprintf("\n%s chat data from: %s", action, strings.Join(result.ToolsBacked, ", ")))) |
| 109 | + logger.Info("terminal prompt:", zap.String("output", |
| 110 | + fmt.Sprintf("Repository: %s (%s)", repoName, repoConfig.URL))) |
| 111 | + logger.Info("terminal prompt:", zap.String("output", |
| 112 | + fmt.Sprintf("Duration: %s", result.Duration.Round(time.Millisecond)))) |
| 113 | + logger.Info("terminal prompt:", zap.String("output", |
| 114 | + "Restore: eos backup restore <snapshot-id> --target /tmp/chats")) |
| 115 | + logger.Info("terminal prompt:", zap.String("output", |
| 116 | + "List: eos backup list snapshots --tag chat-backup")) |
| 117 | + |
| 118 | + return nil |
| 119 | + }), |
| 120 | +} |
| 121 | + |
| 122 | +// resolveChatsBackupRepository finds a repository to use for chat backups. |
| 123 | +// Reuses the same resolution logic as quick backups. |
| 124 | +func resolveChatsBackupRepository(rc *eos_io.RuntimeContext) (string, backup.Repository, error) { |
| 125 | + logger := otelzap.Ctx(rc.Ctx) |
| 126 | + |
| 127 | + config, err := backup.LoadConfig(rc) |
| 128 | + if err != nil { |
| 129 | + return "", backup.Repository{}, fmt.Errorf("loading backup configuration: %w\n"+ |
| 130 | + "Create a repository first: eos backup create repository local --path /var/lib/eos/backups", err) |
| 131 | + } |
| 132 | + |
| 133 | + // Try default repository |
| 134 | + if repoName, err := backup.ResolveRepositoryNameFromConfig(config, ""); err == nil { |
| 135 | + repo := config.Repositories[repoName] |
| 136 | + logger.Info("Using default repository for chat backup", |
| 137 | + zap.String("repository", repoName)) |
| 138 | + return repoName, repo, nil |
| 139 | + } |
| 140 | + |
| 141 | + // If only one repository exists, use it |
| 142 | + if len(config.Repositories) == 1 { |
| 143 | + for name := range config.Repositories { |
| 144 | + repo := config.Repositories[name] |
| 145 | + logger.Info("Using sole configured repository for chat backup", |
| 146 | + zap.String("repository", name)) |
| 147 | + return name, repo, nil |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + if len(config.Repositories) == 0 { |
| 152 | + return "", backup.Repository{}, fmt.Errorf("no repositories configured\n" + |
| 153 | + "Create one first: eos backup create repository local --path /var/lib/eos/backups") |
| 154 | + } |
| 155 | + |
| 156 | + // Multiple repos, no default |
| 157 | + repoNames := make([]string, 0, len(config.Repositories)) |
| 158 | + for name := range config.Repositories { |
| 159 | + repoNames = append(repoNames, name) |
| 160 | + } |
| 161 | + return "", backup.Repository{}, eos_err.NewExpectedError(rc.Ctx, fmt.Errorf( |
| 162 | + "multiple repositories configured (%s) but no default set; update %s", |
| 163 | + strings.Join(repoNames, ", "), backup.ConfigFile)) |
| 164 | +} |
| 165 | + |
| 166 | +func init() { |
| 167 | + BackupCmd.AddCommand(chatsCmd) |
| 168 | + |
| 169 | + chatsCmd.Flags().Bool("dry-run", false, "Show what would be backed up without creating backup") |
| 170 | + chatsCmd.Flags().String("user", "", "Target user (default: auto-detect from SUDO_USER)") |
| 171 | + chatsCmd.Flags().StringSlice("tool", nil, |
| 172 | + fmt.Sprintf("Limit to specific tools (available: %s)", strings.Join(chats.AvailableToolNames(), ", "))) |
| 173 | +} |
0 commit comments