skillfile 1.7.1

Tool-agnostic AI skill & agent manager - the Brewfile for your AI tooling
Documentation
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
mod update_check;

use skillfile::commands;
use skillfile::config;

use std::io::Write as _;
use std::path::{Path, PathBuf};
use std::process;

use clap::{CommandFactory, FromArgMatches, Parser, Subcommand};
use clap_complete::engine::{ArgValueCandidates, CompletionCandidate};
use skillfile_core::error::SkillfileError;
use skillfile_core::parser::{
    parse_owner_repo_ref, resolve_explicit_owner_repo_ref, resolve_owner_repo_ref,
};
use skillfile_deploy::adapter::known_adapters;

/// Read entry names from the Skillfile in the current directory for shell completion.
fn complete_entry_names() -> Vec<CompletionCandidate> {
    let path = std::path::Path::new("Skillfile");
    let Ok(result) = skillfile_core::parser::parse_manifest(path) else {
        return Vec::new();
    };
    result
        .manifest
        .entries
        .iter()
        .map(|e| CompletionCandidate::new(&e.name))
        .collect()
}

/// Parse and validate entity type (must be "skill" or "agent").
fn parse_entity_type(s: &str) -> Result<String, String> {
    match s {
        "skill" | "agent" => Ok(s.to_string()),
        _ => Err(format!("invalid type '{s}': expected 'skill' or 'agent'")),
    }
}

fn cli_long_about() -> String {
    let supported = known_adapters().join(", ");
    format!(
        "\
Tool-agnostic AI skill & agent manager - the Brewfile for your AI tooling.

Declare skills and agents in a Skillfile, lock them to exact SHAs, and deploy
to any supported platform with a single command.

Supported platforms: {supported}.

Quick start:
  skillfile init                          # configure platforms
  skillfile add github skill owner/repo path/to/SKILL.md
  skillfile install                       # fetch + deploy"
    )
}

fn cli_command() -> clap::Command {
    Cli::command().long_about(cli_long_about())
}

fn completion_env_shell(shell: clap_complete::Shell) -> &'static str {
    match shell.to_string().as_str() {
        "bash" => "bash",
        "elvish" => "elvish",
        "fish" => "fish",
        "powershell" => "powershell",
        "zsh" => "zsh",
        other => panic!("unsupported completion shell: {other}"),
    }
}

fn completion_registration_output(
    completer: &Path,
    shell_name: &str,
) -> Result<Vec<u8>, SkillfileError> {
    let output = std::process::Command::new(completer)
        .env("COMPLETE", shell_name)
        .output()
        .map_err(|e| {
            SkillfileError::Install(format!("failed to generate shell completions: {e}"))
        })?;

    if output.status.success() {
        Ok(output.stdout)
    } else {
        let stderr = String::from_utf8_lossy(&output.stderr);
        Err(SkillfileError::Install(format!(
            "failed to generate shell completions: {stderr}"
        )))
    }
}

fn completion_binary_name() -> std::ffi::OsString {
    format!("skillfile{}", std::env::consts::EXE_SUFFIX).into()
}

fn completion_registration_completer_from(
    current_exe: PathBuf,
    binary_name: &std::ffi::OsStr,
) -> PathBuf {
    if current_exe
        .file_name()
        .is_some_and(|name| name == binary_name)
    {
        return current_exe;
    }

    let Some(profile_dir) = current_exe.parent().and_then(|p| p.parent()) else {
        return current_exe;
    };
    let candidate = profile_dir.join(binary_name);

    if candidate.exists() {
        candidate
    } else {
        current_exe
    }
}

fn completion_registration_completer() -> Result<PathBuf, SkillfileError> {
    let current_exe = std::env::current_exe()
        .map_err(|e| SkillfileError::Install(format!("failed to locate skillfile binary: {e}")))?;
    let binary_name = completion_binary_name();
    Ok(completion_registration_completer_from(
        current_exe,
        &binary_name,
    ))
}

fn write_completion_registration(shell: clap_complete::Shell) -> Result<(), SkillfileError> {
    let completer = completion_registration_completer()?;
    let output = completion_registration_output(&completer, completion_env_shell(shell))?;
    std::io::stdout()
        .write_all(&output)
        .map_err(SkillfileError::Io)
}

#[derive(Parser)]
#[command(
    name = "skillfile",
    about = "Tool-agnostic AI skill & agent manager",
    version,
    after_long_help = "\
ENVIRONMENT VARIABLES:
  SKILLFILE_QUIET            Suppress progress output (same as --quiet)
  GITHUB_TOKEN, GH_TOKEN    GitHub API token for SHA resolution and private repos
  MERGETOOL                  Merge tool for `skillfile resolve` (default: $EDITOR)
  EDITOR                     Fallback editor for `skillfile resolve`"
)]
struct Cli {
    /// Suppress progress output (or set SKILLFILE_QUIET=1)
    #[arg(short, long, global = true)]
    quiet: bool,

    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    // -- Setup (display_order 10-19) ------------------------------------------
    /// Configure install targets interactively
    #[command(display_order = 10)]
    #[command(long_about = "\
Configure which platforms and scopes to install for.

Writes `install` lines to your Skillfile (e.g. `install claude-code global`).
Run this once when setting up a new project.

Examples:
  skillfile init")]
    Init,

    /// Add an entry to the Skillfile
    #[command(display_order = 11)]
    #[command(long_about = "\
Add a skill or agent entry to the Skillfile. The entry is appended to the file
and automatically synced and installed if install targets are configured.

If the sync or install fails, the Skillfile and lock are rolled back.

Examples:
  skillfile add github skill owner/repo skills/SKILL.md
  skillfile add github agent owner/repo agents/reviewer.md v2.0 --name reviewer
  skillfile add local skill skills/git/commit.md
  skillfile add url agent https://example.com/agent.md --name my-agent")]
    Add {
        #[command(subcommand)]
        source: Option<AddSource>,
    },

    /// Remove an entry from the Skillfile
    #[command(display_order = 12)]
    #[command(long_about = "\
Remove a named entry from the Skillfile, its lock record, and its cached files.

Examples:
  skillfile remove browser
  skillfile remove code-refactorer")]
    Remove {
        /// Entry name to remove
        #[arg(add = ArgValueCandidates::new(complete_entry_names))]
        name: String,
    },

    // -- Workflow (display_order 20-29) ---------------------------------------
    /// Re-resolve all refs, update the lock, and redeploy
    #[command(display_order = 20)]
    #[command(long_about = "\
Re-resolve all entry refs, update Skillfile.lock to the latest SHAs, and
redeploy to all configured platforms. Equivalent to `install --update`.

Examples:
  skillfile upgrade
  skillfile upgrade --dry-run")]
    Upgrade {
        /// Show planned actions without fetching or installing
        #[arg(long)]
        dry_run: bool,
    },

    /// Fetch entries and deploy to platform directories
    #[command(display_order = 21)]
    #[command(long_about = "\
Fetch all entries into .skillfile/cache/ and deploy them to the directories
expected by each configured platform.

On a fresh clone, this reads Skillfile.lock and fetches the exact pinned
content. Patches from .skillfile/patches/ are applied after deployment.

Examples:
  skillfile install
  skillfile install --dry-run
  skillfile install --update      # re-resolve refs, update the lock")]
    Install {
        /// Show planned actions without fetching or installing
        #[arg(long)]
        dry_run: bool,
        /// Re-resolve all refs and update the lock
        #[arg(long)]
        update: bool,
    },

    /// Fetch entries into .skillfile/cache/ without deploying
    #[command(display_order = 22)]
    #[command(long_about = "\
Fetch community entries into .skillfile/cache/ and update Skillfile.lock,
but do not deploy to platform directories. Useful for reviewing changes
before deploying.

Examples:
  skillfile sync
  skillfile sync --dry-run
  skillfile sync --entry browser
  skillfile sync --update")]
    Sync {
        /// Show planned actions without fetching
        #[arg(long)]
        dry_run: bool,
        /// Sync only this named entry
        #[arg(long, value_name = "NAME")]
        entry: Option<String>,
        /// Re-resolve all refs and update the lock
        #[arg(long)]
        update: bool,
    },

    /// Show state of all entries
    #[command(display_order = 23)]
    #[command(long_about = "\
Show the state of every entry: locked, unlocked, pinned, or missing.

With --check-upstream, resolves the current upstream SHA for each entry
and shows whether an update is available.

Examples:
  skillfile status
  skillfile status --check-upstream")]
    Status {
        /// Check current upstream SHA (makes API calls)
        #[arg(long)]
        check_upstream: bool,
    },

    /// Show detailed information about a single entry
    #[command(display_order = 24)]
    #[command(long_about = "\
Display all known information about a single entry: source, lock state,
pin state, modified state, installed paths across all targets, and cache path.

No network calls — reads only local manifest, lock, patch, and cache state.

Examples:
  skillfile info browser
  skillfile info code-refactorer")]
    Info {
        /// Entry name to inspect
        #[arg(add = ArgValueCandidates::new(complete_entry_names))]
        name: String,
    },

    // -- Discovery (display_order 26-29) ----------------------------------------
    /// Search community registries and add skills interactively
    #[command(display_order = 26)]
    #[command(long_about = "\
Search community registries for skills and agents.

By default, queries agentskill.sh (110K+ skills, public) and skills.sh.
Use --registry to target a single registry. skillhub.club is included
automatically when SKILLHUB_API_KEY is set.

In interactive mode (the default when a terminal is attached), results
are shown in a navigable TUI with a preview pane. Selecting a result
walks you through adding it to your Skillfile via `skillfile add`.

Non-interactive output (--json, --no-interactive, or piped stdout)
prints a plain-text table or JSON without prompts.

Results are sorted by popularity (stars). The preview pane shows
description, owner, stars, security score, and source repo when
available. Security audit details are fetched on demand for
registries that support them.")]
    #[command(after_help = "\
Examples:
  skillfile search \"code review\"          Search across all registries
  skillfile search docker --limit 5        Limit to 5 results
  skillfile search linting --min-score 80  Only high-trust results
  skillfile search testing --json          Machine-readable output
  skillfile search docker --registry agentskill.sh
  skillfile search docker --no-interactive Plain text, no TUI")]
    Search {
        /// Search query
        query: String,
        /// Maximum number of results
        #[arg(long, default_value_t = 20)]
        limit: usize,
        /// Minimum security score (0-100)
        #[arg(long, value_name = "SCORE")]
        min_score: Option<u8>,
        /// Output results as JSON instead of the interactive TUI
        #[arg(long)]
        json: bool,
        /// Search only this registry
        #[arg(long, value_name = "NAME", value_parser = clap::builder::PossibleValuesParser::new(skillfile_sources::registry::REGISTRY_NAMES))]
        registry: Option<String>,
        /// Print plain-text table instead of the interactive TUI
        #[arg(long)]
        no_interactive: bool,
    },

    #[cfg(debug_assertions)]
    #[command(name = "__search-tui-test", hide = true)]
    SearchTuiTest,

    #[cfg(debug_assertions)]
    #[command(name = "__github-auth-test", hide = true)]
    GithubAuthTest,

    #[cfg(debug_assertions)]
    #[command(name = "__search-path-resolution-test", hide = true)]
    SearchPathResolutionTest,

    // -- Validation (display_order 30-39) -------------------------------------
    /// Check the Skillfile for errors
    #[command(display_order = 30)]
    #[command(long_about = "\
Parse the Skillfile and report any errors: syntax issues, unknown platforms,
duplicate entry names, orphaned lock entries, and duplicate install targets.

Examples:
  skillfile validate")]
    Validate,

    /// Format and sort entries in the Skillfile into a standard order
    #[command(display_order = 31)]
    #[command(long_about = "\
Format and canonicalize the Skillfile in-place. Entries are ordered by source
type, then entity type, then name. Install lines come first.

Examples:
  skillfile format
  skillfile format --dry-run")]
    Format {
        /// Print formatted output without writing
        #[arg(long)]
        dry_run: bool,
    },

    // -- Customization (display_order 40-49) ----------------------------------
    /// Capture local edits so they survive upstream updates
    #[command(display_order = 40)]
    #[command(long_about = "\
Diff your installed copy against the cached upstream version and save the
result as a patch in .skillfile/patches/. Future `install` commands apply
your patch after fetching upstream content.

Examples:
  skillfile pin browser
  skillfile pin browser --dry-run")]
    Pin {
        /// Entry name to pin
        #[arg(add = ArgValueCandidates::new(complete_entry_names))]
        name: String,
        /// Show what would be pinned without writing
        #[arg(long)]
        dry_run: bool,
    },

    /// Discard pinned customisations and restore upstream
    #[command(display_order = 41)]
    #[command(long_about = "\
Remove the patch for an entry from .skillfile/patches/. The next `install`
will deploy the pure upstream version.

Examples:
  skillfile unpin browser")]
    Unpin {
        /// Entry name to unpin
        #[arg(add = ArgValueCandidates::new(complete_entry_names))]
        name: String,
    },

    /// Show local changes or upstream delta after a conflict
    #[command(display_order = 42)]
    #[command(long_about = "\
Show the diff between your installed copy and the cached upstream version.
During a conflict, shows the upstream delta that triggered it.

Examples:
  skillfile diff browser")]
    Diff {
        /// Entry name
        #[arg(add = ArgValueCandidates::new(complete_entry_names))]
        name: String,
    },

    /// Merge upstream changes with your customisations after a conflict
    #[command(display_order = 43)]
    #[command(long_about = "\
When `install --update` detects that upstream changed and you have a patch,
it writes a conflict. Use `resolve` to open a three-way merge in your
configured merge tool ($MERGETOOL or $EDITOR).

Use --abort to discard the conflict state without merging.

Examples:
  skillfile resolve browser
  skillfile resolve --abort")]
    Resolve {
        /// Entry name to resolve
        #[arg(add = ArgValueCandidates::new(complete_entry_names))]
        name: Option<String>,
        /// Clear pending conflict state without merging
        #[arg(long)]
        abort: bool,
    },

    // -- Shell completions (display_order 50) ---------------------------------
    /// Generate shell completions for bash, zsh, fish, or powershell
    #[command(display_order = 50)]
    Completions {
        /// Shell to generate completions for
        #[arg(value_enum)]
        shell: clap_complete::Shell,
    },
}

#[derive(Subcommand)]
enum AddSource {
    /// Add a GitHub-hosted entry (use trailing / to discover and bulk-add)
    Github {
        /// Entity type: skill or agent
        #[arg(value_name = "TYPE", value_parser = parse_entity_type)]
        entity_type: String,
        /// GitHub repository (e.g. owner/repo or owner/repo@ref)
        #[arg(value_name = "OWNER/REPO[@REF]")]
        owner_repo: String,
        /// Path within the repo (omit to discover all entries)
        #[arg(value_name = "PATH")]
        path: Option<String>,
        /// Branch, tag, or SHA (default: main)
        #[arg(value_name = "REF")]
        ref_: Option<String>,
        /// Override name (default: filename stem)
        #[arg(long, value_name = "NAME")]
        name: Option<String>,
        /// Add all discovered entries without interactive selection
        #[arg(long)]
        no_interactive: bool,
    },
    /// Add a GitLab-hosted entry
    Gitlab {
        /// Entity type: skill or agent
        #[arg(value_name = "TYPE", value_parser = parse_entity_type)]
        entity_type: String,
        /// GitLab project path (e.g. group/project or group/subgroup/project)
        #[arg(value_name = "PROJECT")]
        owner_repo: String,
        /// Path within the repo
        #[arg(value_name = "PATH")]
        path: Option<String>,
        /// Branch, tag, or SHA (default: main)
        #[arg(value_name = "REF")]
        ref_: Option<String>,
        /// Override name (default: filename stem)
        #[arg(long, value_name = "NAME")]
        name: Option<String>,
    },
    /// Add a local file entry
    Local {
        /// Entity type: skill or agent
        #[arg(value_name = "TYPE", value_parser = parse_entity_type)]
        entity_type: String,
        /// Path to the .md file relative to repo root
        #[arg(value_name = "PATH")]
        path: String,
        /// Override name (default: filename stem)
        #[arg(long, value_name = "NAME")]
        name: Option<String>,
    },
    /// Add a URL entry
    Url {
        /// Entity type: skill or agent
        #[arg(value_name = "TYPE", value_parser = parse_entity_type)]
        entity_type: String,
        /// Direct URL to the .md file
        #[arg(value_name = "URL")]
        url: String,
        /// Override name (default: filename stem)
        #[arg(long, value_name = "NAME")]
        name: Option<String>,
    },
}

/// Returns `true` if `path` looks like a directory discovery request rather
/// than a single-file add. A path that doesn't end in `.md` is a directory.
fn is_discovery_path(path: &str) -> bool {
    path == "."
        || !std::path::Path::new(path)
            .extension()
            .is_some_and(|ext| ext.eq_ignore_ascii_case("md"))
}

type AddEntryParts<'a> = (&'a str, &'a str, &'a str, Option<&'a str>, Option<&'a str>);
type BulkAddParts<'a> = (&'a str, &'a str, &'a str, Option<&'a str>, bool);

fn github_entry_from_parts(
    (entity_type, owner_repo, path, ref_, name): AddEntryParts<'_>,
) -> skillfile_core::models::Entry {
    let (parsed_repo, parsed_ref) = parse_owner_repo_ref(owner_repo);
    let effective_ref = resolve_owner_repo_ref(parsed_ref, ref_);
    commands::add::entry_from_github(&commands::add::GithubEntryArgs {
        entity_type,
        owner_repo: &parsed_repo,
        path,
        ref_: Some(effective_ref.as_str()),
        name,
    })
}

fn gitlab_entry_from_parts(
    (entity_type, owner_repo, path, ref_, name): AddEntryParts<'_>,
) -> skillfile_core::models::Entry {
    commands::add::entry_from_gitlab(&commands::add::GitlabEntryArgs {
        entity_type,
        owner_repo,
        path,
        ref_,
        name,
    })
}

fn add_github_bulk(
    (entity_type, owner_repo, base_path, ref_, no_interactive): BulkAddParts<'_>,
    repo_root: &std::path::Path,
) -> Result<(), SkillfileError> {
    let (parsed_repo, parsed_ref) = parse_owner_repo_ref(owner_repo);
    let explicit_ref = resolve_explicit_owner_repo_ref(parsed_ref, ref_);
    commands::add::cmd_add_bulk(
        &commands::add::BulkAddArgs {
            entity_type,
            owner_repo: &parsed_repo,
            base_path,
            ref_: explicit_ref.as_deref(),
            no_interactive,
        },
        repo_root,
    )
}

fn handle_add(source: AddSource, repo_root: &std::path::Path) -> Result<(), SkillfileError> {
    let entry = match source {
        AddSource::Github {
            entity_type,
            owner_repo,
            path,
            ref_,
            name: _,
            no_interactive,
        } if is_discovery_path(path.as_deref().unwrap_or(".")) => {
            return add_github_bulk(
                (
                    &entity_type,
                    &owner_repo,
                    path.as_deref().unwrap_or("."),
                    ref_.as_deref(),
                    no_interactive,
                ),
                repo_root,
            );
        }
        AddSource::Github {
            entity_type,
            owner_repo,
            path,
            ref_,
            name,
            no_interactive: _,
        } => github_entry_from_parts((
            &entity_type,
            &owner_repo,
            path.as_deref().unwrap_or("."),
            ref_.as_deref(),
            name.as_deref(),
        )),
        AddSource::Gitlab {
            entity_type,
            owner_repo,
            path,
            ref_,
            name,
        } => gitlab_entry_from_parts((
            &entity_type,
            &owner_repo,
            path.as_deref().unwrap_or("."),
            ref_.as_deref(),
            name.as_deref(),
        )),
        AddSource::Local {
            entity_type,
            path,
            name,
        } => commands::add::entry_from_local(&entity_type, &path, name.as_deref()),
        AddSource::Url {
            entity_type,
            url,
            name,
        } => commands::add::entry_from_url(&entity_type, &url, name.as_deref()),
    };
    commands::add::cmd_add(&entry, repo_root)
}

fn run_install(repo_root: &Path, dry_run: bool, update: bool) -> Result<(), SkillfileError> {
    let user_targets = config::read_user_targets();
    let extra = if user_targets.is_empty() {
        None
    } else {
        Some(user_targets.as_slice())
    };
    skillfile_deploy::install::cmd_install(
        repo_root,
        &skillfile_deploy::install::CmdInstallOpts {
            dry_run,
            update,
            extra_targets: extra,
        },
    )
}

fn run_content_commands(repo_root: &Path, cmd: Command) -> Result<(), SkillfileError> {
    match cmd {
        Command::Completions { shell } => write_completion_registration(shell),
        Command::Validate => commands::validate::cmd_validate(repo_root),
        Command::Info { name } => commands::info::cmd_info(&name, repo_root),
        Command::Format { dry_run } => commands::format::cmd_format(repo_root, dry_run),
        Command::Pin { name, dry_run } => commands::pin::cmd_pin(&name, repo_root, dry_run),
        Command::Unpin { name } => commands::pin::cmd_unpin(&name, repo_root),
        Command::Diff { name } => commands::diff::cmd_diff(&name, repo_root),
        Command::Resolve { name, abort } => {
            commands::resolve::cmd_resolve(name.as_deref(), abort, repo_root)
        }
        cmd => run_source_commands(repo_root, cmd),
    }
}

fn run_source_commands(repo_root: &Path, cmd: Command) -> Result<(), SkillfileError> {
    match cmd {
        Command::Sync {
            dry_run,
            entry,
            update,
        } => skillfile_sources::sync::cmd_sync(&skillfile_sources::sync::SyncCmdOpts {
            repo_root,
            dry_run,
            entry_filter: entry.as_deref(),
            update,
        }),
        Command::Status { check_upstream } => {
            commands::status::cmd_status(repo_root, check_upstream)
        }
        Command::Init => commands::init::cmd_init(repo_root),
        Command::Upgrade { dry_run } => run_install(repo_root, dry_run, true),
        Command::Install { dry_run, update } => run_install(repo_root, dry_run, update),
        Command::Add {
            source: Some(source),
        } => handle_add(source, repo_root),
        Command::Add { source: None } => commands::add::cmd_add_interactive(repo_root),
        Command::Remove { name } => commands::remove::cmd_remove(&name, repo_root),
        Command::Search {
            query,
            limit,
            min_score,
            json,
            registry,
            no_interactive,
        } => commands::search::cmd_search(&commands::search::SearchConfig {
            query: &query,
            limit,
            min_score,
            json,
            registry: registry.as_deref(),
            no_interactive,
            repo_root,
        }),
        #[cfg(debug_assertions)]
        Command::SearchTuiTest => run_search_tui_test(),
        #[cfg(debug_assertions)]
        Command::GithubAuthTest => run_github_auth_test(),
        #[cfg(debug_assertions)]
        Command::SearchPathResolutionTest => {
            commands::search::run_search_path_resolution_regression()
        }
        _ => Ok(()), // covered by run_content_commands
    }
}

#[cfg(debug_assertions)]
fn run_search_tui_test() -> Result<(), SkillfileError> {
    use skillfile_sources::registry::{RegistryId, SearchResult};

    let items = [SearchResult {
        name: "fixture-skill".to_string(),
        owner: "skillfile".to_string(),
        description: Some("Fixture result for interactive terminal smoke tests.".to_string()),
        security_score: Some(92),
        stars: Some(42),
        url: "https://example.com/fixture-skill".to_string(),
        registry: RegistryId::AgentskillSh,
        source_repo: Some("eljulians/skillfile".to_string()),
        source_path: Some("skills/fixture-skill/SKILL.md".to_string()),
    }];

    commands::search_tui::run_tui(&items, items.len())
        .map(|_| ())
        .map_err(|e| SkillfileError::Install(format!("TUI error: {e}")))
}

#[cfg(debug_assertions)]
fn run_github_auth_test() -> Result<(), SkillfileError> {
    if skillfile_sources::http::github_token()
        .for_url("https://api.github.com/user")
        .is_some()
    {
        println!("available");
        Ok(())
    } else {
        Err(SkillfileError::Install("github auth unavailable".into()))
    }
}

fn run() -> Result<(), SkillfileError> {
    // Inject config-file token before any command (and before the OnceLock is
    // populated by `github_token()`). This runs once; subsequent calls are no-ops.
    skillfile_sources::http::set_config_token(crate::config::read_config_token());
    skillfile_sources::http::set_gitlab_config_token(crate::config::read_gitlab_config_token());
    skillfile_sources::http::set_gitlab_config_host(crate::config::read_gitlab_config_host());

    let cli = match cli_command().try_get_matches() {
        Ok(matches) => Cli::from_arg_matches(&matches).unwrap_or_else(|e| e.exit()),
        Err(e)
            if e.kind() == clap::error::ErrorKind::DisplayHelp
                || e.kind() == clap::error::ErrorKind::DisplayVersion =>
        {
            let _ = e.print();
            return Ok(());
        }
        Err(e) => {
            e.exit();
        }
    };
    let quiet = cli.quiet || std::env::var("SKILLFILE_QUIET").is_ok_and(|v| !v.is_empty());
    skillfile_core::output::set_quiet(quiet);
    let repo_root = PathBuf::from(".");
    run_content_commands(&repo_root, cli.command)
}

fn main() {
    // Handle dynamic shell completion requests before any other initialization.
    // Shells call the binary with COMPLETE=<shell> to get completions at runtime.
    clap_complete::CompleteEnv::with_factory(cli_command).complete();

    // Spawn background update check (non-blocking)
    let update_rx = update_check::should_check().then(update_check::spawn_check);

    let exit_code = match run() {
        Ok(()) => 0,
        Err(e) => {
            let msg = e.to_string();
            if !msg.is_empty() {
                eprintln!("error: {msg}");
            }
            1
        }
    };

    // Print update notice if the background check found a newer version.
    // Give the background thread a short window to finish if it hasn't yet.
    if let Some(rx) = update_rx {
        if let Ok(Some(notice)) = rx.recv_timeout(std::time::Duration::from_secs(2)) {
            eprintln!("\n{notice}");
        }
    }

    if exit_code != 0 {
        process::exit(exit_code);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use clap::ValueEnum;
    use std::path::Path;

    fn completions_non_empty(shell: clap_complete::Shell) {
        let mut buf = Vec::new();
        clap_complete::generate(shell, &mut cli_command(), "skillfile", &mut buf);
        assert!(
            !buf.is_empty(),
            "completions for {shell:?} should produce output"
        );
    }

    #[test]
    fn long_about_lists_dynamic_supported_platforms() {
        let long_about = cli_command().get_long_about().unwrap().to_string();
        assert!(long_about.contains("antigravity"));
        for name in known_adapters() {
            assert!(long_about.contains(name), "missing adapter: {name}");
        }
    }

    #[test]
    fn completions_bash() {
        completions_non_empty(clap_complete::Shell::Bash);
    }

    #[test]
    fn completions_zsh() {
        completions_non_empty(clap_complete::Shell::Zsh);
    }

    #[test]
    fn completions_fish() {
        completions_non_empty(clap_complete::Shell::Fish);
    }

    #[test]
    fn completions_powershell() {
        completions_non_empty(clap_complete::Shell::PowerShell);
    }

    #[test]
    fn completion_env_shell_names_match_clap() {
        for shell in clap_complete::Shell::value_variants() {
            assert_eq!(completion_env_shell(*shell), shell.to_string());
        }
    }

    #[test]
    fn completion_registration_output_reports_spawn_failure() {
        let err = completion_registration_output(Path::new("/definitely/missing-skillfile"), "zsh")
            .expect_err("missing completer should fail");
        let msg = err.to_string();
        assert!(msg.contains("failed to generate shell completions"));
    }

    #[test]
    fn completion_registration_completer_from_prefers_windows_cli_binary() {
        let dir = tempfile::tempdir().expect("tempdir should be created");
        let profile_dir = dir.path().join("target/debug");
        let deps_dir = profile_dir.join("deps");
        std::fs::create_dir_all(&deps_dir).expect("deps dir should be created");

        let test_exe = deps_dir.join("skillfile-abc123.exe");
        std::fs::write(&test_exe, "").expect("test harness placeholder should be written");

        let cli_binary = profile_dir.join("skillfile.exe");
        std::fs::write(&cli_binary, "").expect("cli placeholder should be written");

        let resolved =
            completion_registration_completer_from(test_exe, std::ffi::OsStr::new("skillfile.exe"));

        assert_eq!(resolved, cli_binary);
    }

    #[test]
    fn completion_registration_completer_from_falls_back_when_cli_binary_is_missing() {
        let dir = tempfile::tempdir().expect("tempdir should be created");
        let profile_dir = dir.path().join("target/debug");
        let deps_dir = profile_dir.join("deps");
        std::fs::create_dir_all(&deps_dir).expect("deps dir should be created");

        let test_exe = deps_dir.join("skillfile-abc123.exe");
        std::fs::write(&test_exe, "").expect("test harness placeholder should be written");

        let resolved = completion_registration_completer_from(
            test_exe.clone(),
            std::ffi::OsStr::new("skillfile.exe"),
        );

        assert_eq!(resolved, test_exe);
    }
}