Fix API interaction and add transactions command

This commit is contained in:
2026-05-02 23:12:49 +02:00
parent 65ddf515ad
commit deda785c2a
6 changed files with 212 additions and 10 deletions
+55 -1
View File
@@ -32,6 +32,31 @@ enum Command {
#[arg(short = 'P', long, group = "planref")]
plan_id: Option<String>,
},
// List transactions in the last 30 days
//
// You have to give a plan to list transactions from. You can optionally
// also give an account to show only transactions from that account.
Transactions {
/// Your YNAB token, available from developer settings in YNAB
#[arg(short, long, env = "YNAB_API_TOKEN", hide_env_values = true)]
token: String,
/// The name of the YNAB plan to list transactions from
#[arg(short, long, group = "planref")]
plan: Option<String>,
/// Alternatively, give the YNAB plan ID directly
#[arg(short = 'P', long, group = "planref")]
plan_id: Option<String>,
/// The name of the YNAB account to import to
#[arg(short, long, group = "accountref")]
account: Option<String>,
/// Alternatively, give the YNAB account ID directly
#[arg(short = 'A', long, group = "accountref")]
account_id: Option<String>,
},
/// Convert from a bank export to a CSV you can import manually to YNAB
Convert {
#[arg(short, long, help = "Bank export format", default_value_t = String::from("bulder"))]
@@ -116,13 +141,42 @@ fn main() -> Result<(), Box<dyn Error>> {
plan_id,
} => {
let plan = Lookup::from_options(plan, plan_id)?;
let accounts = Ynab::new(&token).list_accounts(plan)?;
let accounts = Ynab::list_accounts(&Ynab::new(&token), plan)?;
println!("Available accounts in plan:");
for account in accounts {
println!(" - {}", account);
}
Ok(())
}
Command::Transactions {
token,
plan,
plan_id,
account,
account_id,
} => {
let plan = Lookup::from_options(plan, plan_id)?;
// Account is optional here
let accountstr: &str;
let account = match (account, account_id) {
(None, None) => {
accountstr = "";
None
}
(name, id) => {
accountstr = " for account";
Some(Lookup::from_options(name, id)?)
}
};
let transactions = Ynab::new(&token).list_transactions(plan, account)?;
println!("Transactions{} in the last 30 days:", accountstr);
for t in transactions {
println!("{},{},{}", t.date, t.payee, t.amount);
}
Ok(())
}
Command::Import {
token,
plan,