cli
All checks were successful
ci/woodpecker/push/build Pipeline was successful

This commit is contained in:
JMARyA 2024-12-27 07:11:37 +01:00
parent b9267827ee
commit 620b699069
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
4 changed files with 269 additions and 49 deletions

25
src/args.rs Normal file
View file

@ -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()
}

View file

@ -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<String> {
@ -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<String> = 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