|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + digestproj "sift/internal/digest" |
| 11 | +) |
| 12 | + |
| 13 | +type digestOptions struct { |
| 14 | + StateDir string |
| 15 | + OutputDir string |
| 16 | + Scope string |
| 17 | + Window string |
| 18 | + Format string |
| 19 | +} |
| 20 | + |
| 21 | +func runDigest(ctx context.Context, args []string) error { |
| 22 | + opts, err := parseDigestOptions(args) |
| 23 | + if err != nil { |
| 24 | + return &commandError{ |
| 25 | + Code: exitInvalidArguments, |
| 26 | + Err: err, |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + store, err := openStore(ctx, opts.StateDir) |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + defer store.Close() |
| 35 | + |
| 36 | + records, err := store.ListEvents(ctx) |
| 37 | + if err != nil { |
| 38 | + return &commandError{ |
| 39 | + Code: exitOperationalFailure, |
| 40 | + Err: err, |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + projection, err := digestproj.BuildProjection(records, opts.OutputDir, opts.Scope, opts.Window, time.Now().UTC()) |
| 45 | + if err != nil { |
| 46 | + return &commandError{ |
| 47 | + Code: exitOperationalFailure, |
| 48 | + Err: err, |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if err := digestproj.PublishProjection(projection); err != nil { |
| 53 | + return &commandError{ |
| 54 | + Code: exitOperationalFailure, |
| 55 | + Err: err, |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + switch opts.Format { |
| 60 | + case "json": |
| 61 | + if err := writeJSON(os.Stdout, projection.Envelope); err != nil { |
| 62 | + return &commandError{ |
| 63 | + Code: exitOperationalFailure, |
| 64 | + Err: err, |
| 65 | + } |
| 66 | + } |
| 67 | + case "md": |
| 68 | + fmt.Print(projection.Markdown) |
| 69 | + case "text": |
| 70 | + fmt.Printf( |
| 71 | + "scope=%s window=%s generated_at=%s event_count=%d markdown_path=%s\n", |
| 72 | + projection.Envelope.Scope, |
| 73 | + projection.Envelope.Window, |
| 74 | + projection.Envelope.GeneratedAt, |
| 75 | + len(projection.Envelope.EventIDs), |
| 76 | + projection.Envelope.MarkdownPath, |
| 77 | + ) |
| 78 | + default: |
| 79 | + return &commandError{ |
| 80 | + Code: exitInvalidArguments, |
| 81 | + Err: fmt.Errorf("unsupported format %q, expected json, text, or md", opts.Format), |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func parseDigestOptions(args []string) (digestOptions, error) { |
| 89 | + opts := digestOptions{ |
| 90 | + StateDir: "state", |
| 91 | + OutputDir: "output", |
| 92 | + Window: "24h", |
| 93 | + Format: "json", |
| 94 | + } |
| 95 | + |
| 96 | + for i := 0; i < len(args); i++ { |
| 97 | + arg := args[i] |
| 98 | + |
| 99 | + switch { |
| 100 | + case arg == "--state-dir": |
| 101 | + i++ |
| 102 | + if i >= len(args) || strings.TrimSpace(args[i]) == "" { |
| 103 | + return digestOptions{}, fmt.Errorf("missing value for --state-dir") |
| 104 | + } |
| 105 | + opts.StateDir = args[i] |
| 106 | + case strings.HasPrefix(arg, "--state-dir="): |
| 107 | + value := strings.TrimPrefix(arg, "--state-dir=") |
| 108 | + if strings.TrimSpace(value) == "" { |
| 109 | + return digestOptions{}, fmt.Errorf("missing value for --state-dir") |
| 110 | + } |
| 111 | + opts.StateDir = value |
| 112 | + case arg == "--output-dir": |
| 113 | + i++ |
| 114 | + if i >= len(args) || strings.TrimSpace(args[i]) == "" { |
| 115 | + return digestOptions{}, fmt.Errorf("missing value for --output-dir") |
| 116 | + } |
| 117 | + opts.OutputDir = args[i] |
| 118 | + case strings.HasPrefix(arg, "--output-dir="): |
| 119 | + value := strings.TrimPrefix(arg, "--output-dir=") |
| 120 | + if strings.TrimSpace(value) == "" { |
| 121 | + return digestOptions{}, fmt.Errorf("missing value for --output-dir") |
| 122 | + } |
| 123 | + opts.OutputDir = value |
| 124 | + case arg == "--window": |
| 125 | + i++ |
| 126 | + if i >= len(args) || strings.TrimSpace(args[i]) == "" { |
| 127 | + return digestOptions{}, fmt.Errorf("missing value for --window") |
| 128 | + } |
| 129 | + opts.Window = args[i] |
| 130 | + case strings.HasPrefix(arg, "--window="): |
| 131 | + value := strings.TrimPrefix(arg, "--window=") |
| 132 | + if strings.TrimSpace(value) == "" { |
| 133 | + return digestOptions{}, fmt.Errorf("missing value for --window") |
| 134 | + } |
| 135 | + opts.Window = value |
| 136 | + case arg == "--format": |
| 137 | + i++ |
| 138 | + if i >= len(args) || strings.TrimSpace(args[i]) == "" { |
| 139 | + return digestOptions{}, fmt.Errorf("missing value for --format") |
| 140 | + } |
| 141 | + opts.Format = args[i] |
| 142 | + case strings.HasPrefix(arg, "--format="): |
| 143 | + value := strings.TrimPrefix(arg, "--format=") |
| 144 | + if strings.TrimSpace(value) == "" { |
| 145 | + return digestOptions{}, fmt.Errorf("missing value for --format") |
| 146 | + } |
| 147 | + opts.Format = value |
| 148 | + case strings.HasPrefix(arg, "-"): |
| 149 | + return digestOptions{}, fmt.Errorf("unknown flag: %s", arg) |
| 150 | + default: |
| 151 | + if opts.Scope != "" { |
| 152 | + return digestOptions{}, fmt.Errorf("usage: sift digest <scope> [--window 24h] [--format json|text|md]") |
| 153 | + } |
| 154 | + opts.Scope = strings.ToLower(strings.TrimSpace(arg)) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + if opts.Scope == "" { |
| 159 | + return digestOptions{}, fmt.Errorf("usage: sift digest <scope> [--window 24h] [--format json|text|md]") |
| 160 | + } |
| 161 | + |
| 162 | + if opts.Format != "json" && opts.Format != "text" && opts.Format != "md" { |
| 163 | + return digestOptions{}, fmt.Errorf("unsupported format %q, expected json, text, or md", opts.Format) |
| 164 | + } |
| 165 | + |
| 166 | + return opts, nil |
| 167 | +} |
0 commit comments