feat: Support --verbose flag (#23)
All checks were successful
Write changelog / write-changelog (push) Successful in 3s
Rust CI / Rust CI (push) Successful in 1m19s

Reviewed-on: #23
Co-authored-by: Awiteb <a@4rs.nl>
Co-committed-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-04-30 16:49:21 +02:00 committed by awiteb
parent 8cf18db314
commit 31a68b9277
11 changed files with 65 additions and 9 deletions

View file

@ -34,8 +34,12 @@ pub struct Add {
impl LprsCommand for Add {
fn run(mut self, mut vault_manager: Vaults<Plain>) -> LprsResult<()> {
match self.password {
Some(Some(password)) => self.vault_info.password = Some(password),
Some(Some(password)) => {
log::debug!("User provided a password");
self.vault_info.password = Some(password);
}
Some(None) => {
log::debug!("User didn't provide a password, prompting it");
self.vault_info.password = Some(
inquire::Password::new("Vault password:")
.without_confirmation()

View file

@ -29,6 +29,10 @@ pub struct Clean {}
impl LprsCommand for Clean {
fn run(self, vault_manager: Vaults<Plain>) -> LprsResult<()> {
log::info!(
"Cleaning the vaults file: {:?}",
vault_manager.vaults_file.display()
);
fs::write(vault_manager.vaults_file, "[]").map_err(LprsError::Io)
}
}

View file

@ -49,6 +49,7 @@ pub struct Edit {
impl LprsCommand for Edit {
fn run(self, mut vault_manager: Vaults<Plain>) -> LprsResult<()> {
let index = self.index.get() as usize;
log::debug!("Editing vault at index: {index}");
let Some(vault) = vault_manager.vaults.get_mut(index - 1) else {
return Err(LprsError::InvalidVaultIndex(format!(
@ -71,6 +72,7 @@ impl LprsCommand for Edit {
None => None,
};
log::info!("Applying the new values to the vault");
*vault = Vault::<Plain>::new(
self.name.as_ref().unwrap_or(&vault.name),
self.username.as_ref().or(vault.username.as_ref()),

View file

@ -35,6 +35,12 @@ pub struct Export {
impl LprsCommand for Export {
fn run(self, vault_manager: Vaults<Plain>) -> LprsResult<()> {
log::debug!(
"Exporting vault {} to: {} with format: {}",
vault_manager.vaults_file.display(),
self.path.display(),
self.format
);
let exported_data = match self.format {
Format::Lprs => {
serde_json::to_string::<Vec<Vault<Encrypted>>>(&vault_manager.encrypt_vaults()?)

View file

@ -36,6 +36,13 @@ pub struct Import {
impl LprsCommand for Import {
fn run(self, mut vault_manager: Vaults<Plain>) -> LprsResult<()> {
log::debug!(
"Importing vaults from: {} with format: {} to the vault: {}",
self.path.display(),
self.format,
vault_manager.vaults_file.display()
);
let imported_passwords_len = match self.format {
Format::Lprs => {
let vaults = Vaults::try_reload(self.path, vault_manager.master_password.to_vec())?;

View file

@ -46,6 +46,7 @@ impl LprsCommand for List {
));
}
if let Some(user_vault_index) = self.get.map(|n| (n.get() - 1) as usize) {
log::info!("Getting the vault at index: {user_vault_index}");
if user_vault_index >= vault_manager.vaults.len() {
return Err(LprsError::Other(
"The `--get` index is great then the vaults length".to_owned(),
@ -67,6 +68,7 @@ impl LprsCommand for List {
regex::escape(self.filter.as_deref().unwrap_or(""))
)
};
log::debug!("Listing vaults filtered by: {pattern}");
let re = regex::Regex::new(&pattern)?;
@ -107,6 +109,8 @@ impl LprsCommand for List {
.parse::<usize>()
.unwrap_or_default();
log::debug!("The user selected the vault at index: {vault_idx}");
println!(
"{}",
vault_manager

View file

@ -58,33 +58,39 @@ impl_commands!(Commands, Add Remove List Clean Edit Gen Export Import);
#[command(author, version, about, long_about = None)]
pub struct Cli {
/// The vaults json file
#[arg(short = 'f', long)]
pub vaults_file: Option<PathBuf>,
/// Show the logs in the stdout
#[arg(short, long)]
vaults_file: Option<PathBuf>,
pub verbose: bool,
// TODO: verbose flag
#[command(subcommand)]
command: Commands,
pub command: Commands,
}
impl Cli {
/// Run the cli
pub fn run(self) -> LprsResult<()> {
let vaults_file = if let Some(ref path) = self.vaults_file {
path.clone()
let vaults_file = if let Some(path) = self.vaults_file {
log::info!("Using the given vaults file");
path
} else {
log::info!("Using the default vaults file");
crate::utils::vaults_file()?
};
log::debug!("Getting the vaults file: {}", vaults_file.to_string_lossy());
log::debug!("Vaults file: {}", vaults_file.display());
self.command.validate_args()?;
let vault_manager = if matches!(self.command, Commands::Clean(..) | Commands::Gen(..)) {
log::info!("Running command that don't need the vault manager");
// Returns empty vault manager for those commands don't need it
Vaults {
vaults_file,
..Default::default()
}
} else {
log::info!("Reloading the vaults file");
let master_password = utils::master_password_prompt(&vaults_file)?;
Vaults::try_reload(
vaults_file,

View file

@ -37,11 +37,17 @@ pub struct Remove {
impl LprsCommand for Remove {
fn run(self, mut vault_manager: Vaults<Plain>) -> LprsResult<()> {
let index = (self.index.get() - 1) as usize;
log::debug!("Removing vault at index: {index}");
if index > vault_manager.vaults.len() {
if !self.force {
return Err(LprsError::Other(
"The index is greater than the passwords counts".to_owned(),
));
} else {
log::error!(
"The index is greater than the passwords counts, but the force flag is enabled"
);
}
} else {
vault_manager.vaults.remove(index);

View file

@ -43,10 +43,15 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const LAST_VERSION_CHECK_FILE: &str = ".last_version_check";
fn main() -> ExitCode {
let lprs_cli = cli::Cli::parse();
if lprs_cli.verbose {
std::env::set_var("RUST_LOG", "lprs");
}
pretty_env_logger::init();
#[cfg(feature = "update-notify")]
{
log::info!("Checking for new version of lprs...");
match utils::lprs_version() {
Ok(Some(new_version)) if new_version != VERSION => {
println!(
@ -60,11 +65,13 @@ fn main() -> ExitCode {
eprintln!("{err}");
return ExitCode::FAILURE;
}
_ => {}
_ => {
log::info!("No new version found.");
}
}
}
if let Err(err) = cli::Cli::parse().run() {
if let Err(err) = lprs_cli.run() {
if !matches!(
err,
LprsError::Inquire(InquireError::OperationCanceled)

View file

@ -30,7 +30,9 @@ pub fn local_project_file(filename: &str) -> LprsResult<PathBuf> {
.ok_or_else(|| {
LprsError::ProjectDir("Can't extract the project_dir from this OS".to_owned())
})?;
log::debug!("Local project dir: {:?}", local_dir.display());
if !local_dir.exists() {
log::info!("Creating the local project dir: {:?}", local_dir.display());
fs::create_dir_all(&local_dir)?;
}
Ok(local_dir.join(filename))
@ -40,6 +42,10 @@ pub fn local_project_file(filename: &str) -> LprsResult<PathBuf> {
pub fn vaults_file() -> LprsResult<PathBuf> {
let vaults_file = local_project_file(crate::DEFAULT_VAULTS_FILE)?;
if !vaults_file.exists() {
log::info!(
"Vaults file not found, creating a new one: {:?}",
vaults_file.display()
);
fs::write(&vaults_file, "[]")?;
}
Ok(vaults_file)

View file

@ -190,6 +190,10 @@ impl Vaults<Plain> {
/// Encrypt the vaults then export it to the file
pub fn try_export(self) -> LprsResult<()> {
log::debug!(
"Trying to export the vaults to the file: {}",
self.vaults_file.display()
);
fs::write(
&self.vaults_file,
serde_json::to_string(&self.encrypt_vaults()?)?,