Skip to content

Commit ce389f8

Browse files
committed
v0.2.0 overall structure and error handling improved
1 parent 0cfabdd commit ce389f8

File tree

9 files changed

+124
-81
lines changed

9 files changed

+124
-81
lines changed

src/functionality/configs.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,37 @@ fn copy_item_as_root(src: &str, dest: &str, description: &str) -> i8 {
6969
return 0;
7070
}
7171
Err(e) => {
72-
eprintln!("{} failed to copy '{}' to '{}': {}",
73-
"error:".red(), src, dest, e.red());
72+
eprintln!(
73+
"{} failed to copy '{}' to '{}': {}",
74+
"error:".red(),
75+
src,
76+
dest,
77+
e.red()
78+
);
7479
return 1;
7580
}
7681
}
7782
}
7883

79-
8084
/// sets up root config in /root directory by copying files/directories from user's home
8185
/// Note: Copies .oh-my-zsh, .zshrc, and .vimrc using 'cp -r' via sudo.
8286
pub fn setup_root_config(home_dir: &str) -> i8 {
8387
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"),
88+
(
89+
format!("{}/.oh-my-zsh", home_dir),
90+
"/root/.oh-my-zsh".to_string(),
91+
"/root/.oh-my-zsh",
92+
),
93+
(
94+
format!("{}/.zshrc", home_dir),
95+
"/root/.zshrc".to_string(),
96+
"/root/.zshrc",
97+
),
98+
(
99+
format!("{}/.vimrc", home_dir),
100+
"/root/.vimrc".to_string(),
101+
"/root/.vimrc",
102+
),
87103
];
88104

89105
for (src, dest, desc) in &items_to_copy {
@@ -94,4 +110,4 @@ pub fn setup_root_config(home_dir: &str) -> i8 {
94110
}
95111

96112
return 0;
97-
}
113+
}

src/functionality/env.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ pub fn get_env_var(env_var: &str) -> Option<String> {
2828
match var(env_var) {
2929
Ok(value) => Some(value),
3030
Err(e) => {
31-
eprintln!("{} failed to get environment variable '{}': {}",
32-
"error:".red(), env_var, e);
31+
eprintln!(
32+
"{} failed to get environment variable '{}': {}",
33+
"error:".red(),
34+
env_var,
35+
e
36+
);
3337
None
3438
}
3539
}

src/functionality/iptables.rs

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,53 @@ use super::commands::{run_sudo_command, run_sudo_command_with_stdin};
2222
use colored::Colorize;
2323
use std::{fs::read_to_string, path::Path};
2424

25-
/// sets up iptables
25+
/// sets up iptables file by reading from source and writing to destination as root
2626
pub fn iptables_file_setup() -> i8 {
2727
let source_path = Path::new("../configs/iptables.rules");
2828
let dest_path = Path::new("/etc/iptables/iptables.rules");
2929

30-
match read_to_string(source_path) {
31-
Ok(rules) => {
32-
let command = "tee";
33-
let args = &[dest_path.as_os_str().to_str().unwrap()];
30+
let rules = match read_to_string(source_path) {
31+
Ok(rules_content) => rules_content,
32+
Err(e) => {
33+
eprintln!(
34+
"{} failed to read iptables rules from source file '{}': {}",
35+
"error:".red(),
36+
source_path.display(),
37+
e
38+
);
39+
return 2;
40+
}
41+
};
42+
43+
let command = "tee";
44+
45+
let dest_path_str = match dest_path.to_str() {
46+
Some(s) => s,
47+
None => {
48+
eprintln!(
49+
"{} destination path '{}' is not valid UTF-8.",
50+
"error:".red(),
51+
dest_path.display()
52+
);
53+
return 1;
54+
}
55+
};
56+
57+
let args = &[dest_path_str];
3458

35-
match run_sudo_command_with_stdin(command, args, rules) {
36-
Ok(_) => {
37-
println!("iptables.rules {}", "created successfully".green());
38-
return 0;
39-
}
40-
Err(e) => {
41-
eprintln!("error setting up iptables file: {}", e);
42-
1
43-
}
44-
}
59+
match run_sudo_command_with_stdin(command, args, rules) {
60+
Ok(()) => {
61+
println!("iptables.rules {}", "created successfully".green());
62+
return 0;
4563
}
4664
Err(e) => {
47-
eprintln!("failed to read iptables rules from source file: {}", e);
48-
2
65+
eprintln!(
66+
"{} failed to write iptables rules to '{}': {}",
67+
"error:".red(),
68+
dest_path.display(),
69+
e.red()
70+
);
71+
return 1;
4972
}
5073
}
5174
}
@@ -59,11 +82,11 @@ pub fn iptables_rules_setup() -> i8 {
5982
match run_sudo_command(command, args) {
6083
Ok(_) => {
6184
println!("iptables.rules {}", "set successfully".green());
62-
0
85+
return 0;
6386
}
6487
Err(e) => {
6588
eprintln!("error applying iptables rules: {}", e);
66-
1
89+
return 1;
6790
}
6891
}
6992
}

src/functionality/prog_fun.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,9 @@ pub fn check_sw_install_type() -> bool {
9595
println!("{}", "you chose to use the default list.\ninstallation takes a few minutes, please wait...".green());
9696
return false;
9797
}
98-
Err(_) => {
99-
handle_error(
100-
"please enter 0 for a custom list or any other number for the default list.",
101-
)
102-
}
98+
Err(_) => handle_error(
99+
"please enter 0 for a custom list or any other number for the default list.",
100+
),
103101
}
104102
}
105103

src/functionality/shell.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,30 @@ pub fn change_def_shell(name: &str) -> i8 {
3030

3131
match run_sudo_command(command, args) {
3232
Ok(_) => {
33-
println!("zsh {}", "shell set successfully".green());
33+
println!("zsh {}{}", "shell set successfully for ".green(), name);
3434
return 0;
3535
}
3636
Err(e) => {
37-
eprintln!("{} failed to change default shell for user '{}': {}",
38-
"error:".red(), name, e.red());
37+
eprintln!(
38+
"{} failed to change default shell for user '{}': {}",
39+
"error:".red(),
40+
name,
41+
e.red()
42+
);
3943
return 1;
4044
}
4145
}
4246
}
4347

44-
4548
/// installs oh my zsh by piping curl output to bash
4649
pub fn install_omz() -> i8 {
4750
let mut curl_cmd = Command::new("curl");
48-
curl_cmd.args(&[
49-
"-fsSL",
50-
"https://github.com/ohmyzsh/ohmyzsh/master/tools/install.sh",
51-
])
52-
.stdout(Stdio::piped());
51+
curl_cmd
52+
.args(&[
53+
"-fsSL",
54+
"https://github.com/ohmyzsh/ohmyzsh/master/tools/install.sh",
55+
])
56+
.stdout(Stdio::piped());
5357

5458
let mut curl_process = match curl_cmd.spawn() {
5559
Ok(process) => process,
@@ -95,9 +99,13 @@ pub fn install_omz() -> i8 {
9599
};
96100

97101
if !curl_status.success() {
98-
eprintln!("{} curl command failed with status: {}",
99-
"error:".red(),
100-
curl_status.code().map_or_else(|| "terminated by signal".into(), |c| c.to_string()));
102+
eprintln!(
103+
"{} curl command failed with status: {}",
104+
"error:".red(),
105+
curl_status
106+
.code()
107+
.map_or_else(|| "terminated by signal".into(), |c| c.to_string())
108+
);
101109
let _ = bash_process.kill();
102110
let _ = bash_process.wait();
103111
return 1;
@@ -118,7 +126,10 @@ pub fn install_omz() -> i8 {
118126
eprintln!(
119127
"{} oh-my-zsh installation script failed with status: {}",
120128
"error:".red(),
121-
output.status.code().map_or_else(|| "terminated by signal".into(), |c| c.to_string())
129+
output
130+
.status
131+
.code()
132+
.map_or_else(|| "terminated by signal".into(), |c| c.to_string())
122133
);
123134
if !output.stdout.is_empty() {
124135
eprintln!("--- Script stdout ---");
@@ -140,17 +151,13 @@ pub fn install_omz() -> i8 {
140151
/// Takes home directory, plugin name (for directory and messages), and repository URL.
141152
fn install_zsh_plugin(home_dir: &str, plugin_name: &str, repo_url: &str) -> i8 {
142153
let zsh_custom_path = format!("{}/.oh-my-zsh/custom/plugins/{}", home_dir, plugin_name);
143-
let args = &[
144-
"clone",
145-
repo_url,
146-
&zsh_custom_path,
147-
];
154+
let args = &["clone", repo_url, &zsh_custom_path];
148155

149156
println!("Cloning {}...", plugin_name.green());
150157

151158
match run_user_command("git", args) {
152159
Ok(_) => {
153-
println!("{} {}", plugin_name.green(), "installed successfully".green());
160+
println!("{} {}", plugin_name, "installed successfully".green());
154161
return 0;
155162
}
156163
Err(e) => {
@@ -167,7 +174,6 @@ fn install_zsh_plugin(home_dir: &str, plugin_name: &str, repo_url: &str) -> i8 {
167174
}
168175
}
169176

170-
171177
/// installs zsh autosuggestions plugin
172178
/// Uses the generic zsh plugin installer.
173179
pub fn install_zsh_autosuggestions(home_dir: &str) -> i8 {

src/functionality/software.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1919
*/
2020

21+
use super::prog_fun::handle_error;
2122
use colored::Colorize;
2223
use std::process::Command;
23-
use super::prog_fun::handle_error;
2424

2525
/// installs software using pacman
2626
/// Takes a slice of string slices for package names.
@@ -34,7 +34,12 @@ pub fn software_setup(packages: &[&str]) -> i8 {
3434
.args(packages)
3535
.arg("--noconfirm");
3636

37-
println!("{}{}{}", "Running command: sudo pacman -Sy ".green(), packages.join(" "), " --noconfirm".green());
37+
println!(
38+
"Running command: {}{}{}",
39+
"sudo pacman -Sy ".green(),
40+
packages.join(" "),
41+
" --noconfirm".green()
42+
);
3843

3944
let output = match command.output() {
4045
Ok(output) => output,

src/functionality/task.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ pub fn validate_task_status(status: i8) {
2727
print_setup_status_failed();
2828
exit(status as i32);
2929
}
30-
}
30+
}

src/functionality/zram.rs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,28 @@
1818
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1919
*/
2020

21+
use super::commands::run_sudo_command;
2122
use colored::Colorize;
22-
use std::process::Command;
2323

24-
/// sets up zram swap configuration
24+
/// sets up zram swap configuration by copying the generator file
2525
pub fn zram_swap_setup() -> i8 {
26-
let output = Command::new("sudo")
27-
.arg("cp")
28-
.arg("../configs/zram-generator.conf")
29-
.arg("/etc/systemd/")
30-
.output();
26+
let source_config_path = "../configs/zram-generator.conf";
27+
let destination_path = "/etc/systemd/zram-generator.conf";
3128

32-
match output {
33-
Ok(output) => {
34-
if output.status.success() {
35-
println!("zram {}", "swap configuration copied successfully.".green());
36-
return 0;
37-
} else {
38-
eprintln!(
39-
"{}{}",
40-
"error copying zram-generator.conf:".red(),
41-
String::from_utf8_lossy(&output.stderr).red()
42-
);
43-
return 1;
44-
}
29+
let result = run_sudo_command("cp", &[source_config_path, destination_path]);
30+
31+
match result {
32+
Ok(()) => {
33+
println!("zram {}", "swap configuration copied successfully.".green());
34+
return 0;
4535
}
4636
Err(e) => {
4737
eprintln!(
48-
"{}{}",
49-
"error executing command:".red(),
50-
e.to_string().red()
38+
"{} failed to copy zram-generator.conf: {}",
39+
"error:".red(),
40+
e.red()
5141
);
52-
return 2;
42+
return 1;
5343
}
5444
}
5545
}

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ use functionality::{
2525
env::get_env_var,
2626
iptables::{iptables_file_setup, iptables_rules_setup},
2727
prog_fun::{
28-
check_sw_install_type, default_sw_package, handle_error, print_license_info, print_setup_status_success, set_sw_list, validate_root_priviliges
28+
check_sw_install_type, default_sw_package, handle_error, print_license_info,
29+
print_setup_status_success, set_sw_list, validate_root_priviliges,
2930
},
3031
shell::{
3132
change_def_shell, install_omz, install_zsh_autosuggestions, install_zsh_syntax_highlighting,

0 commit comments

Comments
 (0)