From 6665868da74dc75f0032b43da8d238090d0d8d45 Mon Sep 17 00:00:00 2001 From: amy Date: Sat, 22 Jan 2022 17:37:54 +0100 Subject: [PATCH] move command execution to src/internal/exec.rs and add first part of partitioning --- src/functions/desktops.rs | 2 +- src/functions/network.rs | 7 +- src/functions/partition.rs | 134 ++++++++++++++++++++++++++++++++++++- src/functions/users.rs | 98 +++++++++++++++------------ src/internal/exec.rs | 6 ++ src/internal/files.rs | 10 +-- src/internal/install.rs | 0 src/internal/mod.rs | 1 + src/internal/strings.rs | 3 +- src/main.rs | 7 ++ 10 files changed, 210 insertions(+), 58 deletions(-) create mode 100755 src/internal/exec.rs mode change 100644 => 100755 src/internal/files.rs mode change 100644 => 100755 src/internal/install.rs mode change 100644 => 100755 src/internal/mod.rs mode change 100644 => 100755 src/internal/strings.rs diff --git a/src/functions/desktops.rs b/src/functions/desktops.rs index df8380f..d684a8d 100755 --- a/src/functions/desktops.rs +++ b/src/functions/desktops.rs @@ -1,7 +1,7 @@ use crate::internal::*; pub fn choose_pkgs(desktop_setup: &str) { - println!("Installing {}", desktop_setup); + log(format!("Installing {}", desktop_setup)); match desktop_setup { "onyx" => { install(vec![ diff --git a/src/functions/network.rs b/src/functions/network.rs index 69aaf3b..fe2403d 100755 --- a/src/functions/network.rs +++ b/src/functions/network.rs @@ -6,7 +6,7 @@ pub fn set_hostname(hostname: &str) { let return_val = files::append_file("/etc/hostname", hostname); match return_val { Ok(_) => { - info(format!("Set hostname to {}", hostname)); + log(format!("Set hostname to {}", hostname)); } Err(e) => { crash( @@ -19,17 +19,16 @@ pub fn set_hostname(hostname: &str) { pub fn enable_ipv6(ipv6: bool) { if ipv6 { - println!("Enabling IPv6"); let return_val = files::append_file("/etc/hosts", "::1 localhost"); match return_val { Ok(_) => { - info("Enabled IPv6".to_string()); + log("Enabled IPv6".to_string()); } Err(e) => { crash(format!("Failed to enable IPv6, Error: {}", e), 1); } } } else { - println!("Not enabling ipv6"); + log("Not enabling ipv6".to_string()); } } diff --git a/src/functions/partition.rs b/src/functions/partition.rs index 7894051..4dad15f 100755 --- a/src/functions/partition.rs +++ b/src/functions/partition.rs @@ -1,7 +1,135 @@ -pub fn partition(device: &str, mode: &str) { +use crate::internal::exec::*; +use crate::internal::*; + +pub fn partition(device: &str, mode: &str, efi: bool) { if mode == "manual" { - println!("Manual partitioning"); + log("Manual partitioning".to_string()); } else { - println!("automatically partitioning {}", device); + log(format!("automatically partitioning {}", device)); + if efi { + let return_code = exec( + "parted", + vec![ + String::from("-s"), + String::from(device), + String::from("mklabel"), + String::from("gpt"), + ], + ); + match return_code { + Ok(_) => { + log("Created GPT label".to_string()); + } + Err(e) => { + crash(format!("Failed to create GPT label, Error: {}", e), 1); + } + } + let return_code = exec( + "parted", + vec![ + String::from("-s"), + String::from(device), + String::from("mkpart"), + String::from("fat32"), + String::from("0"), + String::from("300"), + ], + ); + match return_code { + Ok(_) => { + log("Created fat32 EFI partition".to_string()); + } + Err(e) => { + crash( + format!("Failed to create fat32 EFI partition, Error: {}", e), + 1, + ); + } + } + let return_code = exec( + "parted", + vec![ + String::from("-s"), + String::from(device), + String::from("mkpart"), + String::from("btrfs"), + String::from("300"), + String::from("100%"), + ], + ); + match return_code { + Ok(_) => { + log("Created btrfs root partition".to_string()); + } + Err(e) => { + crash( + format!("Failed to create btrfs root partition, Error: {}", e), + 1, + ); + } + } + } else { + let return_code = exec( + "parted", + vec![ + String::from("-s"), + String::from(device), + String::from("mklabel"), + String::from("msdos"), + ], + ); + match return_code { + Ok(_) => { + log("Created MSDOS label".to_string()); + } + Err(e) => { + crash(format!("Failed to create MSDOS label, Error: {}", e), 1); + } + } + let return_code = exec( + "parted", + vec![ + String::from("-s"), + String::from(device), + String::from("mkpart"), + String::from("btrfs"), + String::from("512MIB"), + String::from("100&"), + ], + ); + match return_code { + Ok(_) => { + log("Created btrfs root partition".to_string()); + } + Err(e) => { + crash( + format!("Failed to create btrfs root partition, Error: {}", e), + 1, + ); + } + } + let return_code = exec( + "parted", + vec![ + String::from("-s"), + String::from(device), + String::from("mkpart"), + String::from("ext4"), + String::from("1MIB"), + String::from("512MIB"), + ], + ); + match return_code { + Ok(_) => { + log("Created ext4 boot partition".to_string()); + } + Err(e) => { + crash( + format!("Failed to create ext4 boot partition, Error: {}", e), + 1, + ); + } + } + } } } diff --git a/src/functions/users.rs b/src/functions/users.rs index 0bf3582..a4afab5 100755 --- a/src/functions/users.rs +++ b/src/functions/users.rs @@ -1,16 +1,19 @@ +use crate::internal::exec::*; use crate::internal::*; -use std::process::Command; pub fn new_user(username: &str, hasroot: bool, password: &str) { - let return_val = Command::new("useradd") - .arg("-m") - .arg("-s") - .arg("/bin/bash") - .arg(username) - .output(); + let return_val = exec( + "useradd", + vec![ + String::from("-m"), + String::from("-s"), + String::from("/bin/bash"), + String::from(username), + ], + ); match return_val { Ok(_) => { - info(format!("Created user {}", username)); + log(format!("Created user {}", username)); } Err(e) => { crash( @@ -20,37 +23,43 @@ pub fn new_user(username: &str, hasroot: bool, password: &str) { } } if hasroot { - let return_val = Command::new("usermod") - .arg("-a") - .arg("-G") - .arg("wheel") - .arg(username) - .output(); + let return_val = exec( + "usermod", + vec![ + String::from("-a"), + String::from("-G"), + String::from("wheel"), + String::from(username), + ], + ); match return_val { Ok(_) => { - info(format!("Added user {} to group wheel", username)); + log(format!("Added user {} to group wheel", username)); } Err(e) => { crash(format!("Failed to add user {}, Error: {}", username, e), 1); } } } - let return_val = Command::new("arch-chroot") - .arg("/mnt") - .arg("usermod") - .arg("--password") - .arg("$(echo") - .arg(format!("${{{}}}", password)) - .arg("|") - .arg("openssl") - .arg("passwd") - .arg("-1") - .arg("-stdin)") - .arg(username) - .output(); + let return_val = exec( + "arch-chroot", + vec![ + String::from("/mnt"), + String::from("usermod"), + String::from("--password"), + String::from("$(echo"), + String::from(format!("${}", password)), + String::from("|"), + String::from("openssl"), + String::from("passwd"), + String::from("-1"), + String::from("-stdin)"), + String::from(username), + ], + ); match return_val { Ok(_) => { - info(format!("Set password for user {}", username)); + log(format!("Set password for user {}", username)); } Err(e) => { crash( @@ -63,22 +72,25 @@ pub fn new_user(username: &str, hasroot: bool, password: &str) { pub fn root_pass(root_pass: &str) { println!("Setting root password to '{}'", root_pass); - let return_val = Command::new("arch-chroot") - .arg("/mnt") - .arg("usermod") - .arg("--password") - .arg("$(echo") - .arg(format!("${{{}}}", root_pass)) - .arg("|") - .arg("openssl") - .arg("passwd") - .arg("-1") - .arg("-stdin)") - .arg("root") - .output(); + let return_val = exec( + "arch-chroot", + vec![ + String::from("/mnt"), + String::from("usermod"), + String::from("--password"), + String::from("$(echo"), + String::from(format!("${{{}}}", root_pass)), + String::from("|"), + String::from("openssl"), + String::from("passwd"), + String::from("-1"), + String::from("-stdin)"), + String::from("root"), + ], + ); match return_val { Ok(_) => { - info("Set root password".to_string()); + log("Set root password".to_string()); } Err(e) => { crash(format!("Failed to set root password, Error: {}", e), 1); diff --git a/src/internal/exec.rs b/src/internal/exec.rs new file mode 100755 index 0000000..035a625 --- /dev/null +++ b/src/internal/exec.rs @@ -0,0 +1,6 @@ +use std::process::Command; + +pub fn exec(command: &str, args: Vec) -> Result { + let returncode = Command::new(command).args(args).output(); + returncode +} diff --git a/src/internal/files.rs b/src/internal/files.rs old mode 100644 new mode 100755 index e8c6f76..bf288a5 --- a/src/internal/files.rs +++ b/src/internal/files.rs @@ -6,7 +6,7 @@ pub fn create_file(path: &str) { let return_val = File::create(path); match return_val { Ok(_file) => { - info(format!("Created file {}", path)); + log(format!("Created file {}", path)); } Err(e) => { crash(format!("Failed to create file {}, Error: {}", path, e), 1); @@ -15,7 +15,7 @@ pub fn create_file(path: &str) { } pub fn append_file(path: &str, content: &str) -> std::io::Result<()> { - info(format!("Appending {} to file {}", content, path)); + log(format!("Appending {} to file {}", content, path)); let mut file = OpenOptions::new().append(true).open(path)?; file.write(content.as_bytes())?; Ok(()) @@ -25,7 +25,7 @@ pub fn delete_file(path: &str) { let return_val = std::fs::remove_file(path); match return_val { Ok(_) => { - info(format!("Deleted file {}", path)); + log(format!("Deleted file {}", path)); } Err(e) => { crash(format!("Failed to delete file {}, Error: {}", path, e), 1); @@ -37,7 +37,7 @@ pub fn create_directory(path: &str) { let return_val = std::fs::create_dir(path); match return_val { Ok(_) => { - info(format!("Created directory {}", path)); + log(format!("Created directory {}", path)); } Err(e) => { crash( @@ -54,7 +54,7 @@ pub fn overwrite_file(path: &str, content: &str) { let return_val = append_file(path, content); match return_val { Ok(_) => { - info(format!("Overwrote file {}", path)); + log(format!("Overwrote file {}", path)); } Err(e) => { crash( diff --git a/src/internal/install.rs b/src/internal/install.rs old mode 100644 new mode 100755 diff --git a/src/internal/mod.rs b/src/internal/mod.rs old mode 100644 new mode 100755 index a32ed5a..dc2da91 --- a/src/internal/mod.rs +++ b/src/internal/mod.rs @@ -1,3 +1,4 @@ +pub mod exec; pub mod files; pub mod install; pub mod strings; diff --git a/src/internal/strings.rs b/src/internal/strings.rs old mode 100644 new mode 100755 index f55e068..21a244a --- a/src/internal/strings.rs +++ b/src/internal/strings.rs @@ -1,9 +1,8 @@ use crate::uwu; -use std::io::Write; +use std::env; use std::process::exit; use std::str::FromStr; use std::time::UNIX_EPOCH; -use std::{env, io}; pub fn info(a: String) { let a = if env::var("JADE_UWU").unwrap_or_else(|_| "".to_string()) == "true" { uwu!(&a) diff --git a/src/main.rs b/src/main.rs index 43195ed..175175a 100755 --- a/src/main.rs +++ b/src/main.rs @@ -22,6 +22,12 @@ fn main() { .help("The device to partition") .required_if("mode", "auto"), ) + .arg( + Arg::with_name("efi") + .help("If the install destination should be partitioned with EFI") + .long("efi") + .takes_value(false), + ), ) .subcommand( SubCommand::with_name("locale") @@ -104,6 +110,7 @@ fn main() { partition::partition( app.value_of("device").unwrap(), app.value_of("mode").unwrap(), + app.is_present("efi"), ); } else if let Some(app) = app.subcommand_matches("locale") { let kbrlayout = app.value_of("keyboard").unwrap();