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
//! shq: Shell Query - CLI for capturing and querying shell command history.
use clap::{Parser, Subcommand};
mod commands;
mod hooks;
#[derive(Parser)]
#[command(name = "shq")]
#[command(about = "Shell Query - capture and query shell command history")]
#[command(version)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Initialize BIRD database
Init {
/// Storage mode: duckdb (single-writer, simpler) or parquet (multi-writer, needs compaction)
#[arg(short = 'm', long = "mode", default_value = "duckdb")]
mode: String,
/// Force re-initialization (deletes existing database)
#[arg(short = 'f', long = "force")]
force: bool,
},
/// Run a command and capture it to BIRD
#[command(visible_alias = "r")]
Run {
/// Shell command string (passed to $SHELL -c)
#[arg(short = 'c', long = "command")]
shell_cmd: Option<String>,
/// Tag this invocation with a name (like git tag)
#[arg(short = 't', long = "tag")]
tag: Option<String>,
/// Extract events from output after command completes (overrides config)
#[arg(short = 'x', long = "extract", conflicts_with = "no_extract")]
extract: bool,
/// Disable event extraction (overrides config)
#[arg(short = 'X', long = "no-extract", conflicts_with = "extract")]
no_extract: bool,
/// Override format detection for event extraction (e.g., gcc, pytest, cargo)
#[arg(short = 'f', long = "extract-format")]
format: Option<String>,
/// Run compaction in background after command completes
#[arg(short = 'C', long = "compact")]
compact: bool,
/// The command to run (alternative to -c)
#[arg(trailing_var_arg = true)]
cmd: Vec<String>,
},
/// Save output from stdin or file to BIRD
#[command(visible_alias = "S")]
Save {
/// File to read output from (reads stdin if not provided, unless --stdout/--stderr used)
file: Option<String>,
/// Command string (required for Phase A)
#[arg(short = 'c', long = "command", required = true)]
command: String,
/// Exit code of the command
#[arg(short = 'x', long = "exit-code", default_value = "0")]
exit_code: i32,
/// Duration in milliseconds
#[arg(short = 'd', long = "duration")]
duration_ms: Option<i64>,
/// Stream name when reading from stdin/file (default: stdout)
#[arg(short = 's', long = "stream", default_value = "stdout")]
stream: String,
/// File containing stdout (for capturing both streams)
#[arg(short = 'o', long = "stdout", conflicts_with = "file")]
stdout_file: Option<String>,
/// File containing stderr (for capturing both streams)
#[arg(short = 'e', long = "stderr", conflicts_with = "file")]
stderr_file: Option<String>,
/// Session ID (default: shell-{PPID})
#[arg(long = "session-id")]
session_id: Option<String>,
/// Invoker PID (default: PPID)
#[arg(long = "invoker-pid")]
invoker_pid: Option<u32>,
/// Invoker name (default: basename of $SHELL)
#[arg(long = "invoker")]
invoker: Option<String>,
/// Invoker type (default: shell)
#[arg(long = "invoker-type", default_value = "shell")]
invoker_type: String,
/// Extract events after saving (uses config default if not specified)
#[arg(long = "extract")]
extract: bool,
/// Run compaction check after saving
#[arg(long = "compact")]
compact: bool,
/// Tag this invocation with a name (like git tag)
#[arg(short = 't', long = "tag")]
tag: Option<String>,
/// Suppress informational output
#[arg(short = 'q', long = "quiet")]
quiet: bool,
},
/// Show captured output from invocation(s)
#[command(visible_aliases = ["o", "show"])]
Output {
/// Query selector (e.g., ~1, shell:%exit<>0~5, %/make/~1)
#[arg(default_value = "~1")]
query: String,
/// Filter by stream: O/stdout, E/stderr, A/all (combined to stdout)
/// Default (no flag) shows streams routed to their original fds
#[arg(short = 's', long = "stream")]
stream: Option<String>,
/// Shortcut for -s stdout
#[arg(short = 'O', long = "stdout", conflicts_with = "stream")]
stdout_only: bool,
/// Shortcut for -s stderr
#[arg(short = 'E', long = "stderr", conflicts_with = "stream")]
stderr_only: bool,
/// Shortcut for -s all (combined to stdout)
#[arg(short = 'A', long = "all", conflicts_with = "stream")]
all_combined: bool,
/// Pipe output through pager ($PAGER or less -R)
#[arg(short = 'P', long = "pager")]
pager: bool,
/// Raw output - preserve ANSI escape codes (default)
#[arg(short = 'R', long = "raw", conflicts_with = "strip")]
raw: bool,
/// Strip ANSI escape codes from output
#[arg(long = "strip")]
strip: bool,
/// Show first N lines
#[arg(long = "head", value_name = "N")]
head: Option<usize>,
/// Show last N lines
#[arg(short = 't', long = "tail", value_name = "N")]
tail: Option<usize>,
/// Limit output to N lines (same as --head)
#[arg(short = 'n', long = "lines", value_name = "N", conflicts_with = "head")]
lines: Option<usize>,
},
/// List invocation history
#[command(visible_aliases = ["i", "history", "list"])]
Invocations {
/// Query selector (e.g., ~20, shell:~10, %failed~5)
#[arg(default_value = "~20")]
query: String,
/// Output format: compact (default), table, json
#[arg(short = 'f', long = "format", default_value = "compact")]
format: String,
/// Show detailed table view (same as -f table)
#[arg(short = 'd', long = "details")]
details: bool,
/// Limit to N most recent invocations (overrides ~N in query)
#[arg(short = 'n', long = "limit")]
limit: Option<usize>,
},
/// Show detailed info about an invocation
#[command(visible_alias = "I")]
Info {
/// Query selector (e.g., ~1, %/make/~1) or short ID
#[arg(default_value = "~1")]
query: String,
/// Output format: table, json
#[arg(short = 'f', long = "format", default_value = "table")]
format: String,
/// Return only this field (id, cmd, cwd, exit, timestamp, duration, session)
#[arg(long = "field")]
field: Option<String>,
},
/// Re-run a previous command
#[command(visible_aliases = ["R", "!!"])]
Rerun {
/// Query selector (e.g., ~1, ~3, %/make/~1)
#[arg(default_value = "~1")]
query: String,
/// Print command without executing
#[arg(short = 'n', long = "dry-run")]
dry_run: bool,
/// Don't capture the re-run (just execute)
#[arg(short = 'N', long = "no-capture")]
no_capture: bool,
},
/// Execute SQL query
#[command(visible_alias = "q")]
Sql {
/// SQL query to execute
query: String,
},
/// Quick reference for commands and query syntax
#[command(name = "quick-help", visible_aliases = ["?", "h"])]
QuickHelp,
/// Show statistics
#[command(visible_aliases = ["s", "status"])]
Stats {
/// Output format: table, json
#[arg(short = 'f', long = "format", default_value = "table")]
format: String,
/// Show detailed information (config, session info)
#[arg(short = 'd', long = "details")]
details: bool,
/// Get a single field value (for scripting)
#[arg(long = "field")]
field: Option<String>,
},
/// Move old data from recent to archive
Archive {
/// Archive data older than this many days
#[arg(short = 'd', long = "days", default_value = "14")]
days: u32,
/// Show what would be done without making changes
#[arg(short = 'n', long = "dry-run")]
dry_run: bool,
/// Extract events from invocations before archiving (backfill)
#[arg(short = 'x', long = "extract-first")]
extract_first: bool,
},
/// Compact parquet files to reduce storage and improve query performance
Compact {
/// Compact when a session has more than this many non-compacted files
#[arg(short = 't', long = "threshold", default_value = "50")]
file_threshold: usize,
/// Re-compact when more than this many compacted files exist (0 = disabled)
#[arg(short = 'r', long = "recompact-threshold", default_value = "10")]
recompact_threshold: usize,
/// Consolidate ALL files into a single file per session (full merge)
#[arg(short = 'c', long = "consolidate")]
consolidate: bool,
/// Extract events from invocations before compacting
#[arg(short = 'x', long = "extract-first")]
extract_first: bool,
/// Only compact files for this specific session (used by shell hooks)
#[arg(short = 's', long = "session")]
session: Option<String>,
/// Only check today's partition (fast check for shell hooks)
#[arg(long = "today")]
today_only: bool,
/// Suppress output unless compaction occurs
#[arg(short = 'q', long = "quiet")]
quiet: bool,
/// Only compact recent data (skip archive tier)
#[arg(long = "recent-only")]
recent_only: bool,
/// Only compact archive tier (skip recent)
#[arg(long = "archive-only")]
archive_only: bool,
/// Show what would be done without making changes
#[arg(short = 'n', long = "dry-run")]
dry_run: bool,
},
/// Shell hook integration
Hook {
#[command(subcommand)]
action: HookAction,
},
/// Manage format detection hints
#[command(name = "format-hints", visible_alias = "fh")]
FormatHints {
#[command(subcommand)]
action: FormatHintsAction,
},
/// Manage remote storage connections
Remote {
#[command(subcommand)]
action: RemoteAction,
},
/// Push local data to a remote
Push {
/// Remote to push to (uses default if not specified)
#[arg(short, long)]
remote: Option<String>,
/// Only push data since this date or duration (e.g., "7d", "2024-01-15")
#[arg(short, long)]
since: Option<String>,
/// Show what would be pushed without actually pushing
#[arg(long)]
dry_run: bool,
/// Sync blob files (not just metadata)
#[arg(short, long)]
blobs: bool,
},
/// Pull data from a remote to local
Pull {
/// Remote to pull from (uses default if not specified)
#[arg(short, long)]
remote: Option<String>,
/// Only pull data from this client
#[arg(short, long)]
client: Option<String>,
/// Only pull data since this date or duration (e.g., "7d", "2024-01-15")
#[arg(short, long)]
since: Option<String>,
/// Sync blob files (not just metadata)
#[arg(short, long)]
blobs: bool,
},
/// Query parsed events (errors, warnings, test results) from invocation outputs
#[command(visible_alias = "e")]
Events {
/// Query selector (e.g., ~10, %exit<>0~5, %/cargo/~10)
#[arg(default_value = "~10")]
query: String,
/// Filter by severity (error, warning, info, note)
#[arg(short = 's', long = "severity")]
severity: Option<String>,
/// Show count only
#[arg(long = "count")]
count_only: bool,
/// Number of events: N (any), +N (first N), -N (last N)
#[arg(short = 'n', long = "lines", default_value = "50", allow_hyphen_values = true)]
lines: String,
/// Re-parse events from original blobs (ignore cached events)
#[arg(long = "reparse")]
reparse: bool,
/// Extract events if not already extracted
#[arg(short = 'x', long = "extract")]
extract: bool,
/// Override format detection (e.g., gcc, pytest, cargo)
#[arg(short = 'f', long = "format")]
format: Option<String>,
},
/// Update DuckDB extensions to latest versions
UpdateExtensions {
/// Show what would be done without making changes
#[arg(short = 'n', long = "dry-run")]
dry_run: bool,
},
/// Extract events from an invocation's output
ExtractEvents {
/// Invocation ID (default: last invocation, ignored if --all)
#[arg(default_value = "-1", allow_hyphen_values = true)]
selector: String,
/// Override format detection (default: auto or from config)
#[arg(short = 'f', long = "format")]
format: Option<String>,
/// Suppress output (for shell hooks)
#[arg(short = 'q', long = "quiet")]
quiet: bool,
/// Re-extract even if events already exist
#[arg(long = "force")]
force: bool,
/// Extract from all invocations that don't have events yet
#[arg(short = 'a', long = "all")]
all: bool,
/// Only process invocations since this date (YYYY-MM-DD, default: 30 days ago)
#[arg(long = "since")]
since: Option<String>,
/// Maximum number of invocations to process (default: 1000)
#[arg(short = 'n', long = "limit")]
limit: Option<usize>,
/// Show what would be extracted without actually extracting
#[arg(long = "dry-run")]
dry_run: bool,
},
}
#[derive(Subcommand)]
enum HookAction {
/// Output shell integration code (add to .zshrc/.bashrc)
Init {
/// Shell type (zsh, bash). Auto-detected from $SHELL if not specified.
#[arg(short, long)]
shell: Option<String>,
/// Only define shq-on function, don't activate hooks immediately
#[arg(long)]
inactive: bool,
/// Disable prompt indicator (green dot when active)
#[arg(long)]
no_prompt_indicator: bool,
/// Suppress status messages
#[arg(short, long)]
quiet: bool,
},
/// Output ignore patterns for shell hooks (colon-separated)
IgnorePatterns,
}
#[derive(Subcommand)]
enum FormatHintsAction {
/// List format hints (user-defined and built-in)
List {
/// Filter by format name or pattern (substring match)
filter: Option<String>,
/// Show only user-defined hints
#[arg(short = 'u', long)]
user_only: bool,
/// Show only built-in hints from duck_hunt
#[arg(short = 'b', long)]
builtin_only: bool,
},
/// Add a format hint
Add {
/// Glob pattern to match (e.g., "*mycompiler*", "custom-build*")
pattern: String,
/// Format name (e.g., gcc, pytest, cargo_build)
format: String,
/// Priority (higher wins, default: 500)
#[arg(short = 'p', long)]
priority: Option<i32>,
},
/// Remove a format hint by pattern
Remove {
/// Pattern to remove
pattern: String,
},
/// Check which format would be detected for a command
Check {
/// Command to check
command: String,
},
/// Set the default format (when no patterns match)
SetDefault {
/// Default format (e.g., auto, text)
format: String,
},
}
#[derive(Subcommand)]
enum RemoteAction {
/// Add a remote storage connection
Add {
/// Name for this remote (e.g., team, backup, ci)
name: String,
/// Remote type: s3, motherduck, postgres, or file
#[arg(short = 't', long = "type")]
remote_type: String,
/// URI for the remote (e.g., s3://bucket/path/bird.duckdb, md:database_name)
#[arg(short = 'u', long)]
uri: String,
/// Mount as read-only
#[arg(long)]
read_only: bool,
/// Credential provider for S3 (e.g., credential_chain)
#[arg(long)]
credential_provider: Option<String>,
/// Don't auto-attach on connection open
#[arg(long)]
no_auto_attach: bool,
},
/// List configured remotes
List,
/// Remove a remote configuration
Remove {
/// Name of the remote to remove
name: String,
},
/// Test connection to a remote
Test {
/// Name of the remote to test (tests all if not specified)
name: Option<String>,
},
/// Manually attach a remote (for current session only)
Attach {
/// Name of the remote to attach
name: String,
},
/// Show sync status
Status,
}
/// Parse lines argument: N (any), +N (first N), -N (last N).
fn parse_lines_arg(s: &str) -> (usize, commands::LimitOrder) {
use commands::LimitOrder;
let s = s.trim();
if let Some(rest) = s.strip_prefix('+') {
(rest.parse().unwrap_or(50), LimitOrder::First)
} else if let Some(rest) = s.strip_prefix('-') {
(rest.parse().unwrap_or(50), LimitOrder::Last)
} else {
(s.parse().unwrap_or(50), LimitOrder::Any)
}
}
fn main() {
let cli = Cli::parse();
let result = match cli.command {
Commands::Init { mode, force } => commands::init(&mode, force),
Commands::Run { shell_cmd, tag, extract, no_extract, format, compact, cmd } => {
// Resolve extract behavior: --extract forces on, --no-extract forces off, otherwise use config
let extract_override = if extract {
Some(true)
} else if no_extract {
Some(false)
} else {
None
};
commands::run(shell_cmd.as_deref(), &cmd, tag.as_deref(), extract_override, format.as_deref(), compact)
}
Commands::Save { file, command, exit_code, duration_ms, stream, stdout_file, stderr_file, session_id, invoker_pid, invoker, invoker_type, extract, compact, tag, quiet } => {
commands::save(
file.as_deref(),
&command,
exit_code,
duration_ms,
&stream,
stdout_file.as_deref(),
stderr_file.as_deref(),
session_id.as_deref(),
invoker_pid,
invoker.as_deref(),
&invoker_type,
extract,
compact,
tag.as_deref(),
quiet,
)
}
Commands::Output { query, stream, stdout_only, stderr_only, all_combined, pager, raw: _, strip, head, tail, lines } => {
// Resolve stream from flags or -s value
let resolved_stream = if stdout_only {
Some("stdout")
} else if stderr_only {
Some("stderr")
} else if all_combined {
Some("all")
} else {
stream.as_deref()
};
let opts = commands::OutputOptions {
pager,
strip_ansi: strip,
head: head.or(lines),
tail,
};
commands::output(&query, resolved_stream, &opts)
}
Commands::Invocations { query, format, details, limit } => {
let fmt = if details { "table" } else { &format };
commands::invocations(&query, fmt, limit)
}
Commands::Info { query, format, field } => commands::info(&query, &format, field.as_deref()),
Commands::Rerun { query, dry_run, no_capture } => commands::rerun(&query, dry_run, no_capture),
Commands::Sql { query } => commands::sql(&query),
Commands::QuickHelp => commands::quick_help(),
Commands::Stats { format, details, field } => commands::stats(&format, details, field.as_deref()),
Commands::Archive { days, dry_run, extract_first } => commands::archive(days, dry_run, extract_first),
Commands::Compact { file_threshold, recompact_threshold, consolidate, extract_first, session, today_only, quiet, recent_only, archive_only, dry_run } => {
commands::compact(file_threshold, recompact_threshold, consolidate, extract_first, session.as_deref(), today_only, quiet, recent_only, archive_only, dry_run)
}
Commands::Hook { action } => match action {
HookAction::Init { shell, inactive, no_prompt_indicator, quiet } => commands::hook_init(shell.as_deref(), inactive, !no_prompt_indicator, quiet),
HookAction::IgnorePatterns => commands::hook_ignore_patterns(),
},
Commands::FormatHints { action } => match action {
FormatHintsAction::List { filter, user_only, builtin_only } => {
let show_builtin = !user_only;
let show_user = !builtin_only;
commands::format_hints_list(show_builtin, show_user, filter.as_deref())
},
FormatHintsAction::Add { pattern, format, priority } => {
commands::format_hints_add(&pattern, &format, priority)
},
FormatHintsAction::Remove { pattern } => commands::format_hints_remove(&pattern),
FormatHintsAction::Check { command } => commands::format_hints_check(&command),
FormatHintsAction::SetDefault { format } => commands::format_hints_set_default(&format),
},
Commands::Remote { action } => match action {
RemoteAction::Add { name, remote_type, uri, read_only, credential_provider, no_auto_attach } => {
commands::remote_add(&name, &remote_type, &uri, read_only, credential_provider.as_deref(), !no_auto_attach)
},
RemoteAction::List => commands::remote_list(),
RemoteAction::Remove { name } => commands::remote_remove(&name),
RemoteAction::Test { name } => commands::remote_test(name.as_deref()),
RemoteAction::Attach { name } => commands::remote_attach(&name),
RemoteAction::Status => commands::remote_status(),
},
Commands::Push { remote, since, dry_run, blobs } => {
commands::push(remote.as_deref(), since.as_deref(), dry_run, blobs)
},
Commands::Pull { remote, client, since, blobs } => {
commands::pull(remote.as_deref(), client.as_deref(), since.as_deref(), blobs)
},
Commands::Events { query, severity, count_only, lines, reparse, extract, format } => {
// Parse lines: N (any), +N (first N), -N (last N)
let (limit, order) = parse_lines_arg(&lines);
commands::events(&query, severity.as_deref(), count_only, limit, order, reparse, extract, format.as_deref())
}
Commands::UpdateExtensions { dry_run } => commands::update_extensions(dry_run),
Commands::ExtractEvents { selector, format, quiet, force, all, since, limit, dry_run } => {
commands::extract_events(&selector, format.as_deref(), quiet, force, all, since.as_deref(), limit, dry_run)
}
};
if let Err(e) = result {
eprintln!("Error: {}", e);
std::process::exit(1);
}
}