✨ Adds creating logins
Signed-off-by: Ash Svitan <selfsigned-ash@proton.me>
This commit is contained in:
+40
-20
@@ -8,6 +8,18 @@ use std::io::{Error, ErrorKind, Write};
|
||||
|
||||
const ENV_VAR_DEFAULT_VAULT: &str = "PASS_VAULT";
|
||||
|
||||
fn ask_consent(question: String, line1: String, line2: String) -> Result<bool, Error> {
|
||||
println!("{}", question);
|
||||
println!("\t{}", line1);
|
||||
println!("\t{}", line2);
|
||||
print!("Proceed? [y/N] ");
|
||||
io::stdout().flush()?;
|
||||
let mut input = String::new();
|
||||
io::stdin().read_line(&mut input)?;
|
||||
|
||||
return Ok(input.to_lowercase() == "y\n");
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
pass::check_pass()?;
|
||||
rbw::check_rbw()?;
|
||||
@@ -48,10 +60,11 @@ fn main() -> Result<(), Error> {
|
||||
let rbw_items = rbw::get_items()?;
|
||||
let mut rbw_logins = rbw::get_logins(rbw_items)?;
|
||||
|
||||
println!();
|
||||
println!(
|
||||
"Got {} pass logins and {} rbw logins...",
|
||||
pass_logins.len(),
|
||||
rbw_logins.len()
|
||||
"Got {} rbw logins and {} pass logins...",
|
||||
rbw_logins.len(),
|
||||
pass_logins.len()
|
||||
);
|
||||
|
||||
for rbw_login in rbw_logins.clone().iter() {
|
||||
@@ -80,16 +93,12 @@ fn main() -> Result<(), Error> {
|
||||
let pass_user_is_actually_email = pass_user == pass_login.email;
|
||||
|
||||
if rbw_user != pass_user || rbw_password != pass_password {
|
||||
// TODO: need to update
|
||||
println!("Attempting to update {}:", rbw_login.name);
|
||||
println!("\t{} -> {}", pass_user, rbw_user.clone());
|
||||
println!("\t{} -> {}", pass_password, rbw_password.clone());
|
||||
print!("Proceed? [y/N] ");
|
||||
io::stdout().flush()?;
|
||||
let mut input = String::new();
|
||||
io::stdin().read_line(&mut input)?;
|
||||
|
||||
if input.to_lowercase() == "y\n" {
|
||||
let consent = ask_consent(
|
||||
format!("Attempting to update {}:", rbw_login.name),
|
||||
format!("{} -> {}", pass_user, rbw_user.clone()),
|
||||
format!("{} -> {}", pass_password, rbw_password.clone()),
|
||||
)?;
|
||||
if consent {
|
||||
let updated_pass_login = pass::LoginItem {
|
||||
id: pass_login.id.clone(),
|
||||
title: pass_login.title.clone(),
|
||||
@@ -97,7 +106,7 @@ fn main() -> Result<(), Error> {
|
||||
email: rbw_user.clone(),
|
||||
password: rbw_password,
|
||||
};
|
||||
pass::update(&vault, updated_pass_login, pass_user_is_actually_email)?;
|
||||
pass::update(&vault, updated_pass_login, pass_user_is_actually_email);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,13 +123,24 @@ fn main() -> Result<(), Error> {
|
||||
pass_logins.remove(pass_index);
|
||||
}
|
||||
|
||||
// TODO: need to create these
|
||||
println!("remaining {} rbw items", rbw_logins.len());
|
||||
println!("{:?}", rbw_logins);
|
||||
|
||||
// TODO: need to delete these
|
||||
println!("remaining {} pass items", pass_logins.len());
|
||||
println!("{:?}", pass_logins);
|
||||
println!();
|
||||
println!(
|
||||
"Remaining {} rbw logins and {} pass logins...",
|
||||
rbw_logins.len(),
|
||||
pass_logins.len()
|
||||
);
|
||||
|
||||
for rbw_login in rbw_logins {
|
||||
let consent = ask_consent(
|
||||
format!("Attempting to create {}:", rbw_login.name),
|
||||
format!("{}", rbw_login.user),
|
||||
format!("{}", rbw_login.password),
|
||||
)?;
|
||||
if consent {
|
||||
pass::create(&vault, rbw_login.name, rbw_login.user, rbw_login.password);
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
+19
-3
@@ -1,8 +1,10 @@
|
||||
use crate::sh;
|
||||
use regex::Regex;
|
||||
use serde::Deserialize;
|
||||
use std::io::{Error, ErrorKind};
|
||||
|
||||
const EXECUTABLE: &str = "pass-cli";
|
||||
const EMAIL_REGEX: &str = r"^[\w\-\.]+@([\w-]+\.)+[\w-]{2,}$";
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct Vaults {
|
||||
@@ -124,7 +126,7 @@ pub fn get_logins(items: Items) -> Vec<LoginItem> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn update(vault: &String, item: LoginItem, user_is_actually_email: bool) -> Result<(), Error> {
|
||||
pub fn update(vault: &String, item: LoginItem, user_is_actually_email: bool) {
|
||||
let user_field_update = if user_is_actually_email {
|
||||
format!("email={}", item.email)
|
||||
} else {
|
||||
@@ -135,6 +137,20 @@ pub fn update(vault: &String, item: LoginItem, user_is_actually_email: bool) ->
|
||||
EXECUTABLE, vault, item.id, item.password, user_field_update
|
||||
));
|
||||
println!("> {}", output);
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
pub fn create(vault: &String, title: String, user: String, password: String) {
|
||||
let re = Regex::new(EMAIL_REGEX).expect("Couldn't parse regex");
|
||||
let is_email = re.is_match(&user);
|
||||
let user_arg = if is_email {
|
||||
format!("--email '{}'", user)
|
||||
} else {
|
||||
format!("--username '{}'", user)
|
||||
};
|
||||
|
||||
let output = sh::sh(format!(
|
||||
"{} item create login --vault-name '{}' --title '{}' {} --password '{}'",
|
||||
EXECUTABLE, vault, title, user_arg, password
|
||||
));
|
||||
println!("> {}", output);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user