You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jade/src/internal/config.rs

164 lines
4.8 KiB
Rust

use crate::args::{DesktopSetup, PartitionMode};
use crate::functions::*;
use crate::internal::*;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Serialize, Deserialize)]
struct Config {
partition: Partition,
bootloader: Bootloader,
locale: Locale,
networking: Networking,
users: Vec<Users>,
rootpass: String,
desktop: Option<DesktopSetup>,
timeshift: bool,
extra_packages: Vec<String>,
}
#[derive(Serialize, Deserialize)]
struct Partition {
device: String,
mode: PartitionMode,
efi: bool,
}
#[derive(Serialize, Deserialize)]
struct Bootloader {
r#type: String,
location: String,
}
#[derive(Serialize, Deserialize)]
struct Locale {
locale: Vec<String>,
keymap: String,
timezone: String,
}
#[derive(Serialize, Deserialize)]
struct Networking {
hostname: String,
ipv6: bool,
}
#[derive(Serialize, Deserialize)]
struct Users {
name: String,
password: String,
hasroot: bool,
}
pub fn read_config(configpath: PathBuf) {
let data = std::fs::read_to_string(&configpath);
match &data {
Ok(_) => {
log(format!(
"[ \x1b[2;1;32mOK\x1b[0m ] Read config file {configpath:?}"
));
}
Err(e) => {
crash(
format!("Read config file {configpath:?} ERROR: {}", e),
e.raw_os_error().unwrap(),
);
}
}
let config: std::result::Result<Config, serde_json::Error> =
serde_json::from_str(&data.unwrap());
match &config {
Ok(_) => {
log(format!(
"[ \x1b[2;1;32mOK\x1b[0m ] Parse config file {configpath:?}",
));
}
Err(e) => {
crash(format!("Parse config file {configpath:?} ERROR: {}", e), 1);
}
}
let config: Config = config.unwrap();
info(format!(
"Block device to use : /dev/{}",
config.partition.device
));
info(format!("Partitioning mode : {:?}", config.partition.mode));
info(format!("Partitioning for EFI : {}", config.partition.efi));
partition::partition(
PathBuf::from("/dev/").join(config.partition.device),
config.partition.mode,
config.partition.efi,
);
base::install_base_packages();
base::genfstab();
println!();
info(format!(
"Installing bootloader : {}",
config.bootloader.r#type
));
info(format!(
"Installing bootloader to : {}",
config.bootloader.location
));
if config.bootloader.r#type == "grub-efi" {
base::install_bootloader_efi(PathBuf::from(config.bootloader.location));
} else if config.bootloader.r#type == "grub-legacy" {
base::install_bootloader_legacy(PathBuf::from(config.bootloader.location));
}
println!();
info(format!("Adding Locales : {:?}", config.locale.locale));
info(format!("Using keymap : {}", config.locale.keymap));
info(format!("Setting timezone : {}", config.locale.timezone));
locale::set_locale(config.locale.locale.join(" "));
locale::set_keyboard(config.locale.keymap.as_str());
locale::set_timezone(config.locale.timezone.as_str());
println!();
info(format!("Hostname : {}", config.networking.hostname));
info(format!("Enabling ipv6 : {}", config.networking.ipv6));
network::set_hostname(config.networking.hostname.as_str());
network::create_hosts();
if config.networking.ipv6 {
network::enable_ipv6();
}
println!();
println!("---------");
for i in 0..config.users.len() {
info(format!("Creating user : {}", config.users[i].name));
info(format!(
"Setting use password : {}",
config.users[i].password
));
info(format!(
"Enabling root for user : {}",
config.users[i].hasroot
));
users::new_user(
config.users[i].name.as_str(),
config.users[i].hasroot,
config.users[i].password.as_str(),
);
println!("---------");
}
println!();
info(format!("Setting root password : {}", config.rootpass));
users::root_pass(config.rootpass.as_str());
println!();
info(format!("Installing desktop : {:?}", config.desktop));
if let Some(desktop) = &config.desktop {
desktops::install_desktop_setup(*desktop);
}
println!();
info(format!("Enabling timeshift : {}", config.timeshift));
if config.timeshift {
base::setup_timeshift();
}
info(format!("Extra packages : {:?}", config.extra_packages));
let mut extra_packages: Vec<&str> = Vec::new();
for i in 0..config.extra_packages.len() {
extra_packages.push(config.extra_packages[i].as_str());
}
install(extra_packages);
println!("Installation finished! You may reboot now!")
}