|
1 | 1 | /*
|
2 | 2 | * gnulinwiz AKA GNU/Linux Config Wizard: The ultimate post-installation setup assistant for Linux,
|
3 | 3 | * streamlining your configuration process with ease and precision.
|
4 |
| - * |
| 4 | + * |
5 | 5 | * Copyright (C) 2025 Andrew Kushyk
|
6 |
| - * |
| 6 | + * |
7 | 7 | * This program is free software: you can redistribute it and/or modify
|
8 | 8 | * it under the terms of the GNU General Public License as published by
|
9 | 9 | * the Free Software Foundation, either version 3 of the License, or
|
10 | 10 | * (at your option) any later version.
|
11 |
| - * |
| 11 | + * |
12 | 12 | * This program is distributed in the hope that it will be useful,
|
13 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 | 15 | * GNU General Public License for more details.
|
16 |
| - * |
| 16 | + * |
17 | 17 | * You should have received a copy of the GNU General Public License
|
18 | 18 | * along with this program. If not, see <https://www.gnu.org/licenses/>.
|
19 | 19 | */
|
20 | 20 |
|
21 | 21 | use colored::Colorize;
|
22 | 22 | use std::{io::stdin, process::exit};
|
23 | 23 |
|
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", |
35 | 36 | ]
|
36 |
| -} // default_package() |
| 37 | +} |
37 | 38 |
|
38 | 39 | /// prints license information
|
39 | 40 | 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\ |
42 | 45 | 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 | +} |
45 | 50 |
|
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. |
47 | 55 | pub fn validate_root_priviliges() {
|
48 | 56 | if unsafe { libc::getuid() } == 0 {
|
49 | 57 | eprintln!("{}", "this program is not recommended to run with root privileges. please run it with your current user.\nexample: ./gnulinwiz".red());
|
50 | 58 | // exits if root
|
51 | 59 | exit(1);
|
52 | 60 | }
|
53 |
| -} // validate_root_priviliges() |
| 61 | +} |
54 | 62 |
|
55 | 63 | /// prints green-colored success message if set up successfully
|
56 | 64 | pub fn print_setup_status_success() {
|
57 | 65 | println!(
|
58 | 66 | "{}",
|
59 | 67 | "all set! your gnu/linux system is ready to use!".green()
|
60 | 68 | );
|
61 |
| -} // print_setup_status_success() |
| 69 | +} |
62 | 70 |
|
63 | 71 | /// prints red-colored failure message if set up unseccessfully
|
64 | 72 | pub fn print_setup_status_failed() {
|
65 | 73 | println!(
|
66 | 74 | "{}",
|
67 | 75 | "something went wrong... please fix the reported problems and re-run the program.".red()
|
68 | 76 | );
|
69 |
| -} // print_setup_status_failed() |
| 77 | +} |
70 | 78 |
|
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. |
72 | 81 | pub fn check_sw_install_type() -> bool {
|
73 | 82 | println!(
|
74 | 83 | "{}",
|
75 | 84 | "enter any number for the default list of software or 0 to enter a custom list:".yellow()
|
76 | 85 | );
|
77 | 86 |
|
78 |
| - let input = read_input().trim().to_string(); |
| 87 | + let input = read_input(); |
79 | 88 |
|
80 |
| - match input.parse::<i8>() { |
| 89 | + match input.trim().parse::<i8>() { |
81 | 90 | Ok(value) if value == 0 => {
|
82 | 91 | println!("{}", "you chose to enter a custom list.\ninstallation takes a few minutes, please wait...".green());
|
83 |
| - true |
| 92 | + return true; |
84 | 93 | }
|
85 | 94 | Ok(_) => {
|
86 | 95 | println!("{}", "you chose to use the default list.\ninstallation takes a few minutes, please wait...".green());
|
87 |
| - false |
| 96 | + return false; |
88 | 97 | }
|
89 | 98 | 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 | + ) |
92 | 102 | }
|
93 | 103 | }
|
94 |
| -} // check_sw_install_type |
| 104 | +} |
95 | 105 |
|
96 | 106 | /// Reads a line of input from the user.
|
| 107 | +/// Handles read errors by calling handle_error and exiting. |
97 | 108 | fn read_input() -> String {
|
98 | 109 | let mut input = String::new();
|
99 | 110 | 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)) |
102 | 112 | }
|
103 |
| - input |
104 |
| -} // read_input |
| 113 | + return input; |
| 114 | +} |
105 | 115 |
|
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) -> ! { |
108 | 118 | eprintln!("{}{}", "error: ".red(), message);
|
109 | 119 | print_setup_status_failed();
|
110 | 120 | exit(1);
|
111 |
| -} // handle_error |
| 121 | +} |
112 | 122 |
|
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. |
114 | 125 | pub fn set_sw_list() -> Vec<String> {
|
115 |
| - let mut packages = Vec::new(); |
116 |
| - let mut input = String::new(); |
117 |
| - |
118 | 126 | 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