diff options
author | jvech <jmvalenciae@unal.edu.co> | 2024-04-23 07:52:28 -0500 |
---|---|---|
committer | jvech <jmvalenciae@unal.edu.co> | 2024-04-23 07:52:28 -0500 |
commit | a5dfd2627853bd67e9d17ef870fc84be312b1397 (patch) | |
tree | 85e005be5e96916f2d45d85a703ff2717ed28c51 /src/cli.rs |
add: first commit done
Diffstat (limited to 'src/cli.rs')
-rw-r--r-- | src/cli.rs | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..0bd0a78 --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,81 @@ +use std::path::PathBuf; +use clap::{Subcommand, Parser, ValueHint}; +use std::env; + +#[derive(Debug, Parser)] +#[command(version, about, long_about = None)] +pub struct Cli { + /// file path where data is stored [default: + /// $XDG_DATA_HOME/debt/debt.db + #[arg(short, long, env="DEBT_DB", value_hint = ValueHint::FilePath)] + pub database_path: Option<PathBuf>, + + #[command(subcommand)] + pub action: Commands +} + +#[derive(Debug, Subcommand)] +pub enum Commands { + /// Initialize Database + Init, + /// Register a transaction + Register { + /// person to register the transaction + person: String, + + /// amount of money + amount: i32, + + /// additional notes + note: Option<String>, + + /// Register the transaction as a payment + #[arg(short, long)] + payment: bool + }, + + View { + filter: Option<String>, + /// View register history + #[arg(short='H', long)] + history: bool, + /// Transaction accumulation by person + #[arg(short, long)] + total: bool + }, + + /// Add a new agent to lend or pay + Add { + name: String, + }, +} + +impl Cli { + pub fn new() -> Self { + let mut cli = Cli::parse(); + load_datapath(&mut cli); + cli + } +} + +fn load_datapath(cli: &mut Cli) { + let config_path = cli + .database_path + .clone() + .or_else(|| { + xdg::BaseDirectories::with_prefix("debt") + .ok() + .and_then(|x| { + x.find_data_file("cache.db") + }) + }).or_else(|| { + if let Ok(home) = env::var("HOME") { + let fallback = PathBuf::from(&home).join(".debt.db"); + if fallback.exists() { + return Some(fallback) + } + } + None + }); + cli.database_path = config_path; +} |