navinstall/src/main.rs
2025-04-17 05:04:26 +02:00

125 lines
4.1 KiB
Rust

use navinstall::config::InstallConfig;
use navinstall::install::navos::setup_navos;
use navinstall::install::{
drives::setup_disk_image, install, install_mnt, security::ensure_secure_boot,
};
use navinstall::linux::is_root;
use navinstall::pkg::pacstrap_at;
use navinstall::print::print_config;
use navinstall::print_status;
use navinstall::{create_iso::create_iso, expect_yes};
use yansi::{Color, Paint};
pub fn ensure_root() {
if !is_root() {
eprintln!("Error: You need root to install");
std::process::exit(1);
}
}
fn main() {
println!(
"{}",
"⚠️ Warning: This is an alpha version of the installer. DO NOT USE in PROD"
.paint(Color::Yellow)
);
let args = navinstall::args::get_args();
match args.subcommand() {
Some(("create-iso", iso_args)) => {
let without_gui = iso_args.get_flag("without_gui");
let no_tmp = iso_args.get_flag("no_tmp");
let kb_layout_default = "us".to_string();
let kb_layout: &String = iso_args.get_one("kb_layout").unwrap_or(&kb_layout_default);
let kb_variant: Option<&str> =
iso_args.get_one("kb_variant").map(|x: &String| x.as_str());
let auto_install_config: Option<&String> = iso_args.get_one("install");
let user: Option<&String> = iso_args.get_one("user");
let pass: Option<&String> = iso_args.get_one("pass");
let uid: Option<&String> = iso_args.get_one("uid");
let user = if user.is_some() && pass.is_some() {
Some((
user.unwrap().to_string(),
pass.unwrap().to_string(),
uid.map(|x| x.parse().unwrap()).unwrap_or(1000),
))
} else {
None
};
let units: Vec<_> = iso_args.get_many("unit").unwrap_or_default().collect();
create_iso(
without_gui,
no_tmp,
kb_layout,
kb_variant,
user,
units.into_iter().map(|x: &String| x.to_string()).collect(),
auto_install_config,
);
std::process::exit(0);
}
Some(("create-tar", tar_options)) => {
ensure_root();
let dir: &String = tar_options.get_one("dir").unwrap();
print_status("Pacstrapping root fs");
pacstrap_at(&dir, None, &[]);
setup_navos(dir.as_str());
}
Some(("create-img", install_args)) => {
let config_file: &String = install_args.get_one("config").unwrap();
let conf = read_conf(config_file);
let img_file: &String = install_args.get_one("image").unwrap();
setup_disk_image(img_file.as_str());
install_mnt(conf, false);
}
Some(("install", install_args)) => {
ensure_root();
let config_file: &String = install_args.get_one("config").unwrap();
let force = install_args.get_flag("force");
let conf = read_conf(config_file);
if !force {
print_config(&conf);
print!("Do you want to proceed with this configuration? (yes/no) ");
expect_yes();
}
if conf.general.secure_boot.unwrap_or(false) {
ensure_secure_boot();
}
// Run the
install(conf, true);
}
_ => {}
}
}
pub fn read_conf(config_file: &str) -> InstallConfig {
let config_content = std::fs::read_to_string(config_file);
match config_content {
Ok(content) => match toml::from_str(&content) {
Ok(config) => config,
Err(e) => {
eprintln!(
"{} {}",
"Error: Could not deserialize TOML file.".paint(Color::Red),
e.paint(Color::Red)
);
std::process::exit(1);
}
},
Err(_) => {
eprintln!("{}", "Error: Could not read config file.".paint(Color::Red));
std::process::exit(1);
}
}
}