ableos_userland/programs/system_config/src/main.rs

244 lines
8.0 KiB
Rust

use {
std::{
env,
error::Error,
fs::{self, File},
io::{Read, Write},
},
toml::{self, Table, Value},
};
fn main() -> Result<(), Box<dyn Error>> {
let mut args: Vec<String> = env::args().collect();
// #[cfg(target_os="linux")]
if args.len() == 1 {
println!("Pass a file");
}
args.remove(0);
let file_path = &args[0];
println!("configuration path - {}", file_path);
let mut file = File::open(file_path).unwrap(); //.expect("Unable to open the file");
let mut contents = String::new();
file.read_to_string(&mut contents)
.expect("Unable to read the file");
let value = contents.parse::<Table>().unwrap();
fs::remove_dir_all("disk")?;
fs::create_dir("disk")?;
// HANDLE repolist
{
let repolist = value.get("repositories").unwrap();
fs::create_dir("disk/System")?;
let ret = make_repolist("System/repolist.toml".to_string(), repolist);
println!("making repolist {:?}", ret);
}
// HANDLE boot
{
let boot = value.get("boot").unwrap();
let boot_table = boot.as_table().unwrap();
if boot_table.contains_key("limine") {
let limine = boot_table.get("limine").unwrap();
let ret = make_limine_cfg(limine);
println!("limine return {:?}", ret);
}
// println!("{:#?}", boot);
}
// HANDLE users
{
let mut passhash_list: Vec<(String, String)> = vec![];
let users = value.get("users").unwrap();
// println!("{:?}", users.as_table().unwrap().keys());
let users_table = users.as_table().unwrap();
for user in users_table.keys() {
let ut = users_table.get(user).unwrap();
// let home_path = ut.get("home").unwrap();
let home_path = format!("/Users/{}", user);
let pass_hash = ut.get("password_hash").unwrap();
passhash_list.push((user.to_string(), pass_hash.to_string()));
let ret = make_user(home_path.to_string());
println!("making user return {:?}", ret);
// Handle homepath generation of USER
{
let hp = home_path.clone().to_string();
let apps = ut.get("apps");
fs::create_dir_all(format!("disk/{}/Apps/", hp))?;
println!("ok");
if apps.is_some() {
let apps = apps.unwrap();
for app in apps.as_table().unwrap().keys() {
let pack_folder: String = format!("disk/{}/Apps/{}", hp, app);
let pack_config: String = format!("disk/{}/Apps/{}/config.toml", hp, app);
fs::create_dir_all(pack_folder)?;
let mut file = File::create(pack_config)?;
// repo_list_str.as_bytes()
let abc = apps.get(app).unwrap().get("configuration");
let tab = match abc {
Some(val) => val.clone(),
None => {
let tab = Table::new();
Value::Table(tab)
}
};
// .unwrap_or(&Value::Table(Table::new()));
let mut abc = tab.to_string();
if abc.len() > 2 {
abc.remove(0);
abc.remove(0);
abc.remove(abc.len() - 1);
abc.remove(abc.len() - 1);
}
if abc.len() == 2 {
abc.remove(0);
abc.remove(0);
}
file.write_all(abc.as_bytes())?;
}
}
}
let repolist = ut.get("repositories");
let hp = home_path.clone().to_string();
let user_path_repolist = format!("{}/repolist.toml", hp.to_string());
println!("{}", user_path_repolist);
match repolist {
Some(repolist) => {
let ret = make_repolist(user_path_repolist, repolist);
println!("making repolist {:?}", ret);
}
None => {
let repolist = Table::new();
let ret = make_repolist(user_path_repolist, &Value::Table(repolist));
println!("making repolist {:?}", ret);
}
}
}
let ret = generate_password_hashlist(passhash_list);
println!("making password hashlist {:?}", ret);
}
// TODO: Handle system wide things like repolist and Apps/
{}
// let mut file = File::create("disk/foo.txt")?;
// file.write_all(b"Hello, world!")?;
Ok(())
}
pub fn make_limine_cfg(limine: &Value) -> std::io::Result<()> {
let mut limine_str = String::new();
// raw_limine
{
let mut lc = limine.clone();
let boot_entries = lc.as_table_mut().unwrap();
let default_entry = boot_entries.get("default_entry").unwrap();
let timeout = boot_entries.get("timeout").unwrap();
let interface_resolution = boot_entries.get("interface_resolution").unwrap();
let verbose = boot_entries.get("verbose").unwrap();
let term_wallpaper = boot_entries.get("term_wallpaper").unwrap();
let vb_post = match verbose.as_bool().unwrap() {
true => "yes",
false => "no",
};
let term_background = boot_entries
.get("term_backdrop")
.unwrap_or(&Value::Integer(0));
let base = format!(
"DEFAULT_ENTRY={}
TIMEOUT={}
VERBOSE={}
INTERFACE_RESOLUTION={}
# Terminal related settings
TERM_WALLPAPER={}
TERM_BACKDROP={}
",
default_entry, timeout, vb_post, interface_resolution, term_wallpaper, term_background
);
limine_str.push_str(&base);
}
// HANDLE boot_entries
{
let mut lc = limine.clone();
let boot_entries = lc.as_table_mut().unwrap();
let mut real_boot_entries = boot_entries.clone();
for (key, value) in boot_entries.into_iter() {
if !value.is_table() {
real_boot_entries.remove(key);
}
}
for (name, value) in real_boot_entries {
let comment = value.get("comment").unwrap();
let protocol = value.get("protocol").unwrap();
let resolution = value.get("resolution").unwrap();
let kernel_path = value.get("kernel_path").unwrap();
let kernel_cmdline = value.get("kernel_cmdline").unwrap();
let entry = format!(
"
:{}
COMMENT={}
PROTOCOL={}
RESOLUTION={}
KERNEL_PATH={}
KERNEL_CMDLINE={}
",
name, comment, protocol, resolution, kernel_path, kernel_cmdline,
);
limine_str.push_str(&entry);
}
}
let mut file = File::create("disk/limine.cfg")?;
file.write_all(limine_str.as_bytes())?;
Ok(())
}
pub fn make_user(home_path: String) -> std::io::Result<()> {
let path = format!("disk/{}", home_path);
fs::create_dir_all(path)?;
Ok(())
}
pub fn make_repolist(path: String, repolist: &Value) -> std::io::Result<()> {
let path = format!("disk/{}", path);
let mut file = File::create(path)?;
let mut repo_list_str = String::new();
for (repo_name, repo_url) in repolist.as_table().unwrap() {
let entry = format!("{} = {}\n", repo_name, repo_url);
repo_list_str.push_str(&entry);
}
file.write_all(repo_list_str.as_bytes())?;
Ok(())
}
pub fn generate_password_hashlist(passhash_list: Vec<(String, String)>) -> std::io::Result<()> {
let mut file = File::create("disk/passwords.toml")?;
let mut file_cont = String::new();
for (user, hash) in passhash_list {
let ret = format!("{}={}\n", user, hash);
file_cont.push_str(&ret);
}
file.write_all(file_cont.as_bytes())?;
Ok(())
}