| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "flag" |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "time" |
| 10 | |
| 11 | "reasonix/internal/projectiondb" |
| 12 | "reasonix/internal/sessioncatalog" |
| 13 | ) |
| 14 | |
| 15 | type catalogInspection struct { |
| 16 | Name string `json:"name"` |
| 17 | Info projectiondb.Inspection `json:"info"` |
| 18 | } |
| 19 | |
| 20 | type catalogCommand struct { |
| 21 | name string |
| 22 | path func() string |
| 23 | reindex func([]string) int |
| 24 | completionFlags []cliCompletionFlag |
| 25 | } |
| 26 | |
| 27 | var catalogCommands []catalogCommand |
| 28 | |
| 29 | func registerCatalogCommand(command catalogCommand) { |
| 30 | catalogCommands = append(catalogCommands, command) |
| 31 | } |
| 32 | |
| 33 | func runSessionOrCatalogCommand(command string, args []string) int { |
| 34 | configureCLIThemeFromConfig() |
| 35 | if command != "catalogs" { |
| 36 | return sessionOrSessionsCommand(command, args) |
| 37 | } |
| 38 | return catalogsCommand(args) |
| 39 | } |
| 40 | |
| 41 | func doctorCatalogsCommand(args []string) int { |
| 42 | fs := flag.NewFlagSet("doctor catalogs", flag.ContinueOnError) |
| 43 | jsonOut := fs.Bool("json", false, "print catalog diagnostics as JSON") |
| 44 | if code, ok := parseCommandFlags(fs, args); !ok { |
| 45 | return code |
| 46 | } |
| 47 | if fs.NArg() != 0 { |
| 48 | fmt.Fprintln(os.Stderr, "usage: reasonix doctor catalogs [--json]") |
| 49 | return 2 |
| 50 | } |
| 51 | ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 52 | defer cancel() |
| 53 | items := []catalogInspection{ |
| 54 | {Name: "sessions", Info: projectiondb.Inspect(ctx, sessioncatalog.DefaultPath())}, |
| 55 | } |
| 56 | for _, command := range catalogCommands { |
| 57 | items = append(items, catalogInspection{Name: command.name, Info: projectiondb.Inspect(ctx, command.path())}) |
| 58 | } |
| 59 | if *jsonOut { |
| 60 | enc := json.NewEncoder(os.Stdout) |
| 61 | enc.SetIndent("", " ") |
| 62 | if err := enc.Encode(items); err != nil { |
| 63 | fmt.Fprintln(os.Stderr, err) |
| 64 | return 1 |
| 65 | } |
| 66 | return 0 |
| 67 | } |
| 68 | fmt.Println("Reasonix disposable catalogs") |
| 69 | for _, item := range items { |
| 70 | state := "missing" |
| 71 | if item.Info.Exists && item.Info.Error == "" { |
| 72 | state = item.Info.Integrity |
| 73 | } else if item.Info.Error != "" { |
| 74 | state = "error: " + item.Info.Error |
| 75 | } |
| 76 | fmt.Printf(" %-8s %-12s schema=%d size=%d path=%s\n", item.Name, state, item.Info.Schema, item.Info.Size, item.Info.Path) |
| 77 | } |
| 78 | return 0 |
| 79 | } |
| 80 | |
| 81 | func catalogsCommand(args []string) int { |
| 82 | if len(args) < 2 || args[0] != "reindex" { |
| 83 | fmt.Fprintln(os.Stderr, "usage: reasonix catalogs reindex <catalog> [options]") |
| 84 | return 2 |
| 85 | } |
| 86 | for _, command := range catalogCommands { |
| 87 | if args[1] == command.name { |
| 88 | return command.reindex(args[2:]) |
| 89 | } |
| 90 | } |
| 91 | fmt.Fprintln(os.Stderr, "unknown catalog:", args[1]) |
| 92 | return 2 |
| 93 | } |
| 94 | |
| 95 | func printCatalogStatus(status any, jsonOut bool) int { |
| 96 | if jsonOut { |
| 97 | enc := json.NewEncoder(os.Stdout) |
| 98 | enc.SetIndent("", " ") |
| 99 | if err := enc.Encode(status); err != nil { |
| 100 | fmt.Fprintln(os.Stderr, err) |
| 101 | return 1 |
| 102 | } |
| 103 | return 0 |
| 104 | } |
| 105 | b, _ := json.Marshal(status) |
| 106 | fmt.Println(string(b)) |
| 107 | return 0 |
| 108 | } |
| 109 |