Skip to content

Commit 415c97e

Browse files
committed
v0.1.0 code structure improved
1 parent 7e2d6de commit 415c97e

File tree

13 files changed

+517
-400
lines changed

13 files changed

+517
-400
lines changed

src/functionality.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
pub mod commands;
2+
pub mod configs;
3+
pub mod env;
4+
pub mod iptables;
15
pub mod prog_fun;
2-
pub mod setup_fun;
6+
pub mod shell;
7+
pub mod software;
8+
pub mod task;
39
pub mod user_cfg;
10+
pub mod zram;

src/functionality/commands.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use colored::Colorize;
2+
use std::{io::Write, process::Command, process::Stdio};
3+
4+
/// runs sudo commands
5+
pub fn run_sudo_command(command: &str, args: &[&str]) -> Result<(), String> {
6+
let output = Command::new("sudo")
7+
.arg(command)
8+
.args(args)
9+
.output()
10+
.map_err(|e| format!("{} {}: {}", "failed to execute command:".red(), command, e))?;
11+
12+
if output.status.success() {
13+
Ok(())
14+
} else {
15+
Err(format!(
16+
"command `{}` failed:\nstdout: {}\nstderr: {}",
17+
command.red(),
18+
String::from_utf8_lossy(&output.stdout).trim(),
19+
String::from_utf8_lossy(&output.stderr).trim()
20+
))
21+
}
22+
} // run_sudo_command
23+
24+
/// runs user commands
25+
pub fn run_user_command(command: &str, args: &[&str]) -> Result<(), String> {
26+
let output = Command::new(command)
27+
.args(args)
28+
.output()
29+
.map_err(|e| format!("{} {}: {}", "failed to execute command:".red(), command, e))?;
30+
31+
if output.status.success() {
32+
Ok(())
33+
} else {
34+
Err(format!(
35+
"command `{}` failed:\nstdout: {}\nstderr: {}",
36+
command.red(),
37+
String::from_utf8_lossy(&output.stdout).trim(),
38+
String::from_utf8_lossy(&output.stderr).trim()
39+
))
40+
}
41+
} // run_user_command
42+
43+
/// runs sudo commands with stdin
44+
pub fn run_sudo_command_with_stdin(
45+
command: &str,
46+
args: &[&str],
47+
stdin_content: String,
48+
) -> Result<(), String> {
49+
let mut cmd = Command::new("sudo")
50+
.arg(command)
51+
.args(args)
52+
.stdin(Stdio::piped())
53+
.stdout(Stdio::null())
54+
.spawn()
55+
.map_err(|e| format!("failed to spawn command `{}`: {}", command, e))?;
56+
57+
if let Some(mut stdin) = cmd.stdin.take() {
58+
stdin
59+
.write_all(stdin_content.as_bytes())
60+
.map_err(|e| format!("failed to write to stdin of `{}`: {}", command, e))?;
61+
}
62+
63+
let output = cmd
64+
.wait_with_output()
65+
.map_err(|e| format!("failed to wait for command `{}`: {}", command, e))?;
66+
67+
if output.status.success() {
68+
Ok(())
69+
} else {
70+
Err(format!(
71+
"command `{}` failed:\nStdout: {}\nStderr: {}",
72+
command.red(),
73+
String::from_utf8_lossy(&output.stdout).trim(),
74+
String::from_utf8_lossy(&output.stderr).trim()
75+
))
76+
}
77+
} // run_sudo_command_with_stdin

src/functionality/configs.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use super::commands::run_sudo_command;
2+
use colored::Colorize;
3+
use std::path::Path;
4+
5+
/// sets up config files in home directory
6+
pub fn user_config_setup(config_path: String, home_dir: String, cfg_name: &str) -> i8 {
7+
let source = Path::new(&config_path);
8+
let filename = source.file_name();
9+
10+
match filename {
11+
Some(name) => {
12+
let destination_path = Path::new(&home_dir).join(name);
13+
match std::fs::copy(config_path, &destination_path) {
14+
Ok(_) => {
15+
println!("{} {}", cfg_name, "custom config was installed".green());
16+
return 0;
17+
}
18+
Err(e) => {
19+
eprintln!(
20+
"error: custom config failed to install {} to '{}': {}",
21+
cfg_name,
22+
destination_path.display(),
23+
e
24+
);
25+
return 1;
26+
}
27+
}
28+
}
29+
None => {
30+
eprintln!(
31+
"error: could not determine filename from path: {}",
32+
config_path.red()
33+
);
34+
return 1;
35+
}
36+
}
37+
} // user_config_setup
38+
39+
/// sets up root config in /root directory
40+
pub fn setup_root_config(home_dir: String) -> i8 {
41+
let oh_my_zsh_src = format!("{}{}", home_dir, "/.oh-my-zsh");
42+
let oh_my_zsh_dest = String::from("/root/.oh-my-zsh");
43+
let zshrc_src = format!("{}{}", home_dir, "/.zshrc");
44+
let zshrc_dest = String::from("/root/.zshrc");
45+
let vimrc_src = format!("{}{}", home_dir, "/.vimrc");
46+
let vimrc_dest = String::from("/root/.vimrc");
47+
48+
// Create symbolic link for .oh-my-zsh
49+
match run_sudo_command(
50+
"cp",
51+
&["-r", oh_my_zsh_src.as_str(), oh_my_zsh_dest.as_str()],
52+
) {
53+
Ok(_) => println!("/root/.oh-my-zsh {}", "created configuration".green()),
54+
Err(e) => {
55+
eprintln!(
56+
"{}{}",
57+
"error creating configuration /root/.oh-my-zsh:".red(),
58+
e.red()
59+
);
60+
return 1;
61+
}
62+
}
63+
64+
// Create symbolic link for .zshrc
65+
match run_sudo_command("cp", &["-r", zshrc_src.as_str(), zshrc_dest.as_str()]) {
66+
Ok(_) => println!("/root/.zshrc {}", "created configuration".green()),
67+
Err(e) => {
68+
eprintln!(
69+
"{}{}",
70+
"error creating configuration /root/.zshrc:".red(),
71+
e.red()
72+
);
73+
return 2;
74+
}
75+
}
76+
77+
// Create symbolic link for .vimrc
78+
match run_sudo_command("cp", &["-r", vimrc_src.as_str(), vimrc_dest.as_str()]) {
79+
Ok(_) => println!("/root/.vimrc {}", "created configuration".green()),
80+
Err(e) => {
81+
eprintln!(
82+
"{}{}",
83+
"error creating configuration /root/.vimrc:".red(),
84+
e.red()
85+
);
86+
return 3;
87+
}
88+
}
89+
90+
return 0;
91+
} // setup_root_config

src/functionality/env.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use colored::Colorize;
2+
use std::env::var;
3+
4+
/// gets environment variables
5+
fn get_env_var(env_var: &str) -> Option<String> {
6+
match var(env_var) {
7+
Ok(username) => Some(username),
8+
Err(_) => {
9+
eprintln!(
10+
"{}\n",
11+
"error: could not determine the environment variables.".red()
12+
);
13+
None
14+
}
15+
}
16+
} // get_env_var
17+
18+
/// validates environment variables
19+
pub fn validate_env_var(env_var: &str) -> String {
20+
match get_env_var(env_var) {
21+
Some(env_var) => env_var,
22+
None => "".to_string(),
23+
}
24+
} // validate_env_var

src/functionality/iptables.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use super::commands::{run_sudo_command, run_sudo_command_with_stdin};
2+
use colored::Colorize;
3+
use std::{fs::read_to_string, path::Path};
4+
5+
/// sets up iptables
6+
pub fn iptables_file_setup() -> i8 {
7+
let source_path = Path::new("../configs/iptables.rules");
8+
let dest_path = Path::new("/etc/iptables/iptables.rules");
9+
10+
match read_to_string(source_path) {
11+
Ok(rules) => {
12+
let command = "tee";
13+
let args = &[dest_path.as_os_str().to_str().unwrap()];
14+
15+
match run_sudo_command_with_stdin(command, args, rules) {
16+
Ok(_) => {
17+
println!("iptables.rules {}", "created successfully".green());
18+
return 0;
19+
}
20+
Err(e) => {
21+
eprintln!("error setting up iptables file: {}", e);
22+
1
23+
}
24+
}
25+
}
26+
Err(e) => {
27+
eprintln!("failed to read iptables rules from source file: {}", e);
28+
2
29+
}
30+
}
31+
} // iptables_setup()
32+
33+
/// immediately sets up iptables rules
34+
pub fn iptables_rules_setup() -> i8 {
35+
let rules_path = String::from("/etc/iptables/iptables.rules");
36+
let command = "bash";
37+
let args = &["-c", &format!("iptables-restore < {}", rules_path)];
38+
39+
match run_sudo_command(command, args) {
40+
Ok(_) => {
41+
println!("iptables.rules {}", "set successfully".green());
42+
0
43+
}
44+
Err(e) => {
45+
eprintln!("error applying iptables rules: {}", e);
46+
1
47+
}
48+
}
49+
} // iptables_rules_setup()

src/functionality/prog_fun.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub fn default_sw_package() -> Vec<String> {
1111
"gimp".to_string(),
1212
"mpv".to_string(),
1313
"spectacle".to_string(),
14+
"curl".to_string(),
1415
]
1516
} // default_package()
1617

0 commit comments

Comments
 (0)