Add generate command
This commit is contained in:
parent
4b15b9403a
commit
4e333d9665
2 changed files with 69 additions and 1 deletions
66
src/cli/gen_command.rs
Normal file
66
src/cli/gen_command.rs
Normal file
|
@ -0,0 +1,66 @@
|
|||
// 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::Passwords, PassrsError, PassrsResult, RunCommand};
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
pub struct Gen {
|
||||
/// The password length
|
||||
#[arg(default_value_t = NonZeroU64::new(18).unwrap())]
|
||||
length: NonZeroU64,
|
||||
|
||||
/// With uppercase letters (A-Z)
|
||||
#[arg(short, long)]
|
||||
uppercase: bool,
|
||||
/// With lowercase letters (a-z)
|
||||
#[arg(short, long)]
|
||||
lowercase: bool,
|
||||
/// With numbers (0-9)
|
||||
#[arg(short, long)]
|
||||
numbers: bool,
|
||||
/// With symbols (!,# ...)
|
||||
#[arg(short, long)]
|
||||
symbols: bool,
|
||||
}
|
||||
|
||||
impl RunCommand for Gen {
|
||||
fn run(&self, _password_manager: Passwords) -> PassrsResult<()> {
|
||||
if self.uppercase || self.lowercase || self.numbers || self.symbols {
|
||||
println!(
|
||||
"{}",
|
||||
passwords::PasswordGenerator::new()
|
||||
.length(self.length.get() as usize)
|
||||
.uppercase_letters(self.uppercase)
|
||||
.lowercase_letters(self.lowercase)
|
||||
.numbers(self.numbers)
|
||||
.symbols(self.symbols)
|
||||
.strict(true)
|
||||
.generate_one()
|
||||
.expect("The length cannot be zero")
|
||||
);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(PassrsError::Other(
|
||||
"You need to enable at least one kind of characters".to_owned(),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
|
@ -26,6 +26,7 @@ use crate::{
|
|||
pub mod add_command;
|
||||
pub mod clean_command;
|
||||
pub mod edit_command;
|
||||
pub mod gen_command;
|
||||
pub mod list_command;
|
||||
|
||||
crate::create_commands!(
|
||||
|
@ -34,6 +35,7 @@ crate::create_commands!(
|
|||
"List your password and search", List => list_command::List
|
||||
"Clean the password file", Clean => clean_command::Clean
|
||||
"Edit the password content", Edit => edit_command::Edit
|
||||
"Generate password", Gen => gen_command::Gen
|
||||
// TODO: Remove command
|
||||
// TODO: Export command
|
||||
// TODO: Import command
|
||||
|
@ -63,7 +65,7 @@ impl Cli {
|
|||
"Getting password file: {}",
|
||||
passwords_file.to_string_lossy()
|
||||
);
|
||||
let password_manager = if matches!(self.command, Commands::Clean(..)) {
|
||||
let password_manager = if matches!(self.command, Commands::Clean(..) | Commands::Gen(..)) {
|
||||
Passwords {
|
||||
passwords_file,
|
||||
..Default::default()
|
||||
|
|
Loading…
Reference in a new issue