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
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
use std::path::Path;
use anyhow::Result;
use crate::cli::args::{
Commands, FindFilters, IndexFlags, LinksAction, PropertiesAction, TagsAction, TaskAction,
TypesAction, ViewsAction, resolve_single_file,
};
use crate::commands::{
IndexResolution, ResolvedIndex, append as append_commands, backlinks as backlinks_commands,
create_index as create_index_commands, drop_index as drop_index_commands,
find as find_commands, links as links_commands, lint as lint_commands, mv as mv_commands,
properties, read as read_commands, remove as remove_commands, resolve_index,
set as set_commands, summary as summary_commands, tags as tag_commands, tasks as task_commands,
};
use crate::output::{CommandOutcome, Format};
use hyalo_core::bm25::parse_language;
use hyalo_core::case_index::{CaseInsensitiveIndex, CaseInsensitiveMode, mode_enabled};
use hyalo_core::filter;
use hyalo_core::index::{ScanOptions, SnapshotIndex, VaultIndex as _};
use hyalo_core::schema::SchemaConfig;
/// Default output limit for list commands when no `--limit` is passed and no
/// `default_limit` is set in `.hyalo.toml`.
pub(crate) const DEFAULT_OUTPUT_LIMIT: usize = 50;
/// Build a [`CaseInsensitiveIndex`] from a full vault directory scan.
///
/// The scan is always vault-wide — not scoped to any `--file` or `--glob`
/// argument — because case-insensitive link resolution must find *any* file
/// in the vault, even files not included in the current query scope. A scoped
/// `VaultIndex` (built by `collect_files` when `--file` is used) would omit
/// the very link targets we need to resolve, so we re-walk from disk rather
/// than reusing the command's `VaultIndex`.
///
/// Errors during discovery are silently ignored (the index will just be less
/// complete, which degrades gracefully to no case-insensitive fallback).
pub(crate) fn build_case_index_from_dir(dir: &std::path::Path) -> CaseInsensitiveIndex {
use hyalo_core::discovery;
let mut idx = CaseInsensitiveIndex::new();
if let Ok(files) = discovery::discover_files(dir) {
for file in &files {
let rel = discovery::relative_path(dir, file);
idx.insert(&rel);
}
}
idx
}
/// Resolve whether case-insensitive mode is active and, if so, build the
/// index from a full vault directory scan. Returns `Some(index)` when
/// enabled, `None` when disabled.
pub(crate) fn maybe_case_index(
mode: CaseInsensitiveMode,
dir: &std::path::Path,
) -> Option<CaseInsensitiveIndex> {
if mode_enabled(mode, dir) {
Some(build_case_index_from_dir(dir))
} else {
None
}
}
/// Shared context for command dispatch.
pub(crate) struct CommandContext<'a> {
pub dir: &'a Path,
/// The directory where `.hyalo.toml` was loaded from. This is the
/// project root when `dir` comes from `dir = "subdir"` in the config,
/// or the `--dir` target when the user passes `--dir` explicitly.
/// Views and types are stored in `config_dir/.hyalo.toml`.
pub config_dir: &'a Path,
pub site_prefix: Option<&'a str>,
/// Internal format — always Json; commands build JSON, pipeline handles conversion.
pub effective_format: Format,
/// The user-requested format (Text or Json). Used by `read` to decide between
/// `RawOutput` (text mode) and `Success` (JSON mode).
pub user_format: Format,
pub snapshot_index: &'a mut Option<SnapshotIndex>,
pub index_path: Option<&'a Path>,
/// Default stemming language from `[search] language` in `.hyalo.toml`.
pub config_language: Option<&'a str>,
/// Frontmatter property names to scan for `[[wikilink]]` values in the link graph.
/// Comes from `[links] frontmatter_properties` in `.hyalo.toml`. `None` = use defaults.
pub frontmatter_link_props: Option<&'a [String]>,
/// Parsed schema configuration from `[schema.*]` sections in `.hyalo.toml`.
pub schema: &'a SchemaConfig,
/// When `true`, schema validation runs on every `set`/`append` operation even
/// without `--validate`. Comes from `validate_on_write = true` in `.hyalo.toml`.
pub validate_on_write: bool,
/// Vault-relative paths excluded from `hyalo lint`. From `[lint] ignore` in `.hyalo.toml`.
pub lint_ignore: &'a [String],
/// Case-insensitive link resolution mode from `[links] case_insensitive`.
pub case_insensitive_mode: CaseInsensitiveMode,
/// Optional exit code override set by commands that need a non-0/2 exit code
/// (e.g. `lint` returns 1 when errors are found). The output pipeline uses this
/// to override its own exit code calculation.
pub exit_code_override: Option<i32>,
/// Default output limit from `.hyalo.toml` (`default_limit`).
/// `None` = use `DEFAULT_OUTPUT_LIMIT`.
/// `Some(0)` = unlimited.
/// `Some(n)` = limit to n.
pub config_default_limit: Option<usize>,
/// When true, the output is consumed programmatically (`--jq` or `--count`),
/// so the default limit should not apply — only an explicit `--limit` is honoured.
pub programmatic_output: bool,
}
/// Resolve the effective limit for a list command.
///
/// Precedence (highest first):
/// 1. `cli_limit = Some(n)` — user passed `--limit n` (0 = unlimited → returns `None`)
/// 2. If `programmatic` is true (`--jq` or `--count`), skip the default limit — the
/// output is consumed by a pipeline that needs complete results.
/// 3. `config_default` = `Some(n)` from `.hyalo.toml` (0 = unlimited → returns `None`)
/// 4. `DEFAULT_OUTPUT_LIMIT` — hard-coded fallback
///
/// Returns `None` for unlimited, `Some(n)` for an effective cap.
fn resolve_limit(
cli_limit: Option<usize>,
config_default: Option<usize>,
programmatic: bool,
) -> Option<usize> {
match cli_limit {
Some(0) => None, // explicit --limit 0 = unlimited
Some(n) => Some(n),
None => {
if programmatic {
return None;
}
match config_default {
Some(0) => None, // config default_limit = 0 = unlimited
Some(n) => Some(n),
None => Some(DEFAULT_OUTPUT_LIMIT),
}
}
}
}
/// Patch the snapshot index for a list of vault-relative paths that were
/// modified on disk. Uses `refresh_entry` to fully re-scan each file
/// (properties, tags, links, sections, tasks), then flushes to disk once.
fn patch_index_for_modified_files(
snapshot_index: &mut Option<SnapshotIndex>,
index_path: Option<&Path>,
dir: &Path,
modified_files: &[String],
) -> Result<()> {
if modified_files.is_empty() {
return Ok(());
}
let Some(idx) = snapshot_index.as_mut() else {
return Ok(());
};
let mut dirty = false;
for rel in modified_files {
match idx.refresh_entry(dir, rel) {
Ok(true) => dirty = true,
Ok(false) => {} // not in index, nothing to update
Err(e) => {
eprintln!("warning: could not refresh index entry for {rel}: {e:#}");
}
}
}
crate::commands::mutation::save_index_if_dirty(snapshot_index, index_path, dirty)
}
/// Parse `--where-property` filters and validate `--where-tag` names.
/// Returns an error string on invalid input.
fn parse_where_filters(
where_properties: &[String],
where_tags: &[String],
) -> Result<Vec<filter::PropertyFilter>, String> {
let filters = where_properties
.iter()
.map(|s| filter::parse_property_filter(s))
.collect::<Result<Vec<_>, _>>()
.map_err(|e| e.to_string())?;
for tag in where_tags {
crate::commands::tags::validate_tag(tag)?;
}
Ok(filters)
}
pub(crate) fn dispatch(command: Commands, ctx: &mut CommandContext<'_>) -> Result<CommandOutcome> {
let dir = ctx.dir;
let site_prefix = ctx.site_prefix;
let effective_format = ctx.effective_format;
let snapshot_index = &mut *ctx.snapshot_index;
let index_path = ctx.index_path;
match command {
Commands::Find {
pattern,
file_positional,
view: _, // resolved before dispatch
filters: mut filters_raw,
index_flags: _, // consumed in run.rs before dispatch
} => {
// Merge positional files into filters (clap prevents positional+--file
// and positional+--glob at parse time; a view may have set glob though).
if !file_positional.is_empty() {
if !filters_raw.glob.is_empty() {
crate::warn::warn(
"positional file arguments override the view's --glob; \
glob filter has been ignored",
);
}
filters_raw.file = file_positional;
filters_raw.glob.clear(); // file overrides view's glob
}
let FindFilters {
pattern: _, // pattern is handled in run.rs before dispatch
regexp,
properties,
tag,
task,
sections,
file,
glob,
fields,
sort,
reverse,
limit,
broken_links,
orphan,
dead_end,
title,
language,
} = filters_raw;
if orphan && dead_end {
crate::warn::warn(
"--orphan and --dead-end are mutually exclusive (no file can be both); results will always be empty",
);
}
// Parse property filters
let prop_filters: Vec<filter::PropertyFilter> = match properties
.iter()
.map(|s| filter::parse_property_filter(s))
.collect::<Result<Vec<_>, _>>()
{
Ok(f) => f,
Err(e) => {
return Ok(CommandOutcome::UserError(format!("Error: {e}")));
}
};
// Parse task filter
let task_filter = match task.as_deref().map(filter::parse_task_filter) {
Some(Ok(f)) => Some(f),
Some(Err(e)) => {
return Ok(CommandOutcome::UserError(format!("Error: {e}")));
}
None => None,
};
// Parse fields
let parsed_fields = match filter::Fields::parse(&fields) {
Ok(f) => f,
Err(e) => {
return Ok(CommandOutcome::UserError(format!("Error: {e}")));
}
};
// Parse sort
let sort_field = match sort.as_deref().map(filter::parse_sort) {
Some(Ok(f)) => Some(f),
Some(Err(e)) => {
return Ok(CommandOutcome::UserError(format!("Error: {e}")));
}
None => None,
};
// Parse section filters
let section_filters: Vec<hyalo_core::heading::SectionFilter> = match sections
.iter()
.map(|s| hyalo_core::heading::SectionFilter::parse(s))
.collect::<Result<Vec<_>, _>>()
{
Ok(f) => f,
Err(e) => {
return Ok(CommandOutcome::UserError(format!("Error: {e}")));
}
};
for t in &tag {
if let Err(msg) = crate::commands::tags::validate_tag(t) {
return Ok(CommandOutcome::UserError(format!("Error: {msg}")));
}
}
// Validate --language flag and config language against supported languages.
if let Some(ref lang) = language
&& let Err(e) = parse_language(lang)
{
return Ok(CommandOutcome::UserError(format!(
"invalid --language value {lang:?}: {e}"
)));
}
if let Some(cfg_lang) = ctx.config_language
&& let Err(e) = parse_language(cfg_lang)
{
return Ok(CommandOutcome::UserError(format!(
"invalid [search].language config value {cfg_lang:?}: {e}"
)));
}
// Strip the dir prefix from --file args so that
// filter_index_entries matches vault-relative paths.
let file: Vec<String> = file
.into_iter()
.map(|f| hyalo_core::discovery::strip_dir_prefix(dir, &f).unwrap_or(f))
.collect();
let sort_needs_backlinks =
matches!(sort_field.as_ref(), Some(filter::SortField::BacklinksCount));
let sort_needs_links =
matches!(sort_field.as_ref(), Some(filter::SortField::LinksCount));
let sort_needs_title = matches!(sort_field.as_ref(), Some(filter::SortField::Title));
let has_task_filter = task_filter.is_some();
let has_section_filter = !section_filters.is_empty();
let has_title_filter = title.is_some();
// BM25 pattern search requires reading file bodies for each candidate.
let has_bm25_search = pattern.is_some();
let needs_body =
find_commands::needs_body(&parsed_fields, has_task_filter, has_section_filter)
|| sort_needs_links
|| sort_needs_title
|| broken_links
|| orphan
|| dead_end
|| has_title_filter
|| has_bm25_search;
let needs_full_vault =
parsed_fields.backlinks || sort_needs_backlinks || orphan || dead_end;
// The link graph is only built when scan_body is true, so
// backlinks / backlink-sort always require body scanning.
let scan_body = needs_body || needs_full_vault;
match resolve_index(
snapshot_index.as_ref(),
dir,
&file,
&glob,
effective_format,
site_prefix,
needs_full_vault,
&ScanOptions {
scan_body,
bm25_tokenize: false,
default_language: None,
frontmatter_link_props: ctx.frontmatter_link_props,
},
)? {
IndexResolution::Resolved(resolved) => {
let ci = maybe_case_index(ctx.case_insensitive_mode, dir);
find_commands::find(
resolved.as_index(),
dir,
site_prefix,
pattern.as_deref(),
regexp.as_deref(),
&prop_filters,
&tag,
task_filter.as_ref(),
§ion_filters,
&file,
&glob,
&parsed_fields,
sort_field.as_ref(),
reverse,
resolve_limit(limit, ctx.config_default_limit, ctx.programmatic_output),
broken_links,
orphan,
dead_end,
title.as_deref(),
effective_format,
language.as_deref(),
ctx.config_language,
ci.as_ref(),
)
}
IndexResolution::Outcome(outcome) => Ok(outcome),
}
}
Commands::Read {
file_positional,
file,
section,
lines,
frontmatter,
index_flags: _, // consumed in run.rs before dispatch
} => {
let file = match resolve_single_file(file_positional, file) {
Ok(f) => f,
Err(e) => return Ok(CommandOutcome::UserError(format!("{e}"))),
};
read_commands::run(
dir,
&file,
section.as_deref(),
lines.as_deref(),
frontmatter,
effective_format,
ctx.user_format,
)
}
Commands::Properties { action } => {
let action = action.unwrap_or(PropertiesAction::Summary {
glob: vec![],
limit: None,
index_flags: IndexFlags::default(),
});
match action {
PropertiesAction::Summary {
ref glob,
limit: cli_limit,
index_flags: _, // consumed in run.rs before dispatch
} => match resolve_index(
snapshot_index.as_ref(),
dir,
&[],
glob,
effective_format,
site_prefix,
false,
&ScanOptions {
scan_body: false,
bm25_tokenize: false,
default_language: None,
frontmatter_link_props: ctx.frontmatter_link_props,
},
)? {
IndexResolution::Resolved(ResolvedIndex::Snapshot(idx)) => {
let filtered =
find_commands::filter_index_entries(idx.entries(), &[], glob);
match filtered {
Err(e) => Err(e),
Ok(filtered) => {
let paths: Vec<String> =
filtered.iter().map(|e| e.rel_path.clone()).collect();
let file_filter = if glob.is_empty() {
None
} else {
Some(paths.as_slice())
};
properties::properties_summary(
idx,
file_filter,
effective_format,
resolve_limit(
cli_limit,
ctx.config_default_limit,
ctx.programmatic_output,
),
)
}
}
}
IndexResolution::Resolved(ResolvedIndex::Scanned(build)) => {
properties::properties_summary(
&build.index,
None,
effective_format,
resolve_limit(
cli_limit,
ctx.config_default_limit,
ctx.programmatic_output,
),
)
}
IndexResolution::Outcome(outcome) => Ok(outcome),
},
PropertiesAction::Rename {
from,
to,
glob,
dry_run,
index_flags: _, // consumed in run.rs before dispatch
} => properties::properties_rename(
dir,
&from,
&to,
&glob,
dry_run,
effective_format,
snapshot_index,
index_path,
),
}
}
Commands::Tags { action } => {
let action = action.unwrap_or(TagsAction::Summary {
glob: vec![],
limit: None,
index_flags: IndexFlags::default(),
});
match action {
TagsAction::Summary {
ref glob,
limit: cli_limit,
index_flags: _, // consumed in run.rs before dispatch
} => match resolve_index(
snapshot_index.as_ref(),
dir,
&[],
glob,
effective_format,
site_prefix,
false,
&ScanOptions {
scan_body: false,
bm25_tokenize: false,
default_language: None,
frontmatter_link_props: ctx.frontmatter_link_props,
},
)? {
IndexResolution::Resolved(ResolvedIndex::Snapshot(idx)) => {
let filtered =
find_commands::filter_index_entries(idx.entries(), &[], glob);
match filtered {
Err(e) => Err(e),
Ok(filtered) => {
let paths: Vec<String> =
filtered.iter().map(|e| e.rel_path.clone()).collect();
let file_filter = if glob.is_empty() {
None
} else {
Some(paths.as_slice())
};
tag_commands::tags_summary(
idx,
file_filter,
effective_format,
resolve_limit(
cli_limit,
ctx.config_default_limit,
ctx.programmatic_output,
),
)
}
}
}
IndexResolution::Resolved(ResolvedIndex::Scanned(build)) => {
tag_commands::tags_summary(
&build.index,
None,
effective_format,
resolve_limit(
cli_limit,
ctx.config_default_limit,
ctx.programmatic_output,
),
)
}
IndexResolution::Outcome(outcome) => Ok(outcome),
},
TagsAction::Rename {
from,
to,
glob,
dry_run,
index_flags: _, // consumed in run.rs before dispatch
} => tag_commands::tags_rename(
dir,
&from,
&to,
&glob,
dry_run,
effective_format,
snapshot_index,
index_path,
),
}
}
Commands::Task { action } => match action {
TaskAction::Read {
file_positional,
file,
line,
section,
all,
index_flags: _, // consumed in run.rs before dispatch
} => {
let file = match resolve_single_file(file_positional, file) {
Ok(f) => f,
Err(e) => return Ok(CommandOutcome::UserError(format!("{e}"))),
};
task_commands::task_read(
dir,
&file,
&line,
section.as_deref(),
all,
effective_format,
)
}
TaskAction::Toggle {
file_positional,
file,
line,
section,
all,
dry_run,
index_flags: _, // consumed in run.rs before dispatch
} => {
let file = match resolve_single_file(file_positional, file) {
Ok(f) => f,
Err(e) => return Ok(CommandOutcome::UserError(format!("{e}"))),
};
task_commands::task_toggle(
dir,
&file,
&line,
section.as_deref(),
all,
effective_format,
snapshot_index,
index_path,
dry_run,
)
}
TaskAction::Set {
file_positional,
file,
line,
section,
all,
status,
dry_run,
index_flags: _, // consumed in run.rs before dispatch
} => {
let file = match resolve_single_file(file_positional, file) {
Ok(f) => f,
Err(e) => return Ok(CommandOutcome::UserError(format!("{e}"))),
};
if status.chars().count() != 1 {
let out = crate::output::format_error(
effective_format,
"--status must be a single character",
None,
Some("example: --status '?' or --status '-'"),
None,
);
return Ok(CommandOutcome::UserError(out));
}
// chars().count() == 1 guarantees next() returns Some.
let ch = status
.chars()
.next()
.ok_or_else(|| anyhow::anyhow!("--status must be a single character"))?;
task_commands::task_set_status(
dir,
&file,
&line,
section.as_deref(),
all,
ch,
effective_format,
snapshot_index,
index_path,
dry_run,
)
}
},
Commands::Summary {
glob,
recent,
depth,
index_flags: _, // consumed in run.rs before dispatch
} => match resolve_index(
snapshot_index.as_ref(),
dir,
&[],
&glob,
effective_format,
site_prefix,
true,
&ScanOptions {
scan_body: true,
bm25_tokenize: false,
default_language: None,
frontmatter_link_props: ctx.frontmatter_link_props,
},
)? {
IndexResolution::Resolved(resolved) => {
let ci = maybe_case_index(ctx.case_insensitive_mode, dir);
summary_commands::summary(
dir,
resolved.as_index(),
&glob,
recent,
depth,
site_prefix,
effective_format,
ctx.schema,
ci.as_ref(),
)
}
IndexResolution::Outcome(outcome) => Ok(outcome),
},
Commands::Set {
file_positional,
properties,
tag,
mut file,
glob,
where_properties,
where_tags,
dry_run,
validate,
index_flags: _, // consumed in run.rs before dispatch
} => {
if !file_positional.is_empty() {
file = file_positional;
}
let where_prop_filters = match parse_where_filters(&where_properties, &where_tags) {
Ok(f) => f,
Err(e) => {
return Ok(CommandOutcome::UserError(format!("Error: {e}")));
}
};
let do_validate = validate || ctx.validate_on_write;
set_commands::set(
dir,
&properties,
&tag,
&file,
&glob,
&where_prop_filters,
&where_tags,
effective_format,
snapshot_index,
index_path,
dry_run,
do_validate,
if do_validate { Some(ctx.schema) } else { None },
)
}
Commands::Remove {
file_positional,
properties,
tag,
mut file,
glob,
where_properties,
where_tags,
dry_run,
index_flags: _, // consumed in run.rs before dispatch
} => {
if !file_positional.is_empty() {
file = file_positional;
}
let where_prop_filters = match parse_where_filters(&where_properties, &where_tags) {
Ok(f) => f,
Err(e) => {
return Ok(CommandOutcome::UserError(format!("Error: {e}")));
}
};
remove_commands::remove(
dir,
&properties,
&tag,
&file,
&glob,
&where_prop_filters,
&where_tags,
effective_format,
snapshot_index,
index_path,
dry_run,
)
}
Commands::Append {
file_positional,
properties,
mut file,
glob,
where_properties,
where_tags,
dry_run,
validate,
index_flags: _, // consumed in run.rs before dispatch
} => {
if !file_positional.is_empty() {
file = file_positional;
}
let where_prop_filters = match parse_where_filters(&where_properties, &where_tags) {
Ok(f) => f,
Err(e) => {
return Ok(CommandOutcome::UserError(format!("Error: {e}")));
}
};
let do_validate = validate || ctx.validate_on_write;
append_commands::append(
dir,
&properties,
&file,
&glob,
&where_prop_filters,
&where_tags,
effective_format,
snapshot_index,
index_path,
dry_run,
do_validate,
if do_validate { Some(ctx.schema) } else { None },
)
}
Commands::Backlinks {
file_positional,
file,
limit: cli_limit,
index_flags: _, // consumed in run.rs before dispatch
} => {
let file = match resolve_single_file(file_positional, file) {
Ok(f) => f,
Err(e) => return Ok(CommandOutcome::UserError(format!("{e}"))),
};
match resolve_index(
snapshot_index.as_ref(),
dir,
&[],
&[],
effective_format,
site_prefix,
true,
&ScanOptions {
scan_body: true,
bm25_tokenize: false,
default_language: None,
frontmatter_link_props: ctx.frontmatter_link_props,
},
)? {
IndexResolution::Resolved(resolved) => backlinks_commands::backlinks(
resolved.as_index(),
&file,
dir,
effective_format,
resolve_limit(cli_limit, ctx.config_default_limit, ctx.programmatic_output),
),
IndexResolution::Outcome(outcome) => Ok(outcome),
}
}
Commands::Mv {
file_positional,
file,
to,
dry_run,
index_flags: _, // consumed in run.rs before dispatch
} => {
let file = match resolve_single_file(file_positional, file) {
Ok(f) => f,
Err(e) => return Ok(CommandOutcome::UserError(format!("{e}"))),
};
mv_commands::mv(
dir,
&file,
&to,
dry_run,
effective_format,
site_prefix,
snapshot_index,
index_path,
)
}
Commands::CreateIndex {
output,
allow_outside_vault,
} => create_index_commands::create_index(
dir,
site_prefix,
output.as_deref(),
effective_format,
allow_outside_vault,
ctx.config_language,
),
Commands::DropIndex {
path,
allow_outside_vault,
} => drop_index_commands::drop_index(
dir,
path.as_deref(),
effective_format,
allow_outside_vault,
),
Commands::Links { action } => match action {
LinksAction::Fix {
dry_run: _,
apply,
threshold,
glob,
ignore_target,
index_flags: _, // consumed in run.rs before dispatch
} => {
// Scope the immutable borrow of snapshot_index (via resolve_index)
// so we can borrow it mutably for index updates afterwards.
let (outcome, modified_files) = match resolve_index(
snapshot_index.as_ref(),
dir,
&[],
&[],
effective_format,
site_prefix,
true,
&ScanOptions {
scan_body: true,
bm25_tokenize: false,
default_language: None,
frontmatter_link_props: ctx.frontmatter_link_props,
},
)? {
IndexResolution::Resolved(resolved) => {
let ci = maybe_case_index(ctx.case_insensitive_mode, dir);
links_commands::links_fix(
resolved.as_index(),
dir,
site_prefix,
&glob,
!apply,
threshold,
&ignore_target,
effective_format,
ci.as_ref(),
)?
}
IndexResolution::Outcome(outcome) => (outcome, Vec::new()),
};
// resolved is dropped — safe to borrow snapshot_index mutably.
patch_index_for_modified_files(snapshot_index, index_path, dir, &modified_files)?;
Ok(outcome)
}
LinksAction::Auto {
dry_run: _,
apply,
min_length,
exclude_title,
first_only,
exclude_target_glob,
file,
glob,
index_flags: _, // consumed in run.rs before dispatch
} => {
let (outcome, modified_files) = match resolve_index(
snapshot_index.as_ref(),
dir,
&[],
&[],
effective_format,
site_prefix,
true,
&ScanOptions {
scan_body: false,
bm25_tokenize: false,
default_language: None,
frontmatter_link_props: ctx.frontmatter_link_props,
},
)? {
IndexResolution::Resolved(resolved) => links_commands::links_auto(
resolved.as_index(),
dir,
apply,
min_length,
&exclude_title,
first_only,
&exclude_target_glob,
file.as_deref(),
&glob,
effective_format,
)?,
IndexResolution::Outcome(outcome) => (outcome, Vec::new()),
};
patch_index_for_modified_files(snapshot_index, index_path, dir, &modified_files)?;
Ok(outcome)
}
},
Commands::Lint {
file_positional,
file,
glob,
r#type: lint_type,
fix,
dry_run,
limit: cli_limit,
index_flags: _, // consumed in run.rs before dispatch
} => {
// Resolve --type to a glob pattern from its filename_template.
let type_glob: Option<String> = if let Some(type_name) = lint_type {
use hyalo_core::filename_template::FilenameTemplate;
match ctx.schema.types.get(&type_name) {
Some(ts) => match &ts.filename_template {
Some(template_str) => match FilenameTemplate::parse(template_str) {
Ok(tpl) => Some(tpl.to_glob()),
Err(e) => {
return Ok(crate::output::CommandOutcome::UserError(
crate::output::format_error(
ctx.user_format,
&format!(
"invalid filename_template for type '{type_name}': {e}"
),
None,
None,
None,
),
));
}
},
None => {
return Ok(crate::output::CommandOutcome::UserError(
crate::output::format_error(
ctx.user_format,
&format!("type '{type_name}' has no filename_template defined"),
None,
Some(
"set one with: hyalo types set <name> --filename-template <pattern>",
),
None,
),
));
}
},
None => {
return Ok(crate::output::CommandOutcome::UserError(
crate::output::format_error(
ctx.user_format,
&format!("unknown type '{type_name}'"),
None,
Some("run `hyalo types list` to see available types"),
None,
),
));
}
}
} else {
None
};
// Build the file list. Positional arg is treated as a single --file.
let mut files_arg: Vec<String> = file;
if let Some(pos) = file_positional {
files_arg.insert(0, pos);
}
// --type expands to a glob that overrides file/glob args.
let effective_glob: Vec<String> = if let Some(g) = type_glob {
vec![g]
} else {
glob
};
let file_pairs = match crate::commands::collect_files(
dir,
&files_arg,
&effective_glob,
ctx.user_format,
)? {
crate::commands::FilesOrOutcome::Files(f) => f,
crate::commands::FilesOrOutcome::Outcome(o) => return Ok(o),
};
let fix_mode = if fix {
if dry_run {
lint_commands::FixMode::DryRun
} else {
lint_commands::FixMode::Apply
}
} else {
lint_commands::FixMode::Off
};
// Filter out files matching `[lint] ignore` entries.
//
// Each entry is matched against the vault-relative path (with `/`
// separators) as a glob: `vendor/**/*.md`, `legacy/known-bad.md`,
// `templates/*.md`. An entry without glob meta-characters is matched
// literally (exact path equality on the normalized path).
let filtered_pairs: Vec<_> = if ctx.lint_ignore.is_empty() {
file_pairs
} else {
use globset::{GlobBuilder, GlobSetBuilder};
let mut builder = GlobSetBuilder::new();
let mut build_failed = false;
for pat in ctx.lint_ignore {
match GlobBuilder::new(pat)
.literal_separator(true)
.backslash_escape(true)
.build()
{
Ok(g) => {
builder.add(g);
}
Err(e) => {
crate::warn::warn(format!(
"invalid [lint] ignore pattern {pat:?}: {e}"
));
build_failed = true;
}
}
}
let set = if build_failed {
None
} else {
builder.build().ok()
};
match set {
Some(set) => file_pairs
.into_iter()
.filter(|(_, rel)| {
let norm = rel.replace('\\', "/");
!set.is_match(&norm)
})
.collect(),
// If building the set failed (warning already emitted above),
// fall back to no filtering rather than silently ignoring
// potentially relevant files.
None => file_pairs,
}
};
let (outcome, mut counts) = lint_commands::lint_files_with_options(
&filtered_pairs,
ctx.schema,
fix_mode,
resolve_limit(cli_limit, ctx.config_default_limit, ctx.programmatic_output),
snapshot_index,
index_path,
)?;
// Additional config-level lint: check view definitions.
//
// Views live in `.hyalo.toml`, which is located in `ctx.config_dir`
// — that directory can differ from `dir` when the config sets
// `dir = "subkb"` and the `.hyalo.toml` sits in the parent.
let config_violations = lint_commands::validate_views(ctx.config_dir);
let outcome = if let Some(view_result) = config_violations {
for v in &view_result.violations {
match v.severity {
lint_commands::Severity::Error => counts.errors += 1,
lint_commands::Severity::Warn => counts.warnings += 1,
}
}
counts.files_with_issues += 1;
lint_commands::prepend_file_result(outcome, &view_result)?
} else {
outcome
};
// Signal exit code 1 when errors remain after fixes (set before returning).
if counts.errors > 0 {
ctx.exit_code_override = Some(1);
}
Ok(outcome)
}
// `Init`, `Deinit`, and `Completion` are handled as early returns before dispatch is called.
Commands::Init { .. } => unreachable!("Init is dispatched before this match reached"),
Commands::Deinit => unreachable!("Deinit is dispatched before this match reached"),
Commands::Completion { .. } => {
unreachable!("Completion is dispatched before this match reached")
}
Commands::Views { action } => {
let action = action.unwrap_or(ViewsAction::List);
match action {
ViewsAction::List => {
crate::commands::views::list_views(ctx.config_dir, effective_format)
}
ViewsAction::Set {
name,
pattern,
mut filters,
} => {
if pattern.is_some() && filters.regexp.is_some() {
return Ok(CommandOutcome::UserError(
"Error: PATTERN and --regexp are mutually exclusive".to_owned(),
));
}
filters.pattern = pattern;
crate::commands::views::set_view(
ctx.config_dir,
&name,
&filters,
effective_format,
)
}
ViewsAction::Remove { name } => {
crate::commands::views::remove_view(ctx.config_dir, &name, effective_format)
}
}
}
Commands::Types { action } => {
let action = action.unwrap_or(TypesAction::List);
match action {
TypesAction::List => Ok(crate::commands::types::list_types(ctx.schema)),
TypesAction::Show { type_name } => Ok(crate::commands::types::show_type(
&type_name,
ctx.schema,
effective_format,
)),
TypesAction::Remove { type_name } => crate::commands::types::remove_type(
ctx.config_dir,
&type_name,
effective_format,
),
TypesAction::Set {
type_name,
required,
default,
property_type,
property_values,
filename_template,
dry_run,
} => crate::commands::types::set_type(
ctx.config_dir,
&type_name,
&required,
&default,
&property_type,
&property_values,
filename_template.as_deref(),
dry_run,
effective_format,
),
}
}
}
}