diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3678fa7..f7088f5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -52,7 +52,7 @@ jobs: run: cargo test --release - name: Setup registry - run: cargo run --release -- --setup-registry + run: cargo run --release -- setup-registry - name: Run CLI with JavaScript run: cargo run --release -- guest-examples/hello.js @@ -102,7 +102,7 @@ jobs: run: npm run build - name: Setup registry - run: cargo run --release -- --setup-registry + run: cargo run --release -- setup-registry - name: Run Node.js example run: node examples/napi.js @@ -149,7 +149,7 @@ jobs: run: .venv/bin/maturin develop --features python - name: Setup registry - run: cargo run --release -- --setup-registry + run: cargo run --release -- setup-registry - name: Run Python example run: .venv/bin/python examples/python_sdk_example.py diff --git a/Cargo.lock b/Cargo.lock index 5062467..c04cef6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,6 +32,56 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" + +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.61.2", +] + [[package]] name = "anyhow" version = "1.0.100" @@ -216,6 +266,46 @@ dependencies = [ "windows-link 0.2.1", ] +[[package]] +name = "clap" +version = "4.5.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6899ea499e3fb9305a65d5ebf6e3d2248c5fab291f300ad0a704fbe142eae31a" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b12c8b680195a62a8364d16b8447b01b6c2c8f9aaf68bee653be34d4245e238" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a92793da1a46a5f2a02a6f4c46c6496b28c43638adea8306fcb0caa1634f24e5" +dependencies = [ + "heck 0.5.0", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3e64b0cc0439b12df2fa678eae89a1c56a529fd067a9115f7827f1fffd22b32" + [[package]] name = "cmake" version = "0.1.54" @@ -225,6 +315,12 @@ dependencies = [ "cc", ] +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + [[package]] name = "combine" version = "4.6.7" @@ -673,6 +769,12 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hex" version = "0.4.3" @@ -860,6 +962,7 @@ name = "hyperlight-nanvix" version = "0.1.0" dependencies = [ "anyhow", + "clap", "libc", "nanvix", "napi", @@ -1031,6 +1134,12 @@ dependencies = [ "serde", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + [[package]] name = "itoa" version = "1.0.15" @@ -1468,6 +1577,12 @@ version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + [[package]] name = "openssl-probe" version = "0.2.1" @@ -1648,7 +1763,7 @@ version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c7e9b68bb9c3149c5b0cade5d07f953d6d125eb4337723c4ccdb665f1f96185" dependencies = [ - "heck", + "heck 0.4.1", "proc-macro2", "pyo3-build-config", "quote", @@ -1960,7 +2075,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -2224,6 +2339,12 @@ name = "static_assert" version = "0.11.112" source = "git+https://github.com/nanvix/nanvix?rev=7752e9f2deb4a5606f9885e4c130eec4ea583de1#7752e9f2deb4a5606f9885e4c130eec4ea583de1" +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "subtle" version = "2.6.1" @@ -2666,6 +2787,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "uuid" version = "1.19.0" diff --git a/Cargo.toml b/Cargo.toml index b3bdb26..c4472dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ nanvix = { git = "https://github.com/nanvix/nanvix", rev = "7752e9f2deb4a5606f98 ] } tokio = { version = "1.0", features = ["rt-multi-thread", "macros"] } anyhow = "1.0" +clap = { version = "4", features = ["derive"] } libc = "0.2.178" # NAPI bindings (optional) diff --git a/README.md b/README.md index 8d9ac99..cc0c1b3 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ For compiled languages, you'll need to compile first, then run. #### Setup (one-time) ```bash -cargo run -- --setup-registry +cargo run -- setup-registry ``` This downloads the toolchain and runtime files to `~/.cache/nanvix-registry/`. @@ -251,8 +251,8 @@ cargo run --example syscall_interception **Clear cache and re-download:** ```bash -cargo run -- --clear-registry -cargo run -- --setup-registry +cargo run -- clear-registry +cargo run -- setup-registry ``` **Clean socket files if networking issues occur:** diff --git a/src/bin/hyperlight-nanvix.rs b/src/bin/hyperlight-nanvix.rs index 7ffd2a1..30b2204 100644 --- a/src/bin/hyperlight-nanvix.rs +++ b/src/bin/hyperlight-nanvix.rs @@ -1,9 +1,37 @@ use anyhow::Result; +use clap::{Parser, Subcommand}; use hyperlight_nanvix::{cache, RuntimeConfig, Sandbox}; use nanvix::log; use nanvix::registry::Registry; -use std::env; -use std::path::Path; +use std::path::PathBuf; + +/// A Hyperlight VMM wrapper with out-of-the-box support for running Nanvix microkernel guests +#[derive(Parser)] +#[command(name = "hyperlight-nanvix")] +#[command(about = "Run scripts in a Nanvix microkernel guest")] +#[command( + after_help = "Supported file types: .js, .mjs (JavaScript), .py (Python), .elf, .o (Binary)" +)] +struct Cli { + /// Show detailed nanvix logging + #[arg(long)] + verbose: bool, + + #[command(subcommand)] + command: Option, + + /// Path to the script to run + #[arg(value_name = "SCRIPT")] + script_path: Option, +} + +#[derive(Subcommand)] +enum Commands { + /// Download nanvix registry and show compilation instructions + SetupRegistry, + /// Clear the nanvix registry cache + ClearRegistry, +} /// Default log-level (overridden by RUST_LOG environment variable if set). const DEFAULT_LOG_LEVEL: &str = "info"; @@ -78,48 +106,31 @@ async fn clear_registry_command() -> Result<()> { } } - println!("Run 'cargo run -- --setup-registry' to re-download if needed."); + println!("Run 'cargo run -- setup-registry' to re-download if needed."); Ok(()) } #[tokio::main] async fn main() -> Result<()> { - // Parse command line arguments first - let args: Vec = env::args().collect(); - - // Check for flags - let verbose = args.contains(&"--verbose".to_string()); - let setup_registry = args.contains(&"--setup-registry".to_string()); - let clear_registry = args.contains(&"--clear-registry".to_string()); - - // Handle setup-registry command - if setup_registry { - return setup_registry_command().await; + let cli = Cli::parse(); + + // Handle subcommands + if let Some(command) = cli.command { + return match command { + Commands::SetupRegistry => setup_registry_command().await, + Commands::ClearRegistry => clear_registry_command().await, + }; } - // Handle clear-registry command - if clear_registry { - return clear_registry_command().await; - } - - // Find the script argument (first non-flag argument) - let script_arg = args - .iter() - .position(|arg| !arg.starts_with("--") && !arg.ends_with("hyperlight-nanvix")); - - let script_path = if let Some(idx) = script_arg { - Path::new(&args[idx]) - } else { - eprintln!("Usage: {} [--verbose] ", args[0]); - eprintln!(" {} --setup-registry", args[0]); - eprintln!(" {} --clear-registry", args[0]); - eprintln!("Supported file types: .js, .mjs (JavaScript), .py (Python), .elf, .o (Binary)"); - eprintln!("Options:"); - eprintln!(" --verbose Show detailed nanvix logging"); - eprintln!(" --setup-registry Download nanvix registry and show compilation instructions"); - eprintln!(" --clear-registry Clear the nanvix registry cache"); + // Require script path for default operation + let script_path = cli.script_path.unwrap_or_else(|| { + eprintln!("error: the following required arguments were not provided:\n