Skip to content

Commit b4b7f33

Browse files
committed
Setup Wizard
Added a Setup Wizard to help complete beginners to use this software
1 parent 849f576 commit b4b7f33

File tree

3 files changed

+92
-3
lines changed

3 files changed

+92
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/target
22
/config
3+
/config1

src/file_writer.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::io::prelude::*;
22
use std::fs::OpenOptions;
3+
use std::path::Path;
4+
use std::fs::File;
35

46
pub fn write_to_file(text: String, file_path: &str) -> Result<(), String> {
57

@@ -11,3 +13,23 @@ pub fn write_to_file(text: String, file_path: &str) -> Result<(), String> {
1113

1214
write!(file, "{} ", text).map_err(|e| e.to_string())
1315
}
16+
17+
pub fn update_credentials(username: String, password: String) {
18+
let file_path = "config/smtp_account.txt";
19+
let path = Path::new(file_path);
20+
21+
let mut content = String::new();
22+
{
23+
let mut file = File::open(path).expect("An error occurs when tried to open a file");
24+
file.read_to_string(&mut content).expect("An error occurs when tried to read the file");
25+
}
26+
27+
let updated_content = content
28+
.replace("username:[email protected]", &format!("username:{}", username))
29+
.replace("password:password123123", &format!("password:{}", password));
30+
31+
{
32+
let mut file = OpenOptions::new().write(true).truncate(true).open(&path).expect("An error occurs when tried to open a file with OpenOptions");
33+
file.write_all(updated_content.as_bytes()).expect("An error occurs when tried to update content");
34+
}
35+
}

src/setup.rs

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
use std::path::Path;
22
use std::fs::OpenOptions;
3-
use std::io::prelude::*;
3+
use std::io::*;
4+
use std::io;
45

56
use crate::file_reader::smtp_account_reader;
7+
use crate::file_writer::update_credentials;
68

7-
pub fn check_config_components() -> Result<(), String> {
9+
fn read_line() -> String {
10+
let mut input = String::new();
11+
io::stdout().flush().unwrap();
12+
io::stdin().read_line(&mut input).expect("Failed to read line");
13+
input.trim().to_string()
14+
}
15+
16+
pub fn check_config_components() -> std::result::Result<(), String> {
817
let components = vec![
918
Path::new("config/logs.txt"),
1019
Path::new("config/smtp_account.txt"),
@@ -24,11 +33,68 @@ pub fn check_config_components() -> Result<(), String> {
2433
match smtp_account_reader() {
2534
Err(_) => {
2635
let _ = write!(file, "username:[email protected]\npassword:password123123\n\n### The username is your gmail account ([email protected])\n### The password is a 'password for apps' created in Google\n## NOTICE: To create an app-password, you have to have 2-step verification\n## enabled and maybe all rescue options of saving the account.").map_err(|e| e.to_string());
27-
panic!("\n\nThe config has any valid information! Please check it out in config/smtp_account.txt!\n\n");
36+
setup_wizard();
2837
},
2938
_ => {}
3039
}
3140
}
3241
}
3342
Ok(())
3443
}
44+
45+
pub fn setup_wizard() {
46+
let mut iter: u8 = 1;
47+
let mut smtp_account_username = String::new();
48+
let mut smtp_account_password = String::new();
49+
50+
loop {
51+
match iter {
52+
1 => {
53+
println!("Enter your smtp account username:");
54+
},
55+
2 => {
56+
println!("Can you confirm your smtp account username (Y or N)?");
57+
},
58+
3 => {
59+
println!("Enter your smtp account password:");
60+
},
61+
4 => {
62+
println!("Can you confirm your smtp account password (Y or N)?");
63+
},
64+
_ => ()
65+
}
66+
let input = read_line();
67+
match iter {
68+
1 => {
69+
println!("You have entered \"{}\" as your smtp account username", input);
70+
smtp_account_username = input.clone();
71+
iter += 1;
72+
},
73+
2 => {
74+
if input.to_lowercase() == "y" {
75+
iter += 1;
76+
}
77+
else {
78+
iter = 1
79+
}
80+
},
81+
3 => {
82+
println!("You have entered \"{}\" as your smtp account password", input);
83+
smtp_account_password = input.clone();
84+
iter += 1;
85+
},
86+
4 => {
87+
if input.to_lowercase() == "y" {
88+
iter += 1;
89+
}
90+
else {
91+
iter = 3
92+
}
93+
}
94+
_ => {
95+
update_credentials(smtp_account_username, smtp_account_password);
96+
return;
97+
}
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)