Skip to content

Commit c83c76e

Browse files
committed
v0.2.0 refactored some fuctions for better performance
1 parent fc304a2 commit c83c76e

File tree

13 files changed

+285
-263
lines changed

13 files changed

+285
-263
lines changed

src/functionality.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
/*
22
* gnulinwiz AKA GNU/Linux Config Wizard: The ultimate post-installation setup assistant for Linux,
33
* streamlining your configuration process with ease and precision.
4-
*
4+
*
55
* Copyright (C) 2025 Andrew Kushyk
6-
*
6+
*
77
* This program is free software: you can redistribute it and/or modify
88
* it under the terms of the GNU General Public License as published by
99
* the Free Software Foundation, either version 3 of the License, or
1010
* (at your option) any later version.
11-
*
11+
*
1212
* This program is distributed in the hope that it will be useful,
1313
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1414
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1515
* GNU General Public License for more details.
16-
*
16+
*
1717
* You should have received a copy of the GNU General Public License
1818
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1919
*/

src/functionality/commands.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
/*
22
* gnulinwiz AKA GNU/Linux Config Wizard: The ultimate post-installation setup assistant for Linux,
33
* streamlining your configuration process with ease and precision.
4-
*
4+
*
55
* Copyright (C) 2025 Andrew Kushyk
6-
*
6+
*
77
* This program is free software: you can redistribute it and/or modify
88
* it under the terms of the GNU General Public License as published by
99
* the Free Software Foundation, either version 3 of the License, or
1010
* (at your option) any later version.
11-
*
11+
*
1212
* This program is distributed in the hope that it will be useful,
1313
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1414
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1515
* GNU General Public License for more details.
16-
*
16+
*
1717
* You should have received a copy of the GNU General Public License
1818
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1919
*/
@@ -39,7 +39,7 @@ pub fn run_sudo_command(command: &str, args: &[&str]) -> Result<(), String> {
3939
String::from_utf8_lossy(&output.stderr).trim()
4040
))
4141
}
42-
} // run_sudo_command
42+
}
4343

4444
/// runs user commands
4545
pub fn run_user_command(command: &str, args: &[&str]) -> Result<(), String> {
@@ -58,7 +58,7 @@ pub fn run_user_command(command: &str, args: &[&str]) -> Result<(), String> {
5858
String::from_utf8_lossy(&output.stderr).trim()
5959
))
6060
}
61-
} // run_user_command
61+
}
6262

6363
/// runs sudo commands with stdin
6464
pub fn run_sudo_command_with_stdin(
@@ -94,4 +94,4 @@ pub fn run_sudo_command_with_stdin(
9494
String::from_utf8_lossy(&output.stderr).trim()
9595
))
9696
}
97-
} // run_sudo_command_with_stdin
97+
}

src/functionality/configs.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
/*
22
* gnulinwiz AKA GNU/Linux Config Wizard: The ultimate post-installation setup assistant for Linux,
33
* streamlining your configuration process with ease and precision.
4-
*
4+
*
55
* Copyright (C) 2025 Andrew Kushyk
6-
*
6+
*
77
* This program is free software: you can redistribute it and/or modify
88
* it under the terms of the GNU General Public License as published by
99
* the Free Software Foundation, either version 3 of the License, or
1010
* (at your option) any later version.
11-
*
11+
*
1212
* This program is distributed in the hope that it will be useful,
1313
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1414
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1515
* GNU General Public License for more details.
16-
*
16+
*
1717
* You should have received a copy of the GNU General Public License
1818
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1919
*/
@@ -23,7 +23,7 @@ use colored::Colorize;
2323
use std::path::Path;
2424

2525
/// sets up config files in home directory
26-
pub fn user_config_setup(config_path: String, home_dir: String, cfg_name: &str) -> i8 {
26+
pub fn user_config_setup(config_path: &str, home_dir: &str, cfg_name: &str) -> i8 {
2727
let source = Path::new(&config_path);
2828
let filename = source.file_name();
2929

@@ -54,10 +54,10 @@ pub fn user_config_setup(config_path: String, home_dir: String, cfg_name: &str)
5454
return 1;
5555
}
5656
}
57-
} // user_config_setup
57+
}
5858

5959
/// sets up root config in /root directory
60-
pub fn setup_root_config(home_dir: String) -> i8 {
60+
pub fn setup_root_config(home_dir: &str) -> i8 {
6161
let oh_my_zsh_src = format!("{}{}", home_dir, "/.oh-my-zsh");
6262
let oh_my_zsh_dest = String::from("/root/.oh-my-zsh");
6363
let zshrc_src = format!("{}{}", home_dir, "/.zshrc");
@@ -108,4 +108,4 @@ pub fn setup_root_config(home_dir: String) -> i8 {
108108
}
109109

110110
return 0;
111-
} // setup_root_config
111+
}

src/functionality/env.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
/*
22
* gnulinwiz AKA GNU/Linux Config Wizard: The ultimate post-installation setup assistant for Linux,
33
* streamlining your configuration process with ease and precision.
4-
*
4+
*
55
* Copyright (C) 2025 Andrew Kushyk
6-
*
6+
*
77
* This program is free software: you can redistribute it and/or modify
88
* it under the terms of the GNU General Public License as published by
99
* the Free Software Foundation, either version 3 of the License, or
1010
* (at your option) any later version.
11-
*
11+
*
1212
* This program is distributed in the hope that it will be useful,
1313
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1414
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1515
* GNU General Public License for more details.
16-
*
16+
*
1717
* You should have received a copy of the GNU General Public License
1818
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1919
*/
@@ -22,23 +22,15 @@ use colored::Colorize;
2222
use std::env::var;
2323

2424
/// gets environment variables
25-
fn get_env_var(env_var: &str) -> Option<String> {
25+
/// Returns the value as an owned String if found, None otherwise.
26+
/// Prints a basic error message if the variable is missing or reading fails.
27+
pub fn get_env_var(env_var: &str) -> Option<String> {
2628
match var(env_var) {
27-
Ok(username) => Some(username),
28-
Err(_) => {
29-
eprintln!(
30-
"{}\n",
31-
"error: could not determine the environment variables.".red()
32-
);
29+
Ok(value) => Some(value),
30+
Err(e) => {
31+
eprintln!("{} failed to get environment variable '{}': {}",
32+
"error:".red(), env_var, e);
3333
None
3434
}
3535
}
36-
} // get_env_var
37-
38-
/// validates environment variables
39-
pub fn validate_env_var(env_var: &str) -> String {
40-
match get_env_var(env_var) {
41-
Some(env_var) => env_var,
42-
None => "".to_string(),
43-
}
44-
} // validate_env_var
36+
}

src/functionality/iptables.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
/*
22
* gnulinwiz AKA GNU/Linux Config Wizard: The ultimate post-installation setup assistant for Linux,
33
* streamlining your configuration process with ease and precision.
4-
*
4+
*
55
* Copyright (C) 2025 Andrew Kushyk
6-
*
6+
*
77
* This program is free software: you can redistribute it and/or modify
88
* it under the terms of the GNU General Public License as published by
99
* the Free Software Foundation, either version 3 of the License, or
1010
* (at your option) any later version.
11-
*
11+
*
1212
* This program is distributed in the hope that it will be useful,
1313
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1414
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1515
* GNU General Public License for more details.
16-
*
16+
*
1717
* You should have received a copy of the GNU General Public License
1818
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1919
*/
@@ -48,7 +48,7 @@ pub fn iptables_file_setup() -> i8 {
4848
2
4949
}
5050
}
51-
} // iptables_setup()
51+
}
5252

5353
/// immediately sets up iptables rules
5454
pub fn iptables_rules_setup() -> i8 {
@@ -66,4 +66,4 @@ pub fn iptables_rules_setup() -> i8 {
6666
1
6767
}
6868
}
69-
} // iptables_rules_setup()
69+
}

src/functionality/prog_fun.rs

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,129 @@
11
/*
22
* gnulinwiz AKA GNU/Linux Config Wizard: The ultimate post-installation setup assistant for Linux,
33
* streamlining your configuration process with ease and precision.
4-
*
4+
*
55
* Copyright (C) 2025 Andrew Kushyk
6-
*
6+
*
77
* This program is free software: you can redistribute it and/or modify
88
* it under the terms of the GNU General Public License as published by
99
* the Free Software Foundation, either version 3 of the License, or
1010
* (at your option) any later version.
11-
*
11+
*
1212
* This program is distributed in the hope that it will be useful,
1313
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1414
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1515
* GNU General Public License for more details.
16-
*
16+
*
1717
* You should have received a copy of the GNU General Public License
1818
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1919
*/
2020

2121
use colored::Colorize;
2222
use std::{io::stdin, process::exit};
2323

24-
/// default software installation package
25-
pub fn default_sw_package() -> Vec<String> {
26-
vec![
27-
"firefox".to_string(),
28-
"clang".to_string(),
29-
"zsh".to_string(),
30-
"git".to_string(),
31-
"gimp".to_string(),
32-
"mpv".to_string(),
33-
"spectacle".to_string(),
34-
"curl".to_string(),
24+
/// default software installation package list
25+
/// Returns a slice of static strings.
26+
pub fn default_sw_package() -> &'static [&'static str] {
27+
&[
28+
"firefox",
29+
"clang",
30+
"zsh",
31+
"git",
32+
"gimp",
33+
"mpv",
34+
"spectacle",
35+
"curl",
3536
]
36-
} // default_package()
37+
}
3738

3839
/// prints license information
3940
pub fn print_license_info() {
40-
println!("gnulinwiz AKA GNU/Linux Config Wizard Copyright (C) 2025 Andrew Kushyk\n\
41-
This program comes with ABSOLUTELY NO WARRANTY; for details see https://www.gnu.org/licenses/gpl-3.0.html/\n\
41+
let link = "https://www.gnu.org/licenses/gpl-3.0.html/".blue();
42+
println!(
43+
"gnulinwiz AKA GNU/Linux Config Wizard Copyright (C) 2025 Andrew Kushyk\n\
44+
This program comes with ABSOLUTELY NO WARRANTY; for details see {}\n\
4245
This is free software, and you are welcome to redistribute it\n\
43-
under certain conditions; for details see https://www.gnu.org/licenses/gpl-3.0.html/\n");
44-
} // print_license_info()
46+
under certain conditions; for details see {}\n",
47+
link, link
48+
);
49+
}
4550

46-
/// validates if user has root priviliges. Terminates the program otherwise.
51+
/// validates if user has root privileges. Terminates the program otherwise.
52+
///
53+
/// Running this tool with root privileges is generally unsafe and unnecessary.
54+
/// This check prevents root execution.
4755
pub fn validate_root_priviliges() {
4856
if unsafe { libc::getuid() } == 0 {
4957
eprintln!("{}", "this program is not recommended to run with root privileges. please run it with your current user.\nexample: ./gnulinwiz".red());
5058
// exits if root
5159
exit(1);
5260
}
53-
} // validate_root_priviliges()
61+
}
5462

5563
/// prints green-colored success message if set up successfully
5664
pub fn print_setup_status_success() {
5765
println!(
5866
"{}",
5967
"all set! your gnu/linux system is ready to use!".green()
6068
);
61-
} // print_setup_status_success()
69+
}
6270

6371
/// prints red-colored failure message if set up unseccessfully
6472
pub fn print_setup_status_failed() {
6573
println!(
6674
"{}",
6775
"something went wrong... please fix the reported problems and re-run the program.".red()
6876
);
69-
} // print_setup_status_failed()
77+
}
7078

71-
/// checks wether user wants to use default installation list or to enter a custom one
79+
/// checks whether user wants to use default installation list or to enter a custom one
80+
/// Returns true for custom list, false for default. Exits on invalid input.
7281
pub fn check_sw_install_type() -> bool {
7382
println!(
7483
"{}",
7584
"enter any number for the default list of software or 0 to enter a custom list:".yellow()
7685
);
7786

78-
let input = read_input().trim().to_string();
87+
let input = read_input();
7988

80-
match input.parse::<i8>() {
89+
match input.trim().parse::<i8>() {
8190
Ok(value) if value == 0 => {
8291
println!("{}", "you chose to enter a custom list.\ninstallation takes a few minutes, please wait...".green());
83-
true
92+
return true;
8493
}
8594
Ok(_) => {
8695
println!("{}", "you chose to use the default list.\ninstallation takes a few minutes, please wait...".green());
87-
false
96+
return false;
8897
}
8998
Err(_) => {
90-
handle_error("please enter a valid number (0 or 1).");
91-
false
99+
handle_error(
100+
"please enter 0 for a custom list or any other number for the default list.",
101+
)
92102
}
93103
}
94-
} // check_sw_install_type
104+
}
95105

96106
/// Reads a line of input from the user.
107+
/// Handles read errors by calling handle_error and exiting.
97108
fn read_input() -> String {
98109
let mut input = String::new();
99110
if let Err(error) = stdin().read_line(&mut input) {
100-
handle_error(&format!("{}{}", "error reading input:".red(), error));
101-
exit(1);
111+
handle_error(&format!("{}{}", "error reading input: ".red(), error))
102112
}
103-
input
104-
} // read_input
113+
return input;
114+
}
105115

106-
/// Handles errors by printing the error message and performing any necessary cleanup.
107-
fn handle_error(message: &str) {
116+
/// Handles errors by printing the error message and exiting.
117+
pub fn handle_error(message: &str) -> ! {
108118
eprintln!("{}{}", "error: ".red(), message);
109119
print_setup_status_failed();
110120
exit(1);
111-
} // handle_error
121+
}
112122

113-
/// sets a list of software to install
123+
/// sets a list of software to install by reading user input
124+
/// Uses the read_input function and handles potential errors via it.
114125
pub fn set_sw_list() -> Vec<String> {
115-
let mut packages = Vec::new();
116-
let mut input = String::new();
117-
118126
println!("enter the software packages to install (separated by spaces):");
119-
std::io::stdin()
120-
.read_line(&mut input)
121-
.expect("failed to read line");
122-
123-
packages.extend(input.trim().split_whitespace().map(String::from));
124-
125-
return packages;
126-
} // set_software()
127+
let input = read_input();
128+
return input.trim().split_whitespace().map(String::from).collect();
129+
}

0 commit comments

Comments
 (0)