From a5dfd2627853bd67e9d17ef870fc84be312b1397 Mon Sep 17 00:00:00 2001 From: jvech Date: Tue, 23 Apr 2024 07:52:28 -0500 Subject: add: first commit done --- src/cli.rs | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/cli.rs (limited to 'src/cli.rs') 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, + + #[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, + + /// Register the transaction as a payment + #[arg(short, long)] + payment: bool + }, + + View { + filter: Option, + /// 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; +} -- cgit v1.2.3-70-g09d2