Merge pull request #1 from TheAwiteb/edit-command

Edit command
This commit is contained in:
Mohammed Alotaibi 2023-12-24 13:33:59 +03:00 committed by GitHub
commit c62cfa611c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 102 additions and 5 deletions

View file

@ -34,6 +34,7 @@ Commands:
add Add new password add Add new password
list List your password and search list List your password and search
clean Clean the password file clean Clean the password file
edit Edit the password content
help Print this message or the help of the given subcommand(s) help Print this message or the help of the given subcommand(s)
Options: Options:
@ -57,4 +58,4 @@ Contributions to Passrs are welcome! If you would like to contribute, please fol
## License ## License
Passrs is licensed under the GPL-3.0 License. This means that you are free to use, modify, and distribute the software under the terms of this license. Please refer to the [LICENSE](LICENSE) file for more details. Passrs is licensed under the GPL-3.0 License. This means that you are free to use, modify, and distribute the software under the terms of this license. Please refer to the [LICENSE](LICENSE) file for more details.

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

@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License // 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>. // along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
use std::num::NonZeroU64;
use clap::Args; use clap::Args;
use comfy_table::Table; use comfy_table::Table;
use regex::Regex; use regex::Regex;
@ -35,7 +37,7 @@ pub struct List {
/// Return the password with spesifc index /// Return the password with spesifc index
#[arg(short, long, value_name = "INDEX")] #[arg(short, long, value_name = "INDEX")]
get: Option<usize>, get: Option<NonZeroU64>,
/// Search and display only matching passwords. /// Search and display only matching passwords.
/// ///
/// The name and username will be searched. And service and note if included /// The name and username will be searched. And service and note if included
@ -79,7 +81,7 @@ impl RunCommand for List {
.enumerate() .enumerate()
.filter(|(idx, pass)| { .filter(|(idx, pass)| {
if let Some(index) = self.get { if let Some(index) = self.get {
return (idx + 1) == index; return (idx + 1) == index.get() as usize;
} }
if let Some(ref pattern) = self.search { if let Some(ref pattern) = self.search {
if self.regex { if self.regex {

View file

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

View file

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