use arboard::Clipboard;
use flowerypassgen::{gen_password, genoptions::GenOptions};
use std::env;
fn help() {
println!(
"fpgcli - Flowery Pass Gen Command Line Interface
Arguments:
--help, -H, -? - Prints the help argument
--copy, -C - Copies the generated password into your clipboard
--length=<u32>, -L=<u32> - Sets the password length to the given unsigned 32 bit integer value - (default: 8)
--max-attempts=<u32>, -M=<u32> - Sets the maximum attempts to the given unsigned 32 bit integer value - (default: 512)
--symbols=<bool>, -S=<bool> - Determines whether the password can have symbols in it - (default: false)
--duplicates=<bool>, -D=<bool> - Determines whether the password can have duplicate instances of chars in it - (default: true)
");
}
fn main() {
let mut opts: GenOptions = GenOptions { length: (8), max_attempts: (512), use_symbols: (false), use_duplicate_chars: (true) };
let mut copy_to_clipboard: bool = false;
let mut args: Vec<String> = env::args().collect();
args.remove(0);
for arg in args {
if ["--help", "-H", "-?"].contains(&arg.as_str()) {
help();
std::process::exit(0);
} else if ["--copy", "-C"].contains(&arg.as_str()) {
copy_to_clipboard = true;
} else if arg.contains("=") {
let arg_split: Vec<&str> = arg.as_str().split("=").collect();
match arg_split[0] {
"--length" | "-L" => {
match arg_split[1].parse::<u32>() {
Ok(n) => opts.length = n,
Err(_) => eprintln!("Provided length '{}' is not of type u32", arg_split[1]),
}
},
"--max-attempts" | "-M" => {
match arg_split[1].parse::<u32>() {
Ok(n) => opts.max_attempts = n,
Err(_) => eprintln!("Provided maximum attempts '{}' is not of type u32", arg_split[1]),
}
},
"--symbols" | "-S" => {
opts.use_symbols = arg_split[1].to_lowercase() == "true" || arg_split[1] == "1";
},
"--duplicates" | "-D" => {
opts.use_duplicate_chars = arg_split[1].to_lowercase() == "true" || arg_split[1] == "1";
},
_ => {
eprintln!("Unrecognised argument {}.", arg_split[0]);
}
}
} else {
eprintln!("Unrecognised argument {}.", arg.as_str());
}
}
let password = gen_password(&opts);
if copy_to_clipboard {
let mut ctx = Clipboard::new().unwrap();
match ctx.set_text(password.clone()) {
Ok(_) => { println!("Password written to clipboard!"); },
Err(e) => {
eprintln!("Failed to write password to clipboard due to error '{}', please copy manually. Password: {}", e, password)
}
}
}
else {
println!("Password: {}", password);
}
}