From b3d6ec3143c89d8a0c4f813faf693198c2b908f9 Mon Sep 17 00:00:00 2001 From: amy Date: Sun, 23 Jan 2022 00:17:28 +0100 Subject: [PATCH] locale configuration --- src/functions/locale.rs | 52 ++++++++++ src/functions/mod.rs | 1 + src/functions/partition.rs | 189 ++++++++++++++++++++++--------------- src/internal/exec.rs | 23 ++++- src/main.rs | 13 ++- 5 files changed, 195 insertions(+), 83 deletions(-) create mode 100755 src/functions/locale.rs diff --git a/src/functions/locale.rs b/src/functions/locale.rs new file mode 100755 index 0000000..dc50856 --- /dev/null +++ b/src/functions/locale.rs @@ -0,0 +1,52 @@ +use crate::internal::exec::*; +use crate::internal::*; + +pub fn set_timezone(timezone: &str) { + exec_eval(exec( + "ln", + vec![ + "-sf".to_string(), + format!("/usr/share/zoneinfo/{}", timezone), + "/etc/localtime".to_string(), + ], + )); + exec_eval(exec_chroot("hwclock", vec!["--systohc".to_string()])); +} + +pub fn set_locale(locale: String) { + files_eval(files::append_file("/etc/locale.gen", "en_US.UTF-8 UTF-8")); + files_eval(files::append_file("/etc/locale.gen", locale.as_str())); + exec_eval(exec_chroot("locale-gen", vec!["".to_string()])); + files::create_file("/etc/locale.conf"); + files_eval(files::append_file("/etc/locale.conf", "LANG=en_US.UTF-8")); +} + +pub fn set_keyboard(keyboard: &str) { + files::create_file("/etc/vconsole.conf"); + files_eval(files::append_file( + "/etc/vconsole.conf", + format!("KEYMAP={}", keyboard).as_str(), + )); +} + +fn files_eval(return_code: std::result::Result<(), std::io::Error>) { + match return_code { + Ok(_) => { + log("Success".to_string()); + } + Err(e) => { + crash(format!("Failed to create file, Error: {}", e), 1); + } + } +} + +fn exec_eval(return_code: std::result::Result) { + match return_code { + Ok(_) => { + log("Success".to_string()); + } + Err(e) => { + crash(format!("Failed with error: {}", e), 1); + } + } +} diff --git a/src/functions/mod.rs b/src/functions/mod.rs index 871009c..74bfa5f 100755 --- a/src/functions/mod.rs +++ b/src/functions/mod.rs @@ -1,4 +1,5 @@ pub mod desktops; +pub mod locale; pub mod network; pub mod partition; pub mod users; diff --git a/src/functions/partition.rs b/src/functions/partition.rs index 33170f6..257986f 100755 --- a/src/functions/partition.rs +++ b/src/functions/partition.rs @@ -81,23 +81,33 @@ pub fn partition(device: &str, mode: &str, efi: bool) { fn part_nvme(device: &str, efi: bool) { if efi { - returncode_eval(exec("mkfs.vfat", vec![ - String::from(format!("{}p1", device)), - ])); - returncode_eval(exec("mkfs.btrfs", vec![ - String::from(format!("{}p2", device)), - ])); + returncode_eval(exec( + "mkfs.vfat", + vec![String::from(format!("{}p1", device))], + )); + returncode_eval(exec( + "mkfs.btrfs", + vec![String::from(format!("{}p2", device))], + )); mount(format!("{}p2", device), "/mnt", ""); - returncode_eval(exec_workdir("btrfs", "/mnt", vec![ - String::from("subvolume"), - String::from("create"), - String::from("@"), - ])); - returncode_eval(exec_workdir("btrfs", "/mnt", vec![ - String::from("subvolume"), - String::from("create"), - String::from("@home"), - ])); + returncode_eval(exec_workdir( + "btrfs", + "/mnt", + vec![ + String::from("subvolume"), + String::from("create"), + String::from("@"), + ], + )); + returncode_eval(exec_workdir( + "btrfs", + "/mnt", + vec![ + String::from("subvolume"), + String::from("create"), + String::from("@home"), + ], + )); umount("/mnt"); mount(format!("{}p2", device), "/mnt/", "subvol=@"); files::create_directory("/mnt/boot"); @@ -106,23 +116,33 @@ fn part_nvme(device: &str, efi: bool) { mount(format!("{}p2", device), "/mnt/home", "subvol=@home"); mount(format!("{}p1", device), "/mnt/boot/efi", ""); } else { - returncode_eval(exec("mkfs.ext4", vec![ - String::from(format!("{}p1", device)), - ])); - returncode_eval(exec("mkfs.btrfs", vec![ - String::from(format!("{}p2", device)), - ])); + returncode_eval(exec( + "mkfs.ext4", + vec![String::from(format!("{}p1", device))], + )); + returncode_eval(exec( + "mkfs.btrfs", + vec![String::from(format!("{}p2", device))], + )); mount(format!("{}p2", device), "/mnt/", ""); - returncode_eval(exec_workdir("btrfs", "/mnt", vec![ - String::from("subvolume"), - String::from("create"), - String::from("@"), - ])); - returncode_eval(exec_workdir("btrfs", "/mnt", vec![ - String::from("subvolume"), - String::from("create"), - String::from("@home"), - ])); + returncode_eval(exec_workdir( + "btrfs", + "/mnt", + vec![ + String::from("subvolume"), + String::from("create"), + String::from("@"), + ], + )); + returncode_eval(exec_workdir( + "btrfs", + "/mnt", + vec![ + String::from("subvolume"), + String::from("create"), + String::from("@home"), + ], + )); umount("/mnt"); mount(format!("{}p2", device), "/mnt/", "subvol=@"); files::create_directory("/mnt/boot"); @@ -134,23 +154,33 @@ fn part_nvme(device: &str, efi: bool) { fn part_disk(device: &str, efi: bool) { if efi { - returncode_eval(exec("mkfs.vfat", vec![ - String::from(format!("{}1", device)), - ])); - returncode_eval(exec("mkfs.btrfs", vec![ - String::from(format!("{}2", device)), - ])); + returncode_eval(exec( + "mkfs.vfat", + vec![String::from(format!("{}1", device))], + )); + returncode_eval(exec( + "mkfs.btrfs", + vec![String::from(format!("{}2", device))], + )); mount(format!("{}2", device), "/mnt", ""); - returncode_eval(exec_workdir("btrfs", "/mnt", vec![ - String::from("subvolume"), - String::from("create"), - String::from("@"), - ])); - returncode_eval(exec_workdir("btrfs", "/mnt", vec![ - String::from("subvolume"), - String::from("create"), - String::from("@home"), - ])); + returncode_eval(exec_workdir( + "btrfs", + "/mnt", + vec![ + String::from("subvolume"), + String::from("create"), + String::from("@"), + ], + )); + returncode_eval(exec_workdir( + "btrfs", + "/mnt", + vec![ + String::from("subvolume"), + String::from("create"), + String::from("@home"), + ], + )); umount("/mnt"); mount(format!("{}2", device), "/mnt/", "subvol=@"); files::create_directory("/mnt/boot"); @@ -159,23 +189,33 @@ fn part_disk(device: &str, efi: bool) { mount(format!("{}2", device), "/mnt/home", "subvol=@home"); mount(format!("{}1", device), "/mnt/boot/efi", ""); } else { - returncode_eval(exec("mkfs.ext4", vec![ - String::from(format!("{}1", device)), - ])); - returncode_eval(exec("mkfs.btrfs", vec![ - String::from(format!("{}2", device)), - ])); + returncode_eval(exec( + "mkfs.ext4", + vec![String::from(format!("{}1", device))], + )); + returncode_eval(exec( + "mkfs.btrfs", + vec![String::from(format!("{}2", device))], + )); mount(format!("{}2", device), "/mnt/", ""); - returncode_eval(exec_workdir("btrfs", "/mnt", vec![ - String::from("subvolume"), - String::from("create"), - String::from("@"), - ])); - returncode_eval(exec_workdir("btrfs", "/mnt", vec![ - String::from("subvolume"), - String::from("create"), - String::from("@home"), - ])); + returncode_eval(exec_workdir( + "btrfs", + "/mnt", + vec![ + String::from("subvolume"), + String::from("create"), + String::from("@"), + ], + )); + returncode_eval(exec_workdir( + "btrfs", + "/mnt", + vec![ + String::from("subvolume"), + String::from("create"), + String::from("@home"), + ], + )); umount("/mnt"); mount(format!("{}2", device), "/mnt/", "subvol=@"); files::create_directory("/mnt/boot"); @@ -187,18 +227,19 @@ fn part_disk(device: &str, efi: bool) { fn mount(partition: String, mountpoint: &str, options: &str) { let options = if options.is_empty() { "\"\"" } else { options }; - returncode_eval(exec("mount", vec![ - String::from(partition), - String::from(mountpoint), - String::from("-o"), - String::from(options), - ])); + returncode_eval(exec( + "mount", + vec![ + String::from(partition), + String::from(mountpoint), + String::from("-o"), + String::from(options), + ], + )); } fn umount(mountpoint: &str) { - returncode_eval(exec("umount", vec![ - String::from(mountpoint), - ])); + returncode_eval(exec("umount", vec![String::from(mountpoint)])); } fn returncode_eval(return_code: std::result::Result) { @@ -210,4 +251,4 @@ fn returncode_eval(return_code: std::result::Result) -> Result) -> Result { - let returncode = Command::new("arch-chroot").args(&["/mnt", command]).args(args).output(); +pub fn exec_chroot( + command: &str, + args: Vec, +) -> Result { + let returncode = Command::new("arch-chroot") + .args(&["/mnt", command]) + .args(args) + .output(); returncode } -pub fn exec_workdir(command: &str, workdir: &str, args: Vec,) -> Result { - let returncode = Command::new(command).args(args).current_dir(workdir).output(); +pub fn exec_workdir( + command: &str, + workdir: &str, + args: Vec, +) -> Result { + let returncode = Command::new(command) + .args(args) + .current_dir(workdir) + .output(); returncode -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index 175175a..db16934 100755 --- a/src/main.rs +++ b/src/main.rs @@ -115,10 +115,15 @@ fn main() { } else if let Some(app) = app.subcommand_matches("locale") { let kbrlayout = app.value_of("keyboard").unwrap(); let timezn = app.value_of("timezone").unwrap(); - let locale = app.values_of("locales").unwrap(); - println!("keyboard layout: {}", kbrlayout); - println!("timezone: {}", timezn); - println!("locales: {:?}", locale); + let locale: String = app + .values_of("locales") + .unwrap() + .into_iter() + .map(|s| s.to_string()) + .collect(); + locale::set_locale(locale); + locale::set_keyboard(kbrlayout); + locale::set_timezone(timezn); } else if let Some(app) = app.subcommand_matches("networking") { network::enable_ipv6(app.value_of("ipv6").unwrap().parse::().unwrap()); network::set_hostname(app.value_of("hostname").unwrap())