Skip to content

Commit 0cfabdd

Browse files
committed
v0.2.0 remastered config functions for better performance
1 parent c83c76e commit 0cfabdd

File tree

2 files changed

+29
-43
lines changed

2 files changed

+29
-43
lines changed

src/functionality/configs.rs

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -56,56 +56,42 @@ pub fn user_config_setup(config_path: &str, home_dir: &str, cfg_name: &str) -> i
5656
}
5757
}
5858

59-
/// sets up root config in /root directory
60-
pub fn setup_root_config(home_dir: &str) -> i8 {
61-
let oh_my_zsh_src = format!("{}{}", home_dir, "/.oh-my-zsh");
62-
let oh_my_zsh_dest = String::from("/root/.oh-my-zsh");
63-
let zshrc_src = format!("{}{}", home_dir, "/.zshrc");
64-
let zshrc_dest = String::from("/root/.zshrc");
65-
let vimrc_src = format!("{}{}", home_dir, "/.vimrc");
66-
let vimrc_dest = String::from("/root/.vimrc");
59+
/// Helper function to copy a file or directory as root.
60+
/// Takes source path, destination path, and a description for messages.
61+
/// Uses 'cp -r' via run_sudo_command.
62+
/// Returns 0 on success, 1 on failure.
63+
fn copy_item_as_root(src: &str, dest: &str, description: &str) -> i8 {
64+
let args = &["-r", src, dest];
6765

68-
// Create symbolic link for .oh-my-zsh
69-
match run_sudo_command(
70-
"cp",
71-
&["-r", oh_my_zsh_src.as_str(), oh_my_zsh_dest.as_str()],
72-
) {
73-
Ok(_) => println!("/root/.oh-my-zsh {}", "created configuration".green()),
66+
match run_sudo_command("cp", args) {
67+
Ok(_) => {
68+
println!("{} {}", description, "created configuration".green());
69+
return 0;
70+
}
7471
Err(e) => {
75-
eprintln!(
76-
"{}{}",
77-
"error creating configuration /root/.oh-my-zsh:".red(),
78-
e.red()
79-
);
72+
eprintln!("{} failed to copy '{}' to '{}': {}",
73+
"error:".red(), src, dest, e.red());
8074
return 1;
8175
}
8276
}
77+
}
8378

84-
// Create symbolic link for .zshrc
85-
match run_sudo_command("cp", &["-r", zshrc_src.as_str(), zshrc_dest.as_str()]) {
86-
Ok(_) => println!("/root/.zshrc {}", "created configuration".green()),
87-
Err(e) => {
88-
eprintln!(
89-
"{}{}",
90-
"error creating configuration /root/.zshrc:".red(),
91-
e.red()
92-
);
93-
return 2;
94-
}
95-
}
9679

97-
// Create symbolic link for .vimrc
98-
match run_sudo_command("cp", &["-r", vimrc_src.as_str(), vimrc_dest.as_str()]) {
99-
Ok(_) => println!("/root/.vimrc {}", "created configuration".green()),
100-
Err(e) => {
101-
eprintln!(
102-
"{}{}",
103-
"error creating configuration /root/.vimrc:".red(),
104-
e.red()
105-
);
106-
return 3;
80+
/// sets up root config in /root directory by copying files/directories from user's home
81+
/// Note: Copies .oh-my-zsh, .zshrc, and .vimrc using 'cp -r' via sudo.
82+
pub fn setup_root_config(home_dir: &str) -> i8 {
83+
let items_to_copy = [
84+
(format!("{}/.oh-my-zsh", home_dir), "/root/.oh-my-zsh".to_string(), "/root/.oh-my-zsh"),
85+
(format!("{}/.zshrc", home_dir), "/root/.zshrc".to_string(), "/root/.zshrc"),
86+
(format!("{}/.vimrc", home_dir), "/root/.vimrc".to_string(), "/root/.vimrc"),
87+
];
88+
89+
for (src, dest, desc) in &items_to_copy {
90+
let status = copy_item_as_root(src.as_str(), dest.as_str(), desc);
91+
if status != 0 {
92+
return 1;
10793
}
10894
}
10995

11096
return 0;
111-
}
97+
}

src/functionality/software.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn software_setup(packages: &[&str]) -> i8 {
3434
.args(packages)
3535
.arg("--noconfirm");
3636

37-
println!("{}{} --noconfirm", "Running command: sudo pacman -Sy ".green(), packages.join(" "));
37+
println!("{}{}{}", "Running command: sudo pacman -Sy ".green(), packages.join(" "), " --noconfirm".green());
3838

3939
let output = match command.output() {
4040
Ok(output) => output,

0 commit comments

Comments
 (0)