Skip to content

Commit

Permalink
feat: add new List subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
replydev committed May 7, 2024
1 parent c877894 commit 35673f2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
40 changes: 36 additions & 4 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
};

#[derive(Parser)]
#[command(author, version = env!("COTP_VERSION"), about, long_about = None)]
#[command(author, version = env ! ("COTP_VERSION"), about, long_about = None)]
pub struct CotpArgs {
#[command(subcommand)]
command: Option<CotpSubcommands>,
Expand All @@ -24,6 +24,8 @@ enum CotpSubcommands {
Add(AddArgs),
/// Edit an existing OTP Code
Edit(EditArgs),
/// List codes
List(ListArgs),
/// Import codes from other apps
Import(ImportArgs),
/// Export cotp database
Expand Down Expand Up @@ -122,6 +124,13 @@ pub struct EditArgs {
pub change_secret: bool,
}

#[derive(Args)]
pub struct ListArgs {
/// List output format
#[command(flatten)]
pub format: Option<ExportFormat>,
}

#[derive(Args)]
pub struct ImportArgs {
#[command(flatten)]
Expand All @@ -132,18 +141,40 @@ pub struct ImportArgs {
pub path: PathBuf,
}

/// Defines the output formats of the list subcommand
#[derive(Args)]
#[group(required = false, multiple = false)]
pub struct ListFormat {
/// List OTP codes in plain format
#[arg(short, long)]
pub plain: bool,

/// List OTP codes in JSON format
#[arg(short = 'e', long)]
pub json: bool,
}

impl Default for ListFormat {
fn default() -> Self {
Self {
plain: true,
json: false,
}
}
}

#[derive(Args)]
pub struct ExtractArgs {
/// Code Index
#[arg(short, long, required_unless_present_any=["issuer", "label"])]
#[arg(short, long, required_unless_present_any = ["issuer", "label"])]
pub index: Option<usize>,

/// Code issuer
#[arg(short = 's', long, required_unless_present_any=["index","label"])]
#[arg(short = 's', long, required_unless_present_any = ["index", "label"])]
pub issuer: Option<String>,

/// Code label
#[arg(short, long, required_unless_present_any=["index", "issuer"])]
#[arg(short, long, required_unless_present_any = ["index", "issuer"])]
pub label: Option<String>,

/// Copy the code to the clipboard
Expand Down Expand Up @@ -245,6 +276,7 @@ pub fn args_parser(matches: CotpArgs, read_result: OTPDatabase) -> color_eyre::R
match matches.command {
Some(CotpSubcommands::Add(args)) => argument_functions::add(args, read_result),
Some(CotpSubcommands::Edit(args)) => argument_functions::edit(args, read_result),
Some(CotpSubcommands::List(args)) => argument_functions::list(args, read_result),
Some(CotpSubcommands::Import(args)) => argument_functions::import(args, read_result),
Some(CotpSubcommands::Export(args)) => argument_functions::export(args, read_result),
Some(CotpSubcommands::Extract(args)) => argument_functions::extract(args, read_result),
Expand Down
6 changes: 5 additions & 1 deletion src/argument_functions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::args::{AddArgs, EditArgs, ExportArgs, ExtractArgs, ImportArgs};
use crate::args::{AddArgs, EditArgs, ExportArgs, ExtractArgs, ImportArgs, ListArgs};
use crate::exporters::do_export;
use crate::exporters::otp_uri::OtpUriList;
use crate::importers::aegis::AegisJson;
Expand Down Expand Up @@ -133,6 +133,10 @@ pub fn edit(matches: EditArgs, mut database: OTPDatabase) -> color_eyre::Result<
}
}

pub fn list(matches: ListArgs, mut database: OTPDatabase) -> color_eyre::Result<OTPDatabase> {
todo!()
}

pub fn export(matches: ExportArgs, database: OTPDatabase) -> color_eyre::Result<OTPDatabase> {
let export_format = matches.format.unwrap_or_default();
let exported_path = if matches.path.is_dir() {
Expand Down

0 comments on commit 35673f2

Please sign in to comment.