Print output with a dedicated function (removes some duplicated code)
This commit is contained in:
parent
f2e61459fb
commit
f2521e317d
3 changed files with 33 additions and 133 deletions
2
.gitignore
vendored
Executable file
2
.gitignore
vendored
Executable file
|
@ -0,0 +1,2 @@
|
|||
/target
|
||||
Cargo.lock
|
78
Cargo.lock
generated
78
Cargo.lock
generated
|
@ -9,54 +9,10 @@ dependencies = [
|
|||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atty"
|
||||
version = "0.2.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
|
||||
dependencies = [
|
||||
"hermit-abi",
|
||||
"libc",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "colored"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd"
|
||||
dependencies = [
|
||||
"atty",
|
||||
"lazy_static",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.1.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.82"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "89203f3fba0a3795506acaad8ebce3c80c0af93f994d5a1d7a0b1eeb23271929"
|
||||
|
||||
[[package]]
|
||||
name = "maniver"
|
||||
version = "0.1.0-dev"
|
||||
dependencies = [
|
||||
"colored",
|
||||
"regex",
|
||||
]
|
||||
|
||||
|
@ -66,6 +22,12 @@ version = "2.3.4"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0"
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.4.3"
|
||||
|
@ -86,31 +48,9 @@ checksum = "b5eb417147ba9860a96cfe72a0b93bf88fee1744b5636ec99ab20c1aa9376581"
|
|||
|
||||
[[package]]
|
||||
name = "thread_local"
|
||||
version = "1.1.0"
|
||||
version = "1.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bb9bc092d0d51e76b2b19d9d85534ffc9ec2db959a2523cdae0697e2972cd447"
|
||||
checksum = "8018d24e04c95ac8790716a5987d0fec4f8b27249ffa0f7d33f1369bdfb88cbd"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
|
86
src/main.rs
86
src/main.rs
|
@ -1,8 +1,7 @@
|
|||
use std::env;
|
||||
use regex::Regex;
|
||||
use std::io::prelude::*;
|
||||
use std::process::{Command, Stdio};
|
||||
use colored::*;
|
||||
use std::process::{Command, Stdio, Output};
|
||||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
|
@ -42,43 +41,29 @@ fn export_tor(username: String, dir: String) {
|
|||
.output()
|
||||
.expect("failed to execute process");
|
||||
|
||||
println!("status: {}", output.status);
|
||||
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
|
||||
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
|
||||
assert!(output.status.success());
|
||||
|
||||
//let output = Command::new("/usr/bin/cp")
|
||||
// .arg(src_path)
|
||||
// .arg(dest_path)
|
||||
// .output()
|
||||
// .expect("failed to execute process");
|
||||
|
||||
//println!("status: {}", output.status);
|
||||
//println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
|
||||
//println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
|
||||
//assert!(output.status.success());
|
||||
|
||||
print_output(output);
|
||||
}
|
||||
|
||||
fn le_install(domain: String) {
|
||||
|
||||
let output = Command::new("/usr/bin/certbot")
|
||||
.arg("--nginx")
|
||||
// Using ECDSA
|
||||
.arg("--key-type")
|
||||
.arg("rsa")
|
||||
.arg("--rsa-key-size")
|
||||
.arg("3072")
|
||||
.arg("ecdsa")
|
||||
.arg("--elliptic-curve")
|
||||
.arg("secp384r1")
|
||||
// Using RSA
|
||||
//.arg("--key-type")
|
||||
//.arg("rsa")
|
||||
//.arg("--rsa-key-size")
|
||||
//.arg("3072")
|
||||
.arg("-d")
|
||||
.arg(&domain)
|
||||
.output()
|
||||
.expect("failed to execute process");
|
||||
|
||||
println!("status: {}", output.status);
|
||||
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
|
||||
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
|
||||
|
||||
assert!(output.status.success());
|
||||
|
||||
print_output(output);
|
||||
}
|
||||
|
||||
fn reload_nginx() {
|
||||
|
@ -89,6 +74,10 @@ fn reload_nginx() {
|
|||
.output()
|
||||
.expect("Error while reloading Nginx config");
|
||||
|
||||
print_output(output);
|
||||
}
|
||||
|
||||
fn print_output(output: Output) {
|
||||
println!("status: {}", output.status);
|
||||
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
|
||||
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
|
||||
|
@ -98,24 +87,13 @@ fn reload_nginx() {
|
|||
|
||||
fn reload_tor() {
|
||||
|
||||
Command::new("/usr/bin/systemctl")
|
||||
let output = Command::new("/usr/bin/systemctl")
|
||||
.arg("reload")
|
||||
.arg("tor@default")
|
||||
.output()
|
||||
.expect("Error while reloading Tor config");
|
||||
|
||||
//let output = Command::new("/usr/bin/chown")
|
||||
// .arg("-R")
|
||||
// .arg("www-data:www-data")
|
||||
// .arg("/var/lib/tor/niver")
|
||||
// .output()
|
||||
// .expect("failed to execute process");
|
||||
|
||||
//println!("status: {}", output.status);
|
||||
//println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
|
||||
//println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
|
||||
//assert!(output.status.success());
|
||||
|
||||
print_output(output);
|
||||
}
|
||||
|
||||
fn setup_user(username: String, password: String) {
|
||||
|
@ -142,7 +120,6 @@ fn setup_user(username: String, password: String) {
|
|||
} else {
|
||||
println!("Erreur : l'username doit faire moins de 32 caractères");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Set disk usage limit to the user by copying another user quota
|
||||
|
@ -155,12 +132,7 @@ fn quota(username: String) {
|
|||
.output()
|
||||
.expect("failed to execute process");
|
||||
|
||||
println!("status: {}", output.status);
|
||||
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
|
||||
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
|
||||
|
||||
assert!(output.status.success());
|
||||
|
||||
print_output(output);
|
||||
}
|
||||
|
||||
// Chown /srv/hyper/username to root:root
|
||||
|
@ -178,10 +150,7 @@ fn chroot(username: String) {
|
|||
.output()
|
||||
.expect("Failed to chown /srv/hyper/<username> to root:root");
|
||||
|
||||
println!("status: {}", output.status);
|
||||
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
|
||||
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
|
||||
assert!(output.status.success());
|
||||
print_output(output);
|
||||
|
||||
let output = Command::new("/usr/bin/chmod")
|
||||
.arg("755")
|
||||
|
@ -189,15 +158,11 @@ fn chroot(username: String) {
|
|||
.output()
|
||||
.expect("Failed to chmod /srv/hyper/<username> to 755");
|
||||
|
||||
println!("status: {}", output.status);
|
||||
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
|
||||
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
|
||||
assert!(output.status.success());
|
||||
print_output(output);
|
||||
|
||||
} else {
|
||||
println!("Erreur : l'username doit faire moins de 32 caractères");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Creates a new user in the group 'hyper', which is available only over SFTP
|
||||
|
@ -215,12 +180,7 @@ fn newser(username: String) {
|
|||
.output()
|
||||
.expect("failed to execute process");
|
||||
|
||||
println!("status: {}", output.status);
|
||||
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
|
||||
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
|
||||
|
||||
assert!(output.status.success());
|
||||
|
||||
print_output(output);
|
||||
}
|
||||
|
||||
// Changes password of the newly created user
|
||||
|
@ -249,7 +209,6 @@ fn pwd(username: String, password: String) {
|
|||
Err(why) => panic!("couldn't read chpasswd stdout: {}", why),
|
||||
Ok(_) => print!("chpasswd responded with:\n{}", s),
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn is_string_lowercase(stri: String) -> bool {
|
||||
|
@ -261,5 +220,4 @@ fn is_string_lowercase(stri: String) -> bool {
|
|||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Reference in a new issue