Add the edit command

This commit is contained in:
TheAwiteb 2023-12-24 13:28:12 +03:00
parent fae55817bd
commit 534f3c605b
No known key found for this signature in database
GPG key ID: ABF818BD15DC2D34
3 changed files with 96 additions and 2 deletions

89
src/cli/edit_command.rs Normal file
View file

@ -0,0 +1,89 @@
// Local CLI password manager
// Copyright (C) 2024 Awiteb
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
use std::num::NonZeroU64;
use clap::Args;
use crate::{
password::{Password, Passwords},
PassrsError, PassrsResult, RunCommand,
};
#[derive(Debug, Args)]
#[command(author, version, about, long_about = None)]
pub struct Edit {
/// The password index. Check it from list command
index: NonZeroU64,
#[arg(short, long)]
/// The new password name
name: Option<String>,
#[arg(short, long)]
/// The new password username
username: Option<String>,
#[arg(short, long)]
/// The new password
password: Option<String>,
#[arg(short, long)]
/// The new password service
service: Option<String>,
#[arg(short = 'o', long)]
/// The new password note
note: Option<String>,
}
impl RunCommand for Edit {
fn run(&self, mut password_manager: Passwords) -> PassrsResult<()> {
let index = self.index.get() as usize;
if let Some(password) = password_manager.passwords.get_mut(index - 1) {
if self.name.is_none()
&& self.username.is_none()
&& self.password.is_none()
&& self.service.is_none()
&& self.note.is_none()
{
Err(PassrsError::Other(
"You must edit one option at least".to_owned(),
))
} else {
*password = Password {
name: self.name.as_ref().unwrap_or(&password.name).to_string(),
username: self
.username
.as_ref()
.unwrap_or(&password.username)
.to_string(),
password: self
.password
.as_ref()
.unwrap_or(&password.password)
.to_string(),
service: self.service.as_ref().or(password.service.as_ref()).cloned(),
note: self.note.as_ref().or(password.note.as_ref()).cloned(),
};
password_manager.try_export()
}
} else {
Err(PassrsError::InvalidPasswordIndex(format!(
"The index `{}` is greater than the passwords count {}",
self.index,
password_manager.passwords.len()
)))
}
}
}

View file

@ -25,6 +25,7 @@ use crate::{
pub mod add_command;
pub mod clean_command;
pub mod edit_command;
pub mod list_command;
crate::create_commands!(
@ -32,8 +33,8 @@ crate::create_commands!(
"Add new password", Add => add_command::Add
"List your password and search", List => list_command::List
"Clean the password file", Clean => clean_command::Clean
// TODO: Edit command
// TODO: Delete command
"Edit the password content", Edit => edit_command::Edit
// TODO: Remove command
// TODO: Export command
// TODO: Import command
);

View file

@ -34,6 +34,10 @@ pub enum Error {
WeakPassword(String),
#[error("Args Conflict Error: {0}")]
ArgsConflict(String),
#[error("Invalid Password Index Error: {0}")]
InvalidPasswordIndex(String),
#[error("{0}")]
Other(String),
#[error("Invalid Regex: {0}")]
InvalidRegex(#[from] regex::Error),