skillflag 0.1.0

Rust implementation of the Skillflag producer library/CLI and the skill-install companion CLI.
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
//! The `skill-install` CLI.
//!
//! Mirrors `typescript/src/install/cli.ts`, with a deliberately simpler,
//! dependency-free interactive fallback: when `--agent` or `--scope` is
//! missing and stdin is a TTY, a numbered prompt (written to stderr) collects
//! the missing values. `/dev/tty` is never opened.

use std::cell::RefCell;
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};

use crate::core::errors::{Result, SkillflagError};
use crate::core::paths::resolve_path;
use crate::install::install::{install_skill, InstallInput, InstallOptions};
use crate::install::resolve::{
    assert_agent, assert_scope, assert_supported_agent_scopes, resolve_skills_root,
    supported_scopes_for_agent, EnvLookup, SystemEnv, AGENTS, SCOPES,
};
use crate::install::validate::{assert_skill_dir, read_skill_metadata};
use crate::stdio::{drain, read_line, InputStream, StdinStream};

/// Options for [`run_install_cli`]. Streams default to the process stdio;
/// `provided_inputs`/`provided_skill_ids` let a producer CLI hand pre-built
/// tar bundles to the installer (used by `--skill install`).
#[derive(Default)]
pub struct InstallCliOptions {
    pub stdin: Option<RefCell<Box<dyn InputStream>>>,
    pub stdout: Option<RefCell<Box<dyn Write>>>,
    pub stderr: Option<RefCell<Box<dyn Write>>>,
    pub cwd: Option<PathBuf>,
    pub provided_inputs: Vec<InstallInput>,
    pub provided_skill_ids: Vec<String>,
    pub env: Option<Box<dyn EnvLookup>>,
}

#[derive(Debug)]
struct ParsedArgs {
    input_paths: Vec<String>,
    agents: Vec<String>,
    scopes: Vec<String>,
    force: bool,
    help: bool,
}

struct PreparedSource {
    source: String,
    skill_id_hint: String,
    input: InstallInput,
}

struct PlanItem {
    source_index: usize,
    agent: String,
    scope: String,
    destination: PathBuf,
}

pub(crate) fn usage_text() -> String {
    let agent_list = AGENTS.join(", ");
    let scope_list = SCOPES.join(", ");
    [
        "Usage:",
        "  skill-install [PATH ...] [--agent <agent>] [--scope <scope>] [--force]",
        "",
        "Input:",
        "  PATH ...            Skill directory path(s) containing SKILL.md.",
        "  stdin tar stream    If PATH is omitted, reads a tar bundle from stdin.",
        "",
        "Options:",
        "  --agent <value>     Target agent (single value).",
        &format!("                      Supported agents: {agent_list}"),
        "  --scope <value>     Target scope (single value).",
        &format!("                      Supported scopes: {scope_list}"),
        "  --force             Overwrite destination if it already exists.",
        "  -h, --help          Show this help.",
        "",
        "Behavior:",
        "  If --agent or --scope is missing and an interactive TTY is available,",
        "  the installer launches a wizard to collect missing values.",
        "  CLI flags accept only one --agent and one --scope.",
        "  Use the wizard to select multiple agents/scopes.",
    ]
    .join("\n")
}

fn parse_flag_value(value: Option<&String>, flag: &str) -> Result<String> {
    let missing = || SkillflagError::new(format!("Missing value for --{flag}."));
    let value = value.ok_or_else(missing)?;
    if value.starts_with('-') {
        return Err(missing());
    }
    let value = value.trim();
    if value.is_empty() {
        return Err(missing());
    }
    if value.contains(',') {
        return Err(SkillflagError::new(format!(
            "Only one value is allowed for --{flag}. Comma-separated values are not supported."
        )));
    }
    Ok(value.to_string())
}

fn parse_args(args: &[String]) -> Result<ParsedArgs> {
    let mut input_paths = Vec::new();
    let mut agent_value: Option<String> = None;
    let mut scope_value: Option<String> = None;
    let mut force = false;
    let mut help = false;

    let mut index = 0;
    while index < args.len() {
        let arg = &args[index];
        if arg == "--agent" {
            if agent_value.is_some() {
                return Err(SkillflagError::new("Only one --agent flag is allowed."));
            }
            agent_value = Some(parse_flag_value(args.get(index + 1), "agent")?);
            index += 2;
            continue;
        }
        if arg == "--scope" {
            if scope_value.is_some() {
                return Err(SkillflagError::new("Only one --scope flag is allowed."));
            }
            scope_value = Some(parse_flag_value(args.get(index + 1), "scope")?);
            index += 2;
            continue;
        }
        if arg == "--force" {
            force = true;
            index += 1;
            continue;
        }
        if arg == "--help" || arg == "-h" {
            help = true;
            index += 1;
            continue;
        }
        if arg.starts_with('-') {
            return Err(SkillflagError::new(format!("Unknown option: {arg}")));
        }
        input_paths.push(arg.clone());
        index += 1;
    }

    Ok(ParsedArgs {
        input_paths,
        agents: agent_value.into_iter().collect(),
        scopes: scope_value.into_iter().collect(),
        force,
        help,
    })
}

/// Numbered selection prompt on stderr; accepts a number or a literal value.
fn prompt_select(
    stdin: &mut dyn InputStream,
    stderr: &mut dyn Write,
    title: &str,
    label: &str,
    choices: &[&str],
) -> Result<String> {
    writeln!(stderr, "{title}")?;
    for (index, choice) in choices.iter().enumerate() {
        writeln!(stderr, "  {}) {choice}", index + 1)?;
    }
    write!(stderr, "{label} (number or name): ")?;
    stderr.flush()?;
    let line = read_line(stdin)?.ok_or_else(|| SkillflagError::new("Install cancelled."))?;
    let value = line.trim();
    if value.is_empty() {
        return Err(SkillflagError::new("Install cancelled."));
    }
    if let Ok(number) = value.parse::<usize>() {
        if number >= 1 && number <= choices.len() {
            return Ok(choices[number - 1].to_string());
        }
        return Err(SkillflagError::new(format!("Invalid selection: {value}")));
    }
    Ok(value.to_string())
}

fn prompt_missing_flags(
    parsed: &ParsedArgs,
    stdin: &mut dyn InputStream,
    stderr: &mut dyn Write,
) -> Result<(String, String)> {
    let agent = match parsed.agents.first() {
        Some(agent) => assert_agent(agent)?.to_string(),
        None => {
            let value = prompt_select(stdin, stderr, "Select an agent:", "Agent", &AGENTS)?;
            assert_agent(&value)?.to_string()
        }
    };
    let scope = match parsed.scopes.first() {
        Some(scope) => assert_scope(scope)?.to_string(),
        None => {
            let scopes = supported_scopes_for_agent(&agent);
            let value = prompt_select(stdin, stderr, "Select a scope:", "Scope", scopes)?;
            assert_scope(&value)?.to_string()
        }
    };
    Ok((agent, scope))
}

fn prepare_dir_source(path: &str, cwd: &Path) -> Result<PreparedSource> {
    let source_dir = resolve_path(cwd, Path::new(path));
    let metadata = fs::metadata(&source_dir).map_err(|_| {
        SkillflagError::new(format!("PATH does not exist: {}", source_dir.display()))
    })?;
    if !metadata.is_dir() {
        return Err(SkillflagError::new(
            "PATH must be a directory containing SKILL.md.",
        ));
    }
    assert_skill_dir(&source_dir)?;
    let meta = read_skill_metadata(&source_dir)?;
    Ok(PreparedSource {
        source: source_dir.display().to_string(),
        skill_id_hint: meta.name,
        input: InstallInput::Dir(source_dir),
    })
}

fn resolve_install_sources(
    input_paths: &[String],
    provided_inputs: &[InstallInput],
    provided_skill_ids: &[String],
    stdin: &mut dyn InputStream,
    cwd: &Path,
) -> Result<Vec<PreparedSource>> {
    if !input_paths.is_empty() && !provided_inputs.is_empty() {
        return Err(SkillflagError::new(
            "PATH cannot be used when install input is preset.",
        ));
    }

    if !input_paths.is_empty() {
        return input_paths
            .iter()
            .map(|path| prepare_dir_source(path, cwd))
            .collect();
    }

    if !provided_inputs.is_empty() {
        return provided_inputs
            .iter()
            .enumerate()
            .map(|(index, input)| match input {
                InstallInput::Dir(dir) => prepare_dir_source(&dir.display().to_string(), cwd),
                InstallInput::Tar(bytes) => Ok(PreparedSource {
                    source: "tar stream".to_string(),
                    skill_id_hint: provided_skill_ids
                        .get(index)
                        .cloned()
                        .unwrap_or_else(|| "<from skill bundle>".to_string()),
                    input: InstallInput::Tar(bytes.clone()),
                }),
            })
            .collect();
    }

    if !stdin.is_tty() {
        let mut bytes = Vec::new();
        stdin.read_to_end(&mut bytes)?;
        return Ok(vec![PreparedSource {
            source: "tar stream".to_string(),
            skill_id_hint: "<from skill bundle>".to_string(),
            input: InstallInput::Tar(bytes),
        }]);
    }

    Err(SkillflagError::new(format!(
        "Missing PATH or tar stream on stdin.\n{}",
        usage_text()
    )))
}

fn build_install_plan(
    sources: &[PreparedSource],
    agents: &[String],
    scopes: &[String],
    cwd: &Path,
    env: &dyn EnvLookup,
) -> Result<Vec<PlanItem>> {
    let mut plan = Vec::new();
    for (source_index, source) in sources.iter().enumerate() {
        for agent in agents {
            for scope in scopes {
                let skills_root = resolve_skills_root(agent, scope, cwd, env)?;
                plan.push(PlanItem {
                    source_index,
                    agent: agent.clone(),
                    scope: scope.clone(),
                    destination: skills_root.join(&source.skill_id_hint),
                });
            }
        }
    }
    Ok(plan)
}

fn assert_no_install_collisions(plan: &[PlanItem], sources: &[PreparedSource]) -> Result<()> {
    let mut by_destination: Vec<(String, Vec<&PlanItem>)> = Vec::new();
    for item in plan {
        let key = item.destination.display().to_string();
        match by_destination.iter_mut().find(|(dest, _)| *dest == key) {
            Some((_, items)) => items.push(item),
            None => by_destination.push((key, vec![item])),
        }
    }

    let mut collisions: Vec<&(String, Vec<&PlanItem>)> = by_destination
        .iter()
        .filter(|(_, items)| items.len() > 1)
        .collect();
    if collisions.is_empty() {
        return Ok(());
    }
    collisions.sort_by(|a, b| a.0.cmp(&b.0));

    let mut lines = vec!["Install destination collisions detected:".to_string()];
    for (destination, items) in collisions {
        lines.push(format!("- {destination}"));
        for item in items {
            let source = &sources[item.source_index];
            lines.push(format!(
                "  - {} @ {}/{} (source: {})",
                source.skill_id_hint, item.agent, item.scope, source.source
            ));
        }
    }
    lines.push(
        "Resolve collisions by changing skill IDs, sources, --agent, or --scope so each combination has a unique destination."
            .to_string(),
    );
    Err(SkillflagError::new(lines.join("\n")))
}

#[allow(clippy::too_many_arguments)]
fn run_inner(
    args: &[String],
    stdin: &mut dyn InputStream,
    stdout: &mut dyn Write,
    stderr: &mut dyn Write,
    cwd: &Path,
    provided_inputs: &[InstallInput],
    provided_skill_ids: &[String],
    env: &dyn EnvLookup,
) -> Result<i32> {
    let parsed = parse_args(args)?;
    if parsed.help {
        writeln!(stdout, "{}", usage_text())?;
        if !stdin.is_tty() {
            drain(stdin);
        }
        return Ok(0);
    }

    if !provided_inputs.is_empty() && !parsed.input_paths.is_empty() {
        return Err(SkillflagError::new(
            "PATH cannot be used when install input is preset.",
        ));
    }
    if !provided_skill_ids.is_empty() && provided_inputs.is_empty() {
        return Err(SkillflagError::new(
            "Preset skill ids require preset install inputs.",
        ));
    }
    if !provided_skill_ids.is_empty() && provided_skill_ids.len() != provided_inputs.len() {
        return Err(SkillflagError::new(
            "Preset skill id count must match preset install input count.",
        ));
    }

    let (agents, scopes): (Vec<String>, Vec<String>) =
        if parsed.agents.is_empty() || parsed.scopes.is_empty() {
            if stdin.is_tty() {
                let (agent, scope) = prompt_missing_flags(&parsed, stdin, stderr)?;
                (vec![agent], vec![scope])
            } else {
                return Err(SkillflagError::new(format!(
                    "Missing required flags.\n{}",
                    usage_text()
                )));
            }
        } else {
            let agents: Vec<String> = parsed
                .agents
                .iter()
                .map(|agent| assert_agent(agent).map(str::to_string))
                .collect::<Result<_>>()?;
            let scopes: Vec<String> = parsed
                .scopes
                .iter()
                .map(|scope| assert_scope(scope).map(str::to_string))
                .collect::<Result<_>>()?;
            (agents, scopes)
        };
    assert_supported_agent_scopes(&agents, &scopes)?;

    let sources = resolve_install_sources(
        &parsed.input_paths,
        provided_inputs,
        provided_skill_ids,
        stdin,
        cwd,
    )?;
    let plan = build_install_plan(&sources, &agents, &scopes, cwd, env)?;
    assert_no_install_collisions(&plan, &sources)?;

    let mut results = Vec::with_capacity(plan.len());
    for item in &plan {
        let result = install_skill(
            &sources[item.source_index].input,
            &InstallOptions {
                agent: &item.agent,
                scope: &item.scope,
                cwd,
                force: parsed.force,
                env,
            },
        )?;
        results.push((result, &item.agent, &item.scope));
    }

    for (result, agent, scope) in &results {
        writeln!(
            stderr,
            "Installed {} to {} ({agent}/{scope})",
            result.skill_id,
            result.installed_to.display()
        )?;
    }
    Ok(0)
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn run_install_cli_streams(
    args: &[String],
    stdin: &mut dyn InputStream,
    stdout: &mut dyn Write,
    stderr: &mut dyn Write,
    cwd: &Path,
    provided_inputs: &[InstallInput],
    provided_skill_ids: &[String],
    env: &dyn EnvLookup,
) -> i32 {
    let code = match run_inner(
        args,
        stdin,
        stdout,
        stderr,
        cwd,
        provided_inputs,
        provided_skill_ids,
        env,
    ) {
        Ok(code) => code,
        Err(err) => {
            if !stdin.is_tty() {
                drain(stdin);
            }
            let _ = writeln!(stderr, "{}", err.message);
            err.exit_code
        }
    };
    let _ = stdout.flush();
    let _ = stderr.flush();
    code
}

/// Run the installer CLI. `args` are the CLI arguments *after* the program
/// name. Returns the process exit code (0 success, 1 any error).
pub fn run_install_cli(args: &[String], opts: &InstallCliOptions) -> i32 {
    let mut stdin_guard;
    let mut default_stdin;
    let stdin: &mut dyn InputStream = match &opts.stdin {
        Some(cell) => {
            stdin_guard = cell.borrow_mut();
            &mut **stdin_guard
        }
        None => {
            default_stdin = StdinStream::new();
            &mut default_stdin
        }
    };
    let mut stdout_guard;
    let mut default_stdout;
    let stdout: &mut dyn Write = match &opts.stdout {
        Some(cell) => {
            stdout_guard = cell.borrow_mut();
            &mut **stdout_guard
        }
        None => {
            default_stdout = std::io::stdout();
            &mut default_stdout
        }
    };
    let mut stderr_guard;
    let mut default_stderr;
    let stderr: &mut dyn Write = match &opts.stderr {
        Some(cell) => {
            stderr_guard = cell.borrow_mut();
            &mut **stderr_guard
        }
        None => {
            default_stderr = std::io::stderr();
            &mut default_stderr
        }
    };

    let cwd = opts
        .cwd
        .clone()
        .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
    let system_env = SystemEnv;
    let env: &dyn EnvLookup = match &opts.env {
        Some(env) => env.as_ref(),
        None => &system_env,
    };

    run_install_cli_streams(
        args,
        stdin,
        stdout,
        stderr,
        &cwd,
        &opts.provided_inputs,
        &opts.provided_skill_ids,
        env,
    )
}

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

    fn args(list: &[&str]) -> Vec<String> {
        list.iter().map(|s| s.to_string()).collect()
    }

    #[test]
    fn parse_args_flags_and_paths() {
        let parsed = parse_args(&args(&[
            "./a", "--agent", "codex", "--scope", "repo", "--force",
        ]))
        .unwrap();
        assert_eq!(parsed.input_paths, vec!["./a"]);
        assert_eq!(parsed.agents, vec!["codex"]);
        assert_eq!(parsed.scopes, vec!["repo"]);
        assert!(parsed.force);
        assert!(!parsed.help);
    }

    #[test]
    fn parse_args_rejects_duplicates_and_commas() {
        assert_eq!(
            parse_args(&args(&["--agent", "codex", "--agent", "claude"]))
                .unwrap_err()
                .message,
            "Only one --agent flag is allowed."
        );
        assert_eq!(
            parse_args(&args(&["--agent", "codex,claude"]))
                .unwrap_err()
                .message,
            "Only one value is allowed for --agent. Comma-separated values are not supported."
        );
        assert_eq!(
            parse_args(&args(&["--scope", "repo,user"]))
                .unwrap_err()
                .message,
            "Only one value is allowed for --scope. Comma-separated values are not supported."
        );
        assert_eq!(
            parse_args(&args(&["--agent"])).unwrap_err().message,
            "Missing value for --agent."
        );
        assert_eq!(
            parse_args(&args(&["--scope", "--force"]))
                .unwrap_err()
                .message,
            "Missing value for --scope."
        );
        assert_eq!(
            parse_args(&args(&["--bogus"])).unwrap_err().message,
            "Unknown option: --bogus"
        );
    }
}