From 620b699069476dc67425cccc458249bb83af9035 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Fri, 27 Dec 2024 07:11:37 +0100 Subject: [PATCH] cli --- Cargo.lock | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/args.rs | 25 ++++++++ src/main.rs | 118 ++++++++++++++++++++--------------- 4 files changed, 269 insertions(+), 49 deletions(-) create mode 100644 src/args.rs diff --git a/Cargo.lock b/Cargo.lock index e0636ce..c6da70e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,55 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "anstream" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125" +dependencies = [ + "anstyle", + "windows-sys", +] + [[package]] name = "bitflags" version = "2.6.0" @@ -20,6 +69,39 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" +[[package]] +name = "clap" +version = "4.5.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3135e7ec2ef7b10c6ed8950f0f792ed96ee093fa088608f1c76e569722700c84" +dependencies = [ + "clap_builder", +] + +[[package]] +name = "clap_builder" +version = "4.5.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30582fc632330df2bd26877bde0c1f4470d57c582bbc070376afcd04d8cb4838" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_lex" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" + +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + [[package]] name = "equivalent" version = "1.0.1" @@ -42,6 +124,12 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + [[package]] name = "libc" version = "0.2.169" @@ -58,6 +146,7 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" name = "navinstall" version = "0.1.0" dependencies = [ + "clap", "nix", "serde", "toml", @@ -122,6 +211,12 @@ dependencies = [ "serde", ] +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "syn" version = "2.0.91" @@ -173,6 +268,85 @@ version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + [[package]] name = "winnow" version = "0.6.20" diff --git a/Cargo.toml b/Cargo.toml index 402f3dd..ea12e78 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2024" [dependencies] +clap = { version = "4.5.23", features = ["cargo"] } nix = { version = "0.29.0", features = ["user"] } serde = { version = "1.0.216", features = ["derive"] } toml = "0.8.19" diff --git a/src/args.rs b/src/args.rs new file mode 100644 index 0000000..643ccc9 --- /dev/null +++ b/src/args.rs @@ -0,0 +1,25 @@ +use clap::{arg, command}; + +pub fn get_args() -> clap::ArgMatches { + command!() + .about("navOS Installer") + .subcommand(command!("create-iso").about("Create a new installation medium ISO")) + .subcommand( + command!() + .name("install") + .about("Install a system according to configuration") + .arg(arg!([config] "Config file").required(true)), + ) + .subcommand( + command!() + .name("create-tar") + .about("Create a container tar image"), + ) + .subcommand( + command!() + .name("create-img") + .about("Create an install on a disk image for VMs or embedded devices") + .arg(arg!([config] "Config file").required(true)), + ) + .get_matches() +} diff --git a/src/main.rs b/src/main.rs index 94a150e..7f9b5ca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,12 +3,16 @@ use std::io::Write; use config::{DriveConfig, GeneralConfig, InstallConfig}; use nix::unistd::{Uid, getuid}; +mod args; mod config; fn is_root() -> bool { getuid() == Uid::from_raw(0) } +// TODO : Setup users +// TODO : Setup ssh (config + authorized_keys) + // DRIVE SELECTION pub fn str_vec(v: Vec<&str>) -> Vec { @@ -180,55 +184,77 @@ fn run_command(cmd: &[String], input: Option<&str>, inherit: bool) -> (String, S fn main() { println!("⚠️ Warning: This is an alpha version of the installer. DO NOT USE in PROD"); - let args: Vec = std::env::args().collect(); + let args = args::get_args(); - // TODO : Cleanup CLI interface - - if args.get(1).unwrap() == "create-iso" { - create_iso(); - std::process::exit(0); - } - - if args.len() < 2 { - eprintln!("Error: No configuration file provided."); - std::process::exit(1); - } - - let config_file = &args[1]; - let config_content = std::fs::read_to_string(config_file); - - let conf: InstallConfig = match config_content { - Ok(content) => match toml::from_str(&content) { - Ok(config) => config, - Err(e) => { - eprintln!("Error: Could not deserialize TOML file. {e}"); - std::process::exit(1); - } - }, - Err(_) => { - eprintln!("Error: Could not read config file."); - std::process::exit(1); + match args.subcommand() { + Some(("create-iso", _)) => { + create_iso(); + std::process::exit(0); } - }; + Some(("create-tar", _)) => { + println!("Tar creation is not yet supported"); + unimplemented!() + } + Some(("create-img", install_args)) => { + let config_file: &String = install_args.get_one("config").unwrap(); + let config_content = std::fs::read_to_string(config_file); - // TODO : Show config - println!("Config: {conf:?}"); - println!("\nDo you want to proceed with this configuration? (yes/no)"); + let conf: InstallConfig = match config_content { + Ok(content) => match toml::from_str(&content) { + Ok(config) => config, + Err(e) => { + eprintln!("Error: Could not deserialize TOML file. {e}"); + std::process::exit(1); + } + }, + Err(_) => { + eprintln!("Error: Could not read config file."); + std::process::exit(1); + } + }; - let mut input = String::new(); - std::io::stdout().flush().expect("Error flushing stdout."); - std::io::stdin() - .read_line(&mut input) - .expect("Error reading input."); - let input = input.trim().to_lowercase(); + println!("Installing to a disk image is not yet supported"); + unimplemented!() + } + Some(("install", install_args)) => { + let config_file: &String = install_args.get_one("config").unwrap(); + let config_content = std::fs::read_to_string(config_file); - if input != "yes" { - println!("Installation aborted."); - std::process::exit(0); + let conf: InstallConfig = match config_content { + Ok(content) => match toml::from_str(&content) { + Ok(config) => config, + Err(e) => { + eprintln!("Error: Could not deserialize TOML file. {e}"); + std::process::exit(1); + } + }, + Err(_) => { + eprintln!("Error: Could not read config file."); + std::process::exit(1); + } + }; + + // TODO : Show config + println!("Config: {conf:?}"); + println!("\nDo you want to proceed with this configuration? (yes/no)"); + + let mut input = String::new(); + std::io::stdout().flush().expect("Error flushing stdout."); + std::io::stdin() + .read_line(&mut input) + .expect("Error reading input."); + let input = input.trim().to_lowercase(); + + if input != "yes" { + println!("Installation aborted."); + std::process::exit(0); + } + + // Run the + install(conf) + } + _ => {} } - - // Run the - install(conf) } pub fn create_iso() { @@ -269,9 +295,3 @@ pub fn create_iso() { std::fs::remove_dir_all("./work").unwrap(); } - -// TODO : CLI -// navos create-iso -// navos create-tar -// navos create-img -// navos install