astrid 0.10.1

Command-line interface for Astrid secure agent runtime
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
//! `astrid capsule new <name>` — scaffold a complete, first-try-compiling
//! capsule project.
//!
//! Generates the full skeleton a tool capsule needs to build cleanly on the
//! first `cargo build`: the `.cargo/config.toml` carrying the getrandom
//! footgun fix (without it, anything pulling `getrandom` — uuid v4, the
//! `HashMap` `RandomState` — fails to *link* on `wasm32-unknown-unknown`), a
//! pinned `rust-toolchain.toml`, a `Cargo.toml` with the size-optimised
//! release profile, a `Capsule.toml` with the mandatory tool-bus ACL, a
//! working `src/lib.rs` (a `hello` tool example), and a `README.md`.
//!
//! The `wit/` directory is intentionally NOT generated — it is produced at
//! build time by `astrid capsule build`.

use std::path::{Path, PathBuf};
use std::process::ExitCode;

use anyhow::{Context, Result};
use clap::Args;

use crate::theme::Theme;

/// Capsule kinds the scaffolder can generate. Only `tool` is supported in v1.
const SUPPORTED_KINDS: &[&str] = &["tool"];

#[derive(Args, Debug, Clone)]
pub(crate) struct NewArgs {
    /// Capsule name. Must be a valid Rust package name: lowercase letters,
    /// digits, and hyphens, starting with a letter.
    pub name: String,
    /// Capsule kind. Only `tool` is supported in v1.
    #[arg(long, default_value = "tool")]
    pub kind: String,
    /// Parent directory to create the project in. The project is written to
    /// `<path>/<name>` (defaults to `./<name>`).
    #[arg(long)]
    pub path: Option<PathBuf>,
    /// Overwrite an existing non-empty target directory.
    #[arg(long)]
    pub force: bool,
    /// If the `wasm32-unknown-unknown` target is missing and `rustup` is
    /// available, install it without prompting (`rustup target add
    /// wasm32-unknown-unknown`). Useful in non-interactive setups.
    #[arg(long)]
    pub install_target: bool,
}

/// Entry point for `astrid capsule new`.
pub(crate) fn run(args: &NewArgs) -> Result<ExitCode> {
    if !SUPPORTED_KINDS.contains(&args.kind.as_str()) {
        eprintln!(
            "{}",
            Theme::error(&format!(
                "unsupported capsule kind '{}'. Supported kinds: {}",
                args.kind,
                SUPPORTED_KINDS.join(", ")
            ))
        );
        return Ok(ExitCode::from(1));
    }

    if let Err(reason) = validate_name(&args.name) {
        eprintln!(
            "{}",
            Theme::error(&format!("invalid capsule name '{}': {reason}", args.name))
        );
        return Ok(ExitCode::from(1));
    }

    let parent = args.path.clone().unwrap_or_else(|| PathBuf::from("."));
    let target = parent.join(&args.name);

    if dir_is_non_empty(&target) {
        if args.force {
            eprintln!(
                "{}",
                Theme::warning(&format!(
                    "overwriting existing files in {}",
                    target.display()
                ))
            );
        } else {
            eprintln!(
                "{}",
                Theme::error(&format!(
                    "target directory {} already exists and is not empty. \
                     Use --force to overwrite.",
                    target.display()
                ))
            );
            return Ok(ExitCode::from(1));
        }
    }

    // Toolchain preflight runs BEFORE we write the skeleton so any missing
    // piece is surfaced up-front. It is fail-FRIENDLY: a missing toolchain
    // only warns and guides — we still generate the project so the author has
    // something to build once they install what they need.
    preflight::check(args.install_target);

    scaffold(&target, &args.name)
        .with_context(|| format!("failed to scaffold capsule into {}", target.display()))?;

    print_next_steps(&target, &args.name);
    Ok(ExitCode::SUCCESS)
}

/// Rust-toolchain preflight: verify `cargo`/`rustc` and the
/// `wasm32-unknown-unknown` target are present, warn + guide if not, and offer
/// to install the target when `rustup` is available.
///
/// Every check is fail-FRIENDLY: nothing here aborts the scaffold. A capsule
/// author who runs `astrid capsule new` on a machine without the wasm target
/// still gets a complete, correct project — they just get told exactly what to
/// install before the first build succeeds.
mod preflight {
    use std::io::IsTerminal;
    use std::process::Command;

    use crate::theme::Theme;

    /// The compile target every capsule builds for.
    const WASM_TARGET: &str = "wasm32-unknown-unknown";

    /// Run the full preflight. `install_target` forces a non-interactive
    /// `rustup target add` when the target is missing and `rustup` is present.
    pub(super) fn check(install_target: bool) {
        // cargo / rustc presence. A missing toolchain is the only thing that
        // makes the rest moot, so report it and stop probing (but still let
        // the caller scaffold).
        let have_cargo = tool_present("cargo");
        let have_rustc = tool_present("rustc");
        if !have_cargo || !have_rustc {
            warn_no_rust_toolchain(have_cargo, have_rustc);
            return;
        }

        if wasm_target_installed() {
            return;
        }

        // Target missing. If rustup drives the toolchain we can fix it for the
        // author; otherwise we can only point at the right command.
        if rustup_present() {
            if install_target || prompt_install() {
                if run_target_add() {
                    eprintln!(
                        "{}",
                        Theme::success(&format!("installed the {WASM_TARGET} target"))
                    );
                    return;
                }
                eprintln!(
                    "{}",
                    Theme::warning(&format!(
                        "could not install the {WASM_TARGET} target automatically — \
                         run `rustup target add {WASM_TARGET}` yourself."
                    ))
                );
            } else {
                guide_rustup_target_add();
            }
        } else {
            guide_rustup_target_add();
        }
    }

    /// Whether `tool --version` runs successfully (i.e. the binary is on PATH).
    fn tool_present(tool: &str) -> bool {
        Command::new(tool)
            .arg("--version")
            .output()
            .is_ok_and(|o| o.status.success())
    }

    /// Whether `rustup` is the active toolchain manager.
    fn rustup_present() -> bool {
        tool_present("rustup")
    }

    /// Whether the wasm target's std is available to build against.
    ///
    /// Preferred probe is `rustup target list --installed` (cheap, exact). When
    /// `rustup` is not in play we fall back to asking `rustc` to emit the
    /// target cfg — that only succeeds if the target's libstd is present, so it
    /// is a reliable installed/not check for a rustup-free (e.g. distro or Nix)
    /// toolchain too.
    fn wasm_target_installed() -> bool {
        if rustup_present()
            && let Ok(out) = Command::new("rustup")
                .args(["target", "list", "--installed"])
                .output()
            && out.status.success()
        {
            return String::from_utf8_lossy(&out.stdout)
                .lines()
                .any(|line| line.trim() == WASM_TARGET);
        }
        // rustup absent or the query failed: probe rustc directly.
        Command::new("rustc")
            .args(["--target", WASM_TARGET, "--print", "cfg"])
            .output()
            .is_ok_and(|o| o.status.success())
    }

    /// Run `rustup target add wasm32-unknown-unknown`, streaming its output.
    fn run_target_add() -> bool {
        eprintln!(
            "{}",
            Theme::dimmed(&format!("running `rustup target add {WASM_TARGET}`..."))
        );
        Command::new("rustup")
            .args(["target", "add", WASM_TARGET])
            .status()
            .is_ok_and(|s| s.success())
    }

    /// Ask whether to install the missing target. Non-interactive sessions
    /// (no TTY) default to NO — we never silently mutate the toolchain without
    /// either a TTY answer or the explicit `--install-target` flag.
    fn prompt_install() -> bool {
        if !std::io::stdin().is_terminal() {
            return false;
        }
        eprintln!(
            "{}",
            Theme::warning(&format!(
                "the {WASM_TARGET} target (required to build capsules) is not installed."
            ))
        );
        eprint!("Install it now with `rustup target add {WASM_TARGET}`? [Y/n] ");
        if std::io::Write::flush(&mut std::io::stderr()).is_err() {
            return false;
        }
        let mut input = String::new();
        if std::io::stdin().read_line(&mut input).unwrap_or(0) == 0 {
            // EOF before an answer — treat as no.
            return false;
        }
        let input = input.trim();
        input.is_empty() || input.eq_ignore_ascii_case("y") || input.eq_ignore_ascii_case("yes")
    }

    /// Print actionable guidance when the wasm target is missing and we did not
    /// (or could not) install it.
    fn guide_rustup_target_add() {
        eprintln!(
            "{}",
            Theme::warning(&format!(
                "the {WASM_TARGET} target is not installed — capsules cannot build without it."
            ))
        );
        eprintln!("  Install it with:");
        eprintln!("    rustup target add {WASM_TARGET}");
        eprintln!(
            "{}",
            Theme::dimmed(
                "  (or pass --install-target to have `astrid capsule new` add it for you.)"
            )
        );
    }

    /// Print actionable guidance when cargo/rustc are not on PATH at all.
    fn warn_no_rust_toolchain(have_cargo: bool, have_rustc: bool) {
        let missing = match (have_cargo, have_rustc) {
            (false, false) => "cargo and rustc were",
            (false, true) => "cargo was",
            (true, false) => "rustc was",
            (true, true) => unreachable!("warn only called when one is missing"),
        };
        eprintln!(
            "{}",
            Theme::warning(&format!(
                "{missing} not found on PATH — you need a Rust toolchain to build this capsule."
            ))
        );
        eprintln!("  Install Rust (rustup) with:");
        eprintln!("    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh");
        eprintln!("  then add the capsule build target:");
        eprintln!("    rustup target add {WASM_TARGET}");
    }
}

/// Validate that `name` is a usable capsule + Rust package name.
///
/// Rules: lowercase ASCII letters, digits, and hyphens; must start with a
/// letter; no leading/trailing/doubled hyphens. This is the intersection of
/// what cargo accepts as a package name and what reads cleanly as a bus topic
/// segment / component id.
fn validate_name(name: &str) -> std::result::Result<(), String> {
    if name.is_empty() {
        return Err("name must not be empty".into());
    }
    let first = name.chars().next().expect("non-empty checked above");
    if !first.is_ascii_lowercase() {
        return Err("name must start with a lowercase letter".into());
    }
    if name.ends_with('-') {
        return Err("name must not end with a hyphen".into());
    }
    if name.contains("--") {
        return Err("name must not contain consecutive hyphens".into());
    }
    for ch in name.chars() {
        if !(ch.is_ascii_lowercase() || ch.is_ascii_digit() || ch == '-') {
            return Err(format!(
                "'{ch}' is not allowed (use lowercase letters, digits, and hyphens)"
            ));
        }
    }
    Ok(())
}

/// Whether `path` exists, is a directory, and contains at least one entry.
fn dir_is_non_empty(path: &Path) -> bool {
    match std::fs::read_dir(path) {
        Ok(mut entries) => entries.next().is_some(),
        Err(_) => false,
    }
}

/// Write the full project skeleton into `target`.
fn scaffold(target: &Path, name: &str) -> Result<()> {
    use super::new_templates as t;

    let crate_ident = name.replace('-', "_");

    write_file(&target.join(".cargo/config.toml"), &t::cargo_config_toml())?;
    write_file(
        &target.join("rust-toolchain.toml"),
        &t::rust_toolchain_toml(),
    )?;
    write_file(&target.join("Cargo.toml"), &t::cargo_toml(name))?;
    write_file(
        &target.join("Capsule.toml"),
        &t::capsule_toml(name, &crate_ident),
    )?;
    write_file(&target.join("src/lib.rs"), &t::lib_rs())?;
    write_file(&target.join("README.md"), &t::readme_md(name))?;
    write_file(
        &target.join("AUTHORING.md"),
        &t::authoring_md(name, &crate_ident),
    )?;
    Ok(())
}

/// Write `contents` to `path`, creating parent directories as needed.
fn write_file(path: &Path, contents: &str) -> Result<()> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)
            .with_context(|| format!("failed to create {}", parent.display()))?;
    }
    std::fs::write(path, contents)
        .with_context(|| format!("failed to write {}", path.display()))?;
    Ok(())
}

/// Print the friendly next-steps message after a successful scaffold.
fn print_next_steps(target: &Path, name: &str) {
    eprintln!(
        "{}",
        Theme::success(&format!("Created capsule '{name}' at {}", target.display()))
    );
    eprintln!();
    eprintln!("{}", Theme::header("Next steps"));
    eprintln!("  cd {}", target.display());
    eprintln!("  astrid capsule build");
    eprintln!("  astrid capsule install ./dist/{name}.capsule");
    eprintln!();
    eprintln!(
        "{}",
        Theme::dimmed("Edit src/lib.rs to replace the hello tool with your own.")
    );
}

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

    #[test]
    fn name_validation_accepts_good_names() {
        for ok in ["a", "hello", "my-tool", "tool42", "a1-b2-c3"] {
            assert!(validate_name(ok).is_ok(), "expected '{ok}' to be valid");
        }
    }

    #[test]
    fn name_validation_rejects_bad_names() {
        for bad in [
            "",
            "1tool",
            "-tool",
            "tool-",
            "tool--name",
            "Tool",
            "my_tool",
            "my tool",
            "tool!",
        ] {
            assert!(
                validate_name(bad).is_err(),
                "expected '{bad}' to be invalid"
            );
        }
    }

    #[test]
    fn dir_is_non_empty_distinguishes_states() {
        let tmp = tempfile::tempdir().unwrap();
        let missing = tmp.path().join("missing");
        assert!(!dir_is_non_empty(&missing), "missing dir is not non-empty");

        let empty = tmp.path().join("empty");
        std::fs::create_dir_all(&empty).unwrap();
        assert!(!dir_is_non_empty(&empty), "empty dir is not non-empty");

        std::fs::write(empty.join("x"), "y").unwrap();
        assert!(dir_is_non_empty(&empty), "dir with a file is non-empty");
    }

    #[test]
    fn scaffold_writes_expected_files() {
        let tmp = tempfile::tempdir().unwrap();
        let target = tmp.path().join("hello-cap");
        scaffold(&target, "hello-cap").unwrap();

        for rel in [
            ".cargo/config.toml",
            "rust-toolchain.toml",
            "Cargo.toml",
            "Capsule.toml",
            "src/lib.rs",
            "README.md",
            "AUTHORING.md",
        ] {
            assert!(
                target.join(rel).is_file(),
                "expected scaffolded file {rel} to exist"
            );
        }
    }
}