skillfile 1.4.2

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
use std::io::{self, IsTerminal, Write};
use std::path::Path;
use std::process::Command;

use skillfile_core::error::SkillfileError;
use skillfile_core::models::EntityType;
use skillfile_core::parser::{parse_manifest, MANIFEST_NAME};
use skillfile_deploy::adapter::{adapters, known_adapters};

const GITIGNORE_ENTRIES: &[&str] = &[".skillfile/cache/", ".skillfile/conflict"];

// ---------------------------------------------------------------------------
// Pure helpers (no IO — fully testable)
// ---------------------------------------------------------------------------

/// Build a new manifest string with install lines replaced.
/// Pure transformation: takes existing content and new targets, returns new content.
fn build_manifest_with_targets(existing: &str, new_targets: &[(String, String)]) -> String {
    let mut non_install: Vec<&str> = existing
        .lines()
        .filter(|line| {
            let stripped = line.trim();
            !stripped.starts_with("install ") && stripped != "install"
        })
        .collect();

    // Strip leading blank lines from remaining content
    while non_install.first().is_some_and(|l| l.trim().is_empty()) {
        non_install.remove(0);
    }

    let mut output = String::new();
    for (adapter, scope) in new_targets {
        use std::fmt::Write as _;
        let _ = writeln!(output, "install  {adapter}  {scope}");
    }
    output.push('\n');
    for line in &non_install {
        output.push_str(line);
        output.push('\n');
    }

    output
}

fn gitignore_additions(existing: &str) -> Option<String> {
    let lines: Vec<&str> = existing.lines().collect();

    let missing: Vec<&&str> = GITIGNORE_ENTRIES
        .iter()
        .filter(|e| !lines.iter().any(|l| l == *e))
        .collect();

    if missing.is_empty() {
        return None;
    }

    let mut additions = String::new();
    if !lines.is_empty() && lines.last().is_some_and(|l| !l.is_empty()) {
        additions.push('\n');
    }
    additions.push_str("# skillfile\n");
    for entry in &missing {
        additions.push_str(entry);
        additions.push('\n');
    }

    Some(additions)
}

// ---------------------------------------------------------------------------
// IO wrappers
// ---------------------------------------------------------------------------

fn rewrite_install_lines(
    manifest_path: &Path,
    new_targets: &[(String, String)],
) -> Result<(), SkillfileError> {
    let text = std::fs::read_to_string(manifest_path)?;
    let output = build_manifest_with_targets(&text, new_targets);
    std::fs::write(manifest_path, &output)?;
    Ok(())
}

fn update_gitignore(repo_root: &Path) -> Result<(), SkillfileError> {
    let gitignore = repo_root.join(".gitignore");
    let existing = if gitignore.exists() {
        std::fs::read_to_string(&gitignore)?
    } else {
        String::new()
    };

    if let Some(additions) = gitignore_additions(&existing) {
        let mut file = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&gitignore)?;
        write!(file, "{additions}")?;
    }

    Ok(())
}

fn supported_types_hint(adapter_name: &str) -> &'static str {
    let reg = adapters();
    match reg.get(adapter_name) {
        Some(a) => match (a.supports(EntityType::Skill), a.supports(EntityType::Agent)) {
            (true, true) => "skill, agent",
            (true, false) => "skill only",
            (false, true) => "agent only",
            _ => "",
        },
        None => "",
    }
}

fn write_personal_config(new_targets: &[(String, String)]) -> Result<(), SkillfileError> {
    use skillfile_core::models::{InstallTarget, Scope};
    let targets: Vec<InstallTarget> = new_targets
        .iter()
        .map(|(a, s)| InstallTarget {
            adapter: a.clone(),
            scope: Scope::parse(s).unwrap_or(Scope::Local),
        })
        .collect();
    crate::config::write_user_targets(&targets)?;
    Ok(())
}

fn select_platforms_and_scope(
    existing_set: &std::collections::HashSet<&str>,
) -> Result<Option<Vec<(String, String)>>, SkillfileError> {
    let adapter_names = known_adapters();
    let mut multi =
        cliclack::multiselect("Select platforms to install to (space to toggle, enter to confirm)");
    for name in &adapter_names {
        multi = multi.item(*name, *name, supported_types_hint(name));
    }

    let initial: Vec<&str> = adapter_names
        .iter()
        .copied()
        .filter(|n| existing_set.contains(n))
        .collect();
    if !initial.is_empty() {
        multi = multi.initial_values(initial);
    }

    let selected: Vec<&str> = multi.interact()?;

    if selected.is_empty() {
        return Ok(None);
    }

    let scope: &str = cliclack::select("Default scope for selected platforms?")
        .item("local", "local", "project-specific")
        .item("global", "global", "user-wide (~/.tool/)")
        .item("both", "both", "add global and local for each platform")
        .interact()?;

    let targets = if scope == "both" {
        selected
            .iter()
            .flat_map(|p| {
                [
                    (p.to_string(), "global".to_string()),
                    (p.to_string(), "local".to_string()),
                ]
            })
            .collect()
    } else {
        selected
            .iter()
            .map(|p| (p.to_string(), scope.to_string()))
            .collect()
    };

    Ok(Some(targets))
}

fn select_destination() -> Result<&'static str, SkillfileError> {
    let config_location = crate::config::config_path().map_or_else(
        || "~/.config/skillfile/config.toml".into(),
        |p| p.display().to_string(),
    );
    let destination: &str = cliclack::select(
        "Where should platform config be stored?\n\
         Tip: In shared repos, personal config avoids merge conflicts when\n\
         teammates use different AI tools.\n\
         Precedence: Skillfile targets always override personal config.",
    )
    .item(
        "personal",
        "Personal config (recommended for shared repos)",
        format!("saved to {config_location} — each developer picks their own platforms"),
    )
    .item(
        "skillfile",
        "Skillfile (shared with team)",
        "committed to git, visible to all collaborators",
    )
    .interact()?;
    Ok(destination)
}

// ---------------------------------------------------------------------------
// GitHub token setup helpers
// ---------------------------------------------------------------------------

/// Check for an existing token via env vars, config file, or `gh` CLI directly.
/// Does NOT use the `OnceLock`-cached `github_token()` — that may already be
/// populated with `None` by the time init runs.
fn detect_existing_token() -> bool {
    let has_env = std::env::var("GITHUB_TOKEN")
        .or_else(|_| std::env::var("GH_TOKEN"))
        .is_ok_and(|t| !t.is_empty());
    if has_env {
        return true;
    }
    if crate::config::read_config_token().is_some() {
        return true;
    }
    Command::new("gh")
        .args(["auth", "token"])
        .output()
        .is_ok_and(|o| o.status.success() && !o.stdout.is_empty())
}

fn gh_available() -> bool {
    Command::new("gh")
        .arg("--version")
        .output()
        .is_ok_and(|o| o.status.success())
}

fn validate_token(token: &str) -> bool {
    ureq::Agent::new_with_defaults()
        .get("https://api.github.com/user")
        .header("Authorization", &format!("Bearer {token}"))
        .header("User-Agent", "skillfile/1.0")
        .call()
        .is_ok_and(|r| r.status() == 200)
}

fn handle_paste_token() -> Result<(), SkillfileError> {
    let token: String =
        cliclack::password("Paste your GitHub personal access token:").interact()?;
    if validate_token(&token) {
        crate::config::write_config_token(&token)?;
        cliclack::log::success("Token saved to config (0o600)")?;
    } else {
        cliclack::log::warning(
            "Token validation failed — not saved. You can set GITHUB_TOKEN manually.",
        )?;
    }
    Ok(())
}

fn handle_gh_cli() -> Result<(), SkillfileError> {
    cliclack::log::info("Run `gh auth login` in another terminal, then press Enter.")?;
    let mut line = String::new();
    io::stdin().read_line(&mut line)?;
    if detect_existing_token() {
        cliclack::log::success("GitHub token found via gh CLI")?;
    } else {
        cliclack::log::warning("Still no token detected. You can set GITHUB_TOKEN manually.")?;
    }
    Ok(())
}

/// Interactive GitHub token setup step for `skillfile init`.
///
/// Skips when a token is already available. Otherwise presents options for gh
/// CLI auth, pasting a token, or skipping (with a rate-limit warning).
fn setup_github_token() -> Result<(), SkillfileError> {
    if detect_existing_token() {
        cliclack::log::success("GitHub token found")?;
        return Ok(());
    }

    let show_gh = gh_available();
    let mut select = cliclack::select("No GitHub token found. How would you like to authenticate?");
    if show_gh {
        select = select.item(
            "gh",
            "Use gh CLI",
            "run `gh auth login` in another terminal",
        );
    }
    select = select
        .item("paste", "Paste a token", "github.com/settings/tokens")
        .item("skip", "Skip", "unauthenticated: 60 req/hr limit");

    let choice: &str = select.interact()?;
    match choice {
        "gh" => handle_gh_cli(),
        "paste" => handle_paste_token(),
        _ => {
            cliclack::log::warning(
                "Skipping token setup. GitHub API limited to 60 req/hr without a token.",
            )?;
            Ok(())
        }
    }
}

// ---------------------------------------------------------------------------
// Public entry point — interactive cliclack flow
// ---------------------------------------------------------------------------

/// Write targets to manifest or personal config and print a summary note.
fn persist_targets(
    manifest_path: &Path,
    destination: &str,
    new_targets: &[(String, String)],
) -> Result<(), SkillfileError> {
    let summary: Vec<String> = new_targets
        .iter()
        .map(|(a, s)| format!("install  {a}  {s}"))
        .collect();

    if destination == "personal" {
        write_personal_config(new_targets)?;
        cliclack::note(
            "Install config written to personal config",
            summary.join("\n"),
        )?;
    } else {
        rewrite_install_lines(manifest_path, new_targets)?;
        cliclack::note("Install config written to Skillfile", summary.join("\n"))?;
    }
    Ok(())
}

pub fn cmd_init(repo_root: &Path) -> Result<(), SkillfileError> {
    // TTY guard: cliclack requires an interactive terminal. Check stdin, stdout,
    // and the CI env var because some CI runners (macOS GitHub Actions) report
    // piped fds as TTY.
    let is_ci = std::env::var("CI").is_ok();
    if is_ci || !io::stdin().is_terminal() || !io::stdout().is_terminal() {
        return Err(SkillfileError::Manifest(
            "skillfile init requires an interactive terminal.\n\
             Use `skillfile add` for scripted/CI usage."
                .into(),
        ));
    }

    cliclack::intro(console::style(" skillfile init ").on_cyan().black())?;

    // Create Skillfile if missing
    let manifest_path = repo_root.join(MANIFEST_NAME);
    if !manifest_path.exists() {
        std::fs::write(&manifest_path, "")?;
        cliclack::log::info(format!("Created {MANIFEST_NAME}"))?;
    }

    // Parse existing manifest
    let result = parse_manifest(&manifest_path)?;
    let existing = &result.manifest.install_targets;
    let user_targets = crate::config::read_user_targets();

    // Show existing config
    let existing_set: std::collections::HashSet<&str> = existing
        .iter()
        .chain(user_targets.iter())
        .map(|t| t.adapter.as_str())
        .collect();

    if !existing.is_empty() || !user_targets.is_empty() {
        let mut lines: Vec<String> = existing
            .iter()
            .map(|t| format!("install  {}  {}  (Skillfile)", t.adapter, t.scope))
            .collect();
        for t in &user_targets {
            lines.push(format!(
                "install  {}  {}  (personal config)",
                t.adapter, t.scope
            ));
        }
        cliclack::note("Existing config", lines.join("\n"))?;
    }

    // Platform + scope selection
    let Some(new_targets) = select_platforms_and_scope(&existing_set)? else {
        cliclack::outro_cancel("No platforms selected.")?;
        return Ok(());
    };

    let destination = select_destination()?;
    persist_targets(&manifest_path, destination, &new_targets)?;
    setup_github_token()?;
    update_gitignore(repo_root)?;

    let outro = if result.manifest.entries.is_empty() {
        "You're all set! Next up:".to_string()
    } else {
        let n = result.manifest.entries.len();
        let word = if n == 1 { "entry" } else { "entries" };
        format!(
            "Platforms configured! This Skillfile already has {n} {word}.\n  \
                 \u{1f680} Run `skillfile install` to fetch and deploy them."
        )
    };
    cliclack::outro(format!(
        "{outro}\n  \
         \u{2795} `skillfile add` to add a skill or agent\n  \
         \u{1f50d} `skillfile search` to discover community skills"
    ))?;

    Ok(())
}

// ---------------------------------------------------------------------------
// Unit tests — pure functions only, no IO
// ---------------------------------------------------------------------------

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

    // -- build_manifest_with_targets --

    #[test]
    fn writes_install_lines() {
        let result = build_manifest_with_targets("", &[("claude-code".into(), "global".into())]);
        assert!(result.contains("install  claude-code  global"));
    }

    #[test]
    fn install_lines_at_top() {
        let result = build_manifest_with_targets(
            "local  skill  skills/foo.md\n",
            &[("claude-code".into(), "global".into())],
        );
        let lines: Vec<&str> = result.lines().collect();
        assert_eq!(lines[0], "install  claude-code  global");
    }

    #[test]
    fn preserves_existing_entries() {
        let result = build_manifest_with_targets(
            "local  skill  skills/foo.md\n",
            &[("claude-code".into(), "local".into())],
        );
        assert!(result.contains("local  skill  skills/foo.md"));
        assert!(result.contains("install  claude-code  local"));
    }

    #[test]
    fn multiple_adapters() {
        let result = build_manifest_with_targets(
            "",
            &[
                ("claude-code".into(), "global".into()),
                ("gemini-cli".into(), "local".into()),
            ],
        );
        assert!(result.contains("install  claude-code  global"));
        assert!(result.contains("install  gemini-cli  local"));
    }

    #[test]
    fn replaces_existing_install_targets() {
        let result = build_manifest_with_targets(
            "install  claude-code  global\nlocal  skill  skills/foo.md\n",
            &[("gemini-cli".into(), "local".into())],
        );
        assert!(!result.contains("claude-code"));
        assert!(result.contains("install  gemini-cli  local"));
        assert!(result.contains("local  skill  skills/foo.md"));
    }

    #[test]
    fn strips_leading_blanks_after_install_removal() {
        let result = build_manifest_with_targets(
            "install  old  global\n\n\nlocal  skill  keep.md\n",
            &[("new".into(), "local".into())],
        );
        let lines: Vec<&str> = result.lines().collect();
        assert_eq!(lines[0], "install  new  local");
        assert_eq!(lines[1], "");
        assert_eq!(lines[2], "local  skill  keep.md");
    }

    // -- gitignore_additions --

    #[test]
    fn gitignore_from_empty() {
        let additions = gitignore_additions("");
        let text = additions.unwrap();
        assert!(text.contains(".skillfile/cache/"));
        assert!(text.contains(".skillfile/conflict"));
    }

    #[test]
    fn gitignore_idempotent() {
        let existing = "# skillfile\n.skillfile/cache/\n.skillfile/conflict\n";
        assert!(gitignore_additions(existing).is_none());
    }

    #[test]
    fn gitignore_does_not_include_patches() {
        let text = gitignore_additions("").unwrap();
        assert!(!text.contains("patches"));
    }

    #[test]
    fn gitignore_appends_only_missing_entries() {
        let text = gitignore_additions("# skillfile\n.skillfile/cache/\n").unwrap();
        assert!(text.contains(".skillfile/conflict"));
        assert!(!text.contains(".skillfile/cache/"));
    }

    #[test]
    fn gitignore_adds_blank_separator_after_content() {
        let text = gitignore_additions("node_modules/").unwrap();
        assert!(text.starts_with('\n'), "should add blank line separator");
    }

    #[test]
    fn gitignore_no_blank_separator_after_trailing_blank_line() {
        // File already ends with a blank line — don't double up.
        let text = gitignore_additions("node_modules/\n\n").unwrap();
        assert!(!text.starts_with('\n'), "should not double-blank");
    }

    // -- supported_types_hint --

    #[test]
    fn hint_for_full_adapter() {
        assert_eq!(supported_types_hint("claude-code"), "skill, agent");
    }

    #[test]
    fn hint_for_skill_only_adapter() {
        assert_eq!(supported_types_hint("codex"), "skill only");
    }

    #[test]
    fn hint_for_unknown_adapter() {
        assert_eq!(supported_types_hint("nonexistent"), "");
    }
}