holys3 0.5.1

Indexed regex search for local files and private S3 buckets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
mod globs;
mod index;
mod json;
mod patterns;
mod printer;
mod scope;

#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

use anyhow::{Context, Result};
use clap::{Parser, Subcommand, ValueEnum};
use holys3_core::{LocalBlobStore, MatchOptions, Strategy};
use holys3_index::{
    search_streaming, update_index, IndexChanged, IndexReader, KeyScope, LocalCorpus, LocalFetcher,
    MatchSink, SearchStats, SegmentedReader,
};
use holys3_s3::{
    build_fetch_config, build_index_namespace, is_index_key, list_prefix, region_from_env,
    s3_client_from_env, ObjectCacheConfig, ObjectMeta, S3BlobStore, S3Client, S3Corpus, S3Fetcher,
};
use scope::Scope;
use std::io::IsTerminal;
use std::path::{Path, PathBuf};
use std::time::Duration;

#[derive(Parser)]
#[command(
    name = "holys3",
    version,
    args_conflicts_with_subcommands = true,
    subcommand_negates_reqs = true,
    about = "Indexed regex search over S3 buckets and local files",
    long_about = "holys3 PATTERN TARGET searches a prebuilt index.\n\
        TARGET is s3://bucket[/prefix] or a local path.\n\
        To search for a pattern named like a subcommand (`index`, `stats`),\n\
        use -e: `holys3 -e index s3://bucket`."
)]
struct Cli {
    #[command(subcommand)]
    cmd: Option<Cmd>,
    #[command(flatten)]
    search: SearchArgs,
}

// Doc comments on clap structs are --help text; markdown would leak into it.
#[allow(clippy::doc_markdown)]
#[derive(Subcommand)]
enum Cmd {
    /// Build or update the index for TARGET (s3://bucket[/prefix] or a local directory).
    Index {
        #[arg(value_name = "TARGET")]
        target: String,
        /// Local index directory (local targets only).
        #[arg(long, default_value = "holys3.idxdir")]
        out: PathBuf,
        #[arg(long, value_enum, default_value = "trigram")]
        strategy: StrategyArg,
        /// Ignore any existing index and re-ingest everything.
        #[arg(long)]
        rebuild: bool,
        #[arg(long, requires = "interval", help = "Continuously update the index")]
        watch: bool,
        #[arg(
            long,
            value_name = "SECONDS",
            requires = "watch",
            value_parser = parse_positive_u64,
            help = "Wait SECONDS after each index attempt"
        )]
        interval: Option<u64>,
        #[arg(long, help = "Emit one JSON status object per line")]
        json: bool,
        #[command(flatten)]
        connect: ConnectArgs,
    },
    /// Report distinct grams + term-dict bytes for a local index.
    Stats {
        #[arg(long, default_value = "holys3.idxdir")]
        index: PathBuf,
    },
}

// Doc comments on clap structs are --help text; markdown would leak into it.
#[allow(clippy::doc_markdown)]
#[derive(clap::Args)]
struct ConnectArgs {
    /// AWS region (s3:// targets only). If omitted, AWS_REGION is required.
    #[arg(long)]
    region: Option<String>,
    /// Custom S3-compatible endpoint (e.g. http://127.0.0.1:9000 for MinIO).
    #[arg(long)]
    endpoint: Option<String>,
    /// Peak S3 fetch concurrency.
    #[arg(long, default_value_t = 750, value_parser = parse_concurrency)]
    concurrency: usize,
}

#[derive(Clone, Copy, PartialEq, Eq, ValueEnum)]
enum ColorArg {
    Never,
    Auto,
    Always,
    Ansi,
}

// Doc comments on clap structs are --help text; markdown would leak into it.
#[allow(clippy::doc_markdown)]
#[derive(clap::Args)]
struct SearchArgs {
    /// PATTERN then TARGET. With -e, the single positional is the TARGET.
    #[arg(value_name = "PATTERN|TARGET", required = true)]
    args: Vec<String>,
    /// A pattern to search for (repeatable; a line matching any pattern is printed).
    #[arg(short = 'e', long = "regexp", value_name = "PATTERN")]
    regexp: Vec<String>,
    /// Treat all patterns as literal strings.
    #[arg(short = 'F', long)]
    fixed_strings: bool,
    /// Case-insensitive search.
    #[arg(short = 'i', long, overrides_with_all = ["smart_case", "case_sensitive"])]
    ignore_case: bool,
    /// Case-insensitive when all patterns are lowercase.
    #[arg(short = 'S', long, overrides_with_all = ["ignore_case", "case_sensitive"])]
    smart_case: bool,
    /// Case-sensitive search (default).
    #[arg(short = 's', long, overrides_with_all = ["ignore_case", "smart_case"])]
    case_sensitive: bool,
    /// Wrap every pattern in word boundaries.
    #[arg(short = 'w', long)]
    word_regexp: bool,
    /// Print only the keys of matching objects.
    #[arg(short = 'l', long, conflicts_with_all = ["count", "count_matches", "json"])]
    files_with_matches: bool,
    /// Print the count of matching lines per object.
    #[arg(
        short = 'c',
        long,
        overrides_with = "count_matches",
        conflicts_with = "json"
    )]
    count: bool,
    /// Print the count of individual matches per object.
    #[arg(long, overrides_with = "count", conflicts_with = "json")]
    count_matches: bool,
    /// Limit the number of matching lines per object.
    #[arg(short = 'm', long, value_name = "NUM")]
    max_count: Option<u64>,
    /// Print NUM lines after each match.
    #[arg(short = 'A', long, value_name = "NUM")]
    after_context: Option<usize>,
    /// Print NUM lines before each match.
    #[arg(short = 'B', long, value_name = "NUM")]
    before_context: Option<usize>,
    /// Print NUM lines before and after each match.
    #[arg(short = 'C', long, value_name = "NUM")]
    context: Option<usize>,
    /// Show line numbers (default: on when printing to a terminal).
    #[arg(short = 'n', long, overrides_with = "no_line_number")]
    line_number: bool,
    /// Suppress line numbers.
    #[arg(short = 'N', long, overrides_with = "line_number")]
    no_line_number: bool,
    /// Show the 1-based byte column of the first match per line. Implies --line-number.
    #[arg(long)]
    column: bool,
    /// Group matches under their object key (default: on when printing to a terminal).
    #[arg(long, overrides_with = "no_heading")]
    heading: bool,
    /// One line per match: key:line:text.
    #[arg(long, overrides_with = "heading")]
    no_heading: bool,
    /// Include or exclude keys (gitignore-style glob; prefix with ! to exclude; repeatable).
    #[arg(short = 'g', long = "glob", value_name = "GLOB")]
    glob: Vec<String>,
    /// Print nothing; exit 0 at the first match.
    #[arg(short = 'q', long)]
    quiet: bool,
    /// When to use colors.
    #[arg(long, value_enum, default_value_t = ColorArg::Auto, value_name = "WHEN")]
    color: ColorArg,
    /// Emit results as JSON Lines (ripgrep-compatible wire format).
    #[arg(long)]
    json: bool,
    /// Print search statistics to stderr (with --json: the summary message).
    #[arg(long)]
    stats: bool,
    /// Local index directory (local targets only).
    #[arg(long, default_value = "holys3.idxdir")]
    index: PathBuf,
    /// Only search objects whose key starts with this prefix.
    #[arg(long)]
    key_prefix: Option<String>,
    /// Only search objects whose key matches this regex.
    #[arg(long)]
    key_regex: Option<String>,
    /// Only search objects covering times at or after this instant
    /// (`2026-06-09`, `2026-06-09T14:30[:00][Z]`, or relative 30m/6h/2d/1w).
    #[arg(long)]
    since: Option<String>,
    /// Only search objects covering times at or before this instant (same formats).
    #[arg(long)]
    until: Option<String>,
    #[arg(
        long,
        requires = "object_cache_cap",
        value_name = "DIR",
        help = "Cache immutable S3 source bodies under DIR"
    )]
    object_cache: Option<PathBuf>,
    #[arg(
        long,
        requires = "object_cache",
        value_name = "BYTES",
        value_parser = parse_positive_u64,
        help = "Limit the source-object cache to BYTES"
    )]
    object_cache_cap: Option<u64>,
    #[command(flatten)]
    connect: ConnectArgs,
}

enum Target {
    Local(PathBuf),
    S3 { bucket: String, prefix: String },
}

/// Single choke point turning a TARGET string into local-vs-S3.
fn parse_target(raw: &str) -> Result<Target> {
    match raw.strip_prefix("s3://") {
        Some(rest) => {
            let (bucket, prefix) = rest.split_once('/').unwrap_or((rest, ""));
            anyhow::ensure!(!bucket.is_empty(), "s3:// target needs a bucket");
            Ok(Target::S3 {
                bucket: bucket.to_owned(),
                prefix: prefix.to_owned(),
            })
        }
        None => Ok(Target::Local(PathBuf::from(raw))),
    }
}

enum Source {
    Local(PathBuf),
    S3(S3Source),
}

struct S3Source {
    client: S3Client,
    bucket: String,
    prefix: String,
    endpoint: Option<String>,
}

fn open_source(target: Target, connect: &ConnectArgs) -> Result<Source> {
    match target {
        Target::Local(dir) => {
            anyhow::ensure!(
                connect.region.is_none() && connect.endpoint.is_none(),
                "--region/--endpoint only apply to s3:// targets"
            );
            anyhow::ensure!(
                dir.is_dir(),
                "local target {} is not a directory",
                dir.display()
            );
            Ok(Source::Local(dir))
        }
        Target::S3 { bucket, prefix } => {
            let region = match &connect.region {
                Some(region) => region.clone(),
                None => region_from_env()?,
            };
            let client = s3_client_from_env(
                &region,
                connect.endpoint.clone(),
                build_fetch_config(connect.concurrency),
            )?;
            Ok(Source::S3(S3Source {
                client,
                bucket,
                prefix,
                endpoint: connect.endpoint.clone(),
            }))
        }
    }
}

/// rg's rule: once any -e is given, every positional is a TARGET.
fn split_pattern_target(args: Vec<String>, regexp: Vec<String>) -> Result<(Vec<String>, String)> {
    if !regexp.is_empty() {
        let [target] = <[String; 1]>::try_from(args)
            .map_err(|_| anyhow::anyhow!("with -e/--regexp, provide exactly one TARGET"))?;
        return Ok((regexp, target));
    }
    let [pattern, target] = <[String; 2]>::try_from(args)
        .map_err(|_| anyhow::anyhow!("usage: holys3 PATTERN TARGET"))?;
    Ok((vec![pattern], target))
}

#[derive(Clone, Copy, ValueEnum)]
enum StrategyArg {
    Trigram,
    Sparse,
}

impl From<StrategyArg> for Strategy {
    fn from(value: StrategyArg) -> Strategy {
        match value {
            StrategyArg::Trigram => Strategy::Trigram,
            StrategyArg::Sparse => Strategy::Sparse,
        }
    }
}

fn parse_concurrency(value: &str) -> std::result::Result<usize, String> {
    let concurrency = value.parse::<usize>().map_err(|err| err.to_string())?;
    if concurrency == 0 {
        return Err("concurrency must be greater than 0".to_owned());
    }
    Ok(concurrency)
}

fn parse_positive_u64(value: &str) -> std::result::Result<u64, String> {
    let value = value.parse::<u64>().map_err(|error| error.to_string())?;
    if value == 0 {
        return Err("value must be greater than 0".to_owned());
    }
    Ok(value)
}

fn build_local_key_prefix(dir: &Path) -> Result<String> {
    let canonical = std::fs::canonicalize(dir)?;
    let mut prefix = canonical
        .to_str()
        .with_context(|| format!("local target is not valid UTF-8: {}", canonical.display()))?
        .to_owned();
    #[cfg(windows)]
    {
        prefix = prefix.replace('\\', "/");
    }
    if !prefix.ends_with('/') {
        prefix.push('/');
    }
    Ok(prefix)
}

fn build_local(
    dir: &Path,
    out: &Path,
    strategy: Strategy,
    rebuild: bool,
) -> Result<index::IndexResult> {
    // Canonical target root: `./logs` and `logs` must produce identical
    // index keys, or invocation spelling would churn the incremental diff.
    let dir = std::fs::canonicalize(dir)?;
    std::fs::create_dir_all(out)?;
    let out_canonical = std::fs::canonicalize(out)?;
    anyhow::ensure!(
        !dir.starts_with(&out_canonical),
        "local index directory must not contain the target directory"
    );
    let corpus = LocalCorpus::new_excluding(&dir, Some(&out_canonical))?;
    let listing = corpus.listing()?;
    let store = LocalBlobStore::new(out);
    let cache_dir = local_cache_dir(out)?;
    let report = update_index(&store, &cache_dir, strategy, &listing, rebuild, &|shard| {
        Ok(Box::new(LocalCorpus::from_listing(shard)))
    })?;
    Ok(index::IndexResult {
        report,
        location: out.display().to_string(),
    })
}

fn list_user_objects(src: &S3Source) -> Result<Vec<ObjectMeta>> {
    Ok(src
        .client
        .list(&src.bucket, &list_prefix(&src.prefix))?
        .into_iter()
        .filter(|object| !is_index_key(&src.prefix, &object.key))
        .collect())
}

fn build_s3(src: &S3Source, strategy: Strategy, rebuild: bool) -> Result<index::IndexResult> {
    // Real sizes ride the listing so the build bounds its fetch chunks by
    // bytes, not just doc count — a bucket of huge objects must not OOM.
    let listing = list_user_objects(src)?
        .into_iter()
        .map(|object| (object.key, object.etag, object.size))
        .collect::<Vec<_>>();
    let cache_dir = build_cache_dir(src.endpoint.as_deref(), &src.bucket, &src.prefix)?;
    let store = S3BlobStore::new(src.client.clone(), src.bucket.clone(), src.prefix.clone());
    let client = src.client.clone();
    let bucket = src.bucket.clone();
    let report = update_index(&store, &cache_dir, strategy, &listing, rebuild, &|shard| {
        Ok(Box::new(S3Corpus::new(
            client.clone(),
            bucket.clone(),
            shard,
        )))
    })?;
    let namespace = build_index_namespace(&src.prefix);
    Ok(index::IndexResult {
        report,
        location: format!("s3://{}/{namespace}", src.bucket),
    })
}

/// Run one search against the opened source. Scope filtering, the optional
/// stats line, and the undated-keys note are shared across all output modes.
#[derive(Clone, Copy)]
struct SearchExecution<'a> {
    index: &'a Path,
    pattern: &'a str,
    scope: Option<&'a Scope>,
    options: MatchOptions,
    stats_line: bool,
    object_cache: Option<&'a ObjectCacheConfig>,
}

fn execute_search(
    source: Source,
    execution: SearchExecution<'_>,
    sink: &dyn MatchSink,
) -> Result<SearchStats> {
    let key_filter = execution.scope.map(|scope| {
        move |key: &str| {
            scope
                .key_prefix()
                .is_none_or(|prefix| key.starts_with(prefix))
                && scope.matches(key)
        }
    });
    let key_matches = key_filter
        .as_ref()
        .map(|filter| filter as &(dyn Fn(&str) -> bool + Sync));
    let search_stats = match source {
        Source::Local(dir) => {
            anyhow::ensure!(
                execution.object_cache.is_none(),
                "--object-cache only applies to s3:// targets"
            );
            let target_prefix = build_local_key_prefix(&dir)?;
            let fetcher = LocalFetcher::new(std::thread::available_parallelism()?.get())?;
            search_with_reopen(
                || open_local_reader(execution.index),
                &fetcher,
                execution.pattern,
                KeyScope {
                    prefix: Some(&target_prefix),
                    matches: key_matches,
                },
                execution.options,
                sink,
            )?
        }
        Source::S3(src) => {
            let cache_dir = build_cache_dir(src.endpoint.as_deref(), &src.bucket, &src.prefix)?;
            let client = src.client.clone();
            let bucket = src.bucket.clone();
            let prefix = src.prefix.clone();
            let fetcher = match execution.object_cache {
                Some(config) => S3Fetcher::with_cache(src.client, src.bucket, config.clone())?,
                None => S3Fetcher::new(src.client, src.bucket),
            };
            search_with_reopen(
                || {
                    let store = S3BlobStore::new(client.clone(), bucket.clone(), prefix.clone());
                    SegmentedReader::open(Box::new(store), &cache_dir)
                },
                &fetcher,
                execution.pattern,
                KeyScope {
                    prefix: execution.scope.and_then(Scope::key_prefix),
                    matches: key_matches,
                },
                execution.options,
                sink,
            )?
        }
    };
    if let Some(scope) = execution.scope {
        scope.report();
    }
    if execution.stats_line {
        eprintln!(
            "candidates={} total={} hits={}",
            search_stats.candidates,
            search_stats.total_docs,
            search_stats.hits.len(),
        );
    }
    Ok(search_stats)
}

fn search_with_reopen(
    mut open: impl FnMut() -> Result<SegmentedReader>,
    fetcher: &dyn holys3_core::DocFetcher,
    pattern: &str,
    scope: KeyScope<'_>,
    options: MatchOptions,
    sink: &dyn MatchSink,
) -> Result<SearchStats> {
    let reader = open()?;
    match search_streaming(&reader, fetcher, pattern, scope, options, sink) {
        Err(error) if error.is::<IndexChanged>() => {
            let reader = open()?;
            search_streaming(&reader, fetcher, pattern, scope, options, sink)
        }
        result => result,
    }
}

/// Returns whether anything matched (drives the exit code).
fn run_search(args: SearchArgs) -> Result<bool> {
    let (patterns, target_raw) = split_pattern_target(args.args, args.regexp)?;
    let pattern = patterns::build_pattern(
        &patterns,
        args.fixed_strings,
        args.word_regexp,
        args.ignore_case,
        args.smart_case,
    )
    .with_context(|| format!("invalid pattern {:?}", patterns.join("|")))?;
    let globs = globs::build_glob_filter(&args.glob)?;
    let scope = Scope::from_args(
        args.key_prefix,
        args.key_regex,
        args.since,
        args.until,
        globs,
    )?;
    let object_cache = match (args.object_cache, args.object_cache_cap) {
        (Some(root), Some(cap_bytes)) => Some(ObjectCacheConfig { root, cap_bytes }),
        (None, None) => None,
        _ => anyhow::bail!("--object-cache and --object-cache-cap must be supplied together"),
    };

    let standard_mode =
        !args.quiet && !args.json && !args.files_with_matches && !args.count && !args.count_matches;
    // standard output AND --json render context (rg emits context messages
    // on the JSON wire too); count/quiet/-l modes do not
    let renders_context = standard_mode || args.json;
    let before = args.before_context.or(args.context).unwrap_or(0);
    let after = args.after_context.or(args.context).unwrap_or(0);
    let options = MatchOptions {
        before_context: if renders_context { before } else { 0 },
        after_context: if renders_context { after } else { 0 },
        max_count: args.max_count,
    };

    let is_tty = std::io::stdout().is_terminal();
    let heading = if args.heading {
        true
    } else if args.no_heading {
        false
    } else {
        is_tty
    };
    let line_numbers = if args.line_number {
        true
    } else if args.no_line_number {
        false
    } else {
        args.column || is_tty
    };
    let color = printer::resolve_color(args.color, is_tty);

    let source = open_source(parse_target(&target_raw)?, &args.connect)?;
    let stats_line = args.stats && !args.json;
    let execution = SearchExecution {
        index: &args.index,
        pattern: &pattern,
        scope: scope.as_ref(),
        options,
        stats_line,
        object_cache: object_cache.as_ref(),
    };

    if args.quiet {
        let sink = printer::QuietSink::new(!args.stats);
        let result = execute_search(source, execution, &sink);
        return match result {
            Ok(_) => Ok(sink.matched()),
            // rg's quiet error-mask: a found match wins over later errors
            Err(_) if sink.matched() => Ok(true),
            Err(err) => Err(err),
        };
    }
    if args.json {
        let started = std::time::Instant::now();
        let sink = json::JsonSink::new();
        let stats = execute_search(source, execution, &sink)?;
        sink.write_summary(&stats, started.elapsed())?;
        return Ok(!stats.hits.is_empty());
    }
    let sink: Box<dyn MatchSink> = if args.files_with_matches {
        Box::new(printer::PathSink::new(color))
    } else if args.count || args.count_matches {
        Box::new(printer::CountSink::new(args.count_matches, color))
    } else {
        Box::new(printer::StandardSink::new(
            printer::RenderConfig {
                heading,
                line_numbers,
                column: args.column,
                context_active: options.before_context > 0 || options.after_context > 0,
            },
            color,
        ))
    };
    let stats = execute_search(source, execution, sink.as_ref())?;
    Ok(!stats.hits.is_empty())
}

fn run() -> Result<bool> {
    let cli = Cli::parse();
    match cli.cmd {
        Some(Cmd::Index {
            target,
            out,
            strategy,
            rebuild,
            watch: _,
            interval,
            json,
            connect,
        }) => {
            let interval = interval.map(Duration::from_secs);
            let strategy = strategy.into();
            let config = index::IndexConfig {
                target: &target,
                interval,
                rebuild,
                json,
            };
            let started = std::time::Instant::now();
            let source = match (|| -> Result<Source> {
                let source = open_source(parse_target(&target)?, &connect)?;
                if matches!(&source, Source::S3(_)) {
                    anyhow::ensure!(
                        out == Path::new("holys3.idxdir"),
                        "--out only applies to local targets"
                    );
                }
                Ok(source)
            })() {
                Ok(source) => source,
                Err(error) => {
                    index::write_start_error(&target, json, started.elapsed(), &error)?;
                    return Err(error);
                }
            };
            match source {
                Source::Local(dir) => index::run_index(config, |cycle_rebuild| {
                    build_local(&dir, &out, strategy, cycle_rebuild)
                })?,
                Source::S3(src) => {
                    index::run_index(config, |cycle_rebuild| {
                        build_s3(&src, strategy, cycle_rebuild)
                    })?;
                }
            }
            Ok(true)
        }
        Some(Cmd::Stats { index }) => {
            let reader = open_local_reader(&index)?;
            let s = reader.stats();
            println!("distinct_grams={}", s.distinct_grams);
            println!("terms_fst_bytes={}", s.terms_fst_bytes);
            println!("postings_bytes={}", s.postings_bytes);
            Ok(true)
        }
        None => run_search(cli.search),
    }
}

fn main() -> std::process::ExitCode {
    match run() {
        Ok(true) => std::process::ExitCode::SUCCESS,
        Ok(false) => std::process::ExitCode::from(1),
        Err(err) => {
            eprintln!("holys3: {err:#}");
            std::process::ExitCode::from(2)
        }
    }
}

/// Cache dir per (endpoint, bucket, prefix): readable bucket name plus a
/// short hash so `a/b` vs `a__b` prefixes (or the same bucket name on two
/// endpoints) can never share state.
fn build_cache_dir(endpoint: Option<&str>, bucket: &str, prefix: &str) -> Result<PathBuf> {
    let mut path = cache_home()?;
    path.push("holys3");
    let scope = format!("{}\0{bucket}\0{prefix}", endpoint.unwrap_or(""));
    path.push(format!(
        "{bucket}-{:016x}",
        holys3_core::hash_ngram(scope.as_bytes())
    ));
    Ok(path)
}

/// Cache dir for a local index, keyed on its canonicalized path. A REAL
/// cache directory, never `<out>/segments` itself: `SegmentedReader`'s
/// self-heal and stale-segment eviction delete cache entries, which must
/// never be the store's own blobs.
fn local_cache_dir(index_dir: &Path) -> Result<PathBuf> {
    let canonical = std::fs::canonicalize(index_dir).with_context(|| {
        format!(
            "no index at {} (run `holys3 index <TARGET> --out {0}`)",
            index_dir.display()
        )
    })?;
    let mut path = cache_home()?;
    path.push("holys3");
    path.push(format!(
        "local-{:016x}",
        holys3_core::hash_ngram(canonical.to_string_lossy().as_bytes())
    ));
    Ok(path)
}

fn open_local_reader(index_dir: &Path) -> Result<SegmentedReader> {
    let cache_dir = local_cache_dir(index_dir)?;
    SegmentedReader::open(Box::new(LocalBlobStore::new(index_dir)), &cache_dir)
}

fn cache_home() -> Result<PathBuf> {
    read_cache_home(
        std::env::var("XDG_CACHE_HOME"),
        // HOME on unix; USERPROFILE is the Windows equivalent
        std::env::var("HOME").or_else(|_| std::env::var("USERPROFILE")),
    )
}

fn read_cache_home(
    xdg_cache_home: std::result::Result<String, std::env::VarError>,
    home: std::result::Result<String, std::env::VarError>,
) -> Result<PathBuf> {
    match xdg_cache_home {
        Ok(path) => Ok(PathBuf::from(path)),
        Err(std::env::VarError::NotPresent) => Ok(PathBuf::from(home.map_err(|_| {
            anyhow::anyhow!("neither XDG_CACHE_HOME, HOME, nor USERPROFILE is set")
        })?)
        .join(".cache")),
        Err(err) => Err(err.into()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::env::VarError;

    #[test]
    fn cli_args_are_consistent() {
        use clap::CommandFactory;
        Cli::command().debug_assert();
    }

    #[test]
    fn read_cache_home_uses_xdg_cache_home() {
        let path = read_cache_home(Err(VarError::NotPresent), Ok("/home/me".to_owned())).unwrap();
        assert_eq!(path, PathBuf::from("/home/me/.cache"));

        let path = read_cache_home(Ok("/cache".to_owned()), Err(VarError::NotPresent)).unwrap();
        assert_eq!(path, PathBuf::from("/cache"));
    }

    #[test]
    fn parse_target_forms() {
        assert!(matches!(
            parse_target("s3://bkt").unwrap(),
            Target::S3 { bucket, prefix } if bucket == "bkt" && prefix.is_empty()
        ));
        assert!(matches!(
            parse_target("s3://bkt/a/b").unwrap(),
            Target::S3 { bucket, prefix } if bucket == "bkt" && prefix == "a/b"
        ));
        assert!(matches!(
            parse_target("s3://bkt/a//b/").unwrap(),
            Target::S3 { bucket, prefix } if bucket == "bkt" && prefix == "a//b/"
        ));
        assert!(parse_target("s3://").is_err());
        assert!(matches!(
            parse_target("./logs").unwrap(),
            Target::Local(p) if p == Path::new("./logs")
        ));
    }

    #[test]
    fn split_pattern_target_rules() {
        let (pats, target) =
            split_pattern_target(vec!["ERROR".into(), "s3://b".into()], vec![]).unwrap();
        assert_eq!(pats, vec!["ERROR"]);
        assert_eq!(target, "s3://b");
        let (pats, target) =
            split_pattern_target(vec!["s3://b".into()], vec!["a".into(), "b".into()]).unwrap();
        assert_eq!(pats, vec!["a", "b"]);
        assert_eq!(target, "s3://b");
        assert!(split_pattern_target(vec!["onlypattern".into()], vec![]).is_err());
        assert!(split_pattern_target(vec!["t1".into(), "t2".into()], vec!["p".into()]).is_err());
    }

    #[test]
    fn clap_parses_rg_style_invocations() {
        // subcommand wins the first positional
        let cli = Cli::try_parse_from(["holys3", "index", "s3://b"]).unwrap();
        assert!(matches!(cli.cmd, Some(Cmd::Index { .. })));
        // -e escape hatch searches for the literal word "index"
        let cli = Cli::try_parse_from(["holys3", "-e", "index", "s3://b"]).unwrap();
        assert!(cli.cmd.is_none());
        assert_eq!(cli.search.regexp, vec!["index"]);
        // last case flag wins
        let cli = Cli::try_parse_from(["holys3", "-i", "-s", "p", "t"]).unwrap();
        assert!(cli.search.case_sensitive && !cli.search.ignore_case);
        // --json conflicts with -c
        assert!(Cli::try_parse_from(["holys3", "--json", "-c", "p", "t"]).is_err());
    }
}