| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "flag" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | |
| 10 | "reasonix/internal/config" |
| 11 | "reasonix/internal/historycatalog" |
| 12 | ) |
| 13 | |
| 14 | func init() { |
| 15 | registerCatalogCommand(catalogCommand{ |
| 16 | name: "history", path: historycatalog.DefaultPath, reindex: reindexHistoryCatalog, |
| 17 | completionFlags: []cliCompletionFlag{ |
| 18 | completionFlag("--json", cliCompletionNoValue), |
| 19 | completionFlag("--dir", cliCompletionPathValue), |
| 20 | }, |
| 21 | }) |
| 22 | } |
| 23 | |
| 24 | func reindexHistoryCatalog(args []string) int { |
| 25 | fs := flag.NewFlagSet("catalogs reindex history", flag.ContinueOnError) |
| 26 | var dirs stringListFlag |
| 27 | jsonOut := fs.Bool("json", false, "print status as JSON") |
| 28 | fs.Var(&dirs, "dir", "session directory to index; repeat for multiple directories") |
| 29 | if code, ok := parseCommandFlags(fs, args); !ok { |
| 30 | return code |
| 31 | } |
| 32 | ctx := context.Background() |
| 33 | roots := []historycatalog.Root{} |
| 34 | if len(dirs) > 0 { |
| 35 | for _, dir := range dirs { |
| 36 | roots = append(roots, historycatalog.Root{Path: filepath.Clean(dir), Source: "global", Scope: "global"}) |
| 37 | } |
| 38 | } else { |
| 39 | for _, target := range defaultSessionCatalogTargets() { |
| 40 | roots = append(roots, historycatalog.Root{Path: target.Path, Source: target.Scope, Scope: target.Scope, WorkspaceRoot: target.WorkspaceRoot}) |
| 41 | roots = append(roots, historycatalog.Root{Path: filepath.Join(target.Path, "subagents"), Source: target.Scope, Scope: target.Scope, WorkspaceRoot: target.WorkspaceRoot, Subagents: true}) |
| 42 | } |
| 43 | roots = append(roots, historycatalog.Root{Path: config.ArchiveDir(), Source: "archive", Scope: "global", Archive: true}) |
| 44 | } |
| 45 | status, err := historycatalog.Rebuild(ctx, historycatalog.Options{}, roots) |
| 46 | if err != nil { |
| 47 | fmt.Fprintln(os.Stderr, "error:", err) |
| 48 | return 1 |
| 49 | } |
| 50 | return printCatalogStatus(status, *jsonOut) |
| 51 | } |
| 52 |