Lines
69.57 %
Functions
37.5 %
Branches
100 %
//! `account merge`, built from the `account-merge` form descriptor.
use clap::{Arg, ArgAction, ArgMatches, Args, Command, Error, FromArgMatches};
use cli_core::CommandError;
use forms::{ACCOUNT_MERGE, FormValues};
use rpc::Session;
use std::io::{BufRead, IsTerminal};
use crate::transaction::{confirmed, field_arg, values_of};
#[derive(Debug)]
pub struct MergeArgs {
pub values: FormValues,
pub yes: bool,
}
impl Args for MergeArgs {
fn augment_args(cmd: Command) -> Command {
ACCOUNT_MERGE
.fields
.iter()
.fold(cmd, |cmd, field| cmd.arg(field_arg(&ACCOUNT_MERGE, field)))
.arg(
Arg::new("yes")
.long("yes")
.action(ArgAction::SetTrue)
.help("Merge without asking; required when stdin is not a terminal"),
)
fn augment_args_for_update(cmd: Command) -> Command {
Self::augment_args(cmd)
impl FromArgMatches for MergeArgs {
fn from_arg_matches(matches: &ArgMatches) -> Result<Self, Error> {
Ok(Self {
values: values_of(&ACCOUNT_MERGE, matches),
yes: matches.get_flag("yes"),
})
fn update_from_arg_matches(&mut self, matches: &ArgMatches) -> Result<(), Error> {
*self = Self::from_arg_matches(matches)?;
Ok(())
fn question(values: &FormValues) -> String {
let pick = |key| values.text(key).unwrap_or_default();
format!(
"Merge account {} into {}? Its splits, lots, budgets, sub-accounts and tags move, \
and it is deleted.",
pick("from"),
pick("into")
async fn merge_confirmed(
session: &mut Session,
args: &MergeArgs,
interactive: bool,
input: impl BufRead,
) -> Result<Option<String>, CommandError> {
match confirmed(&question(&args.values), args.yes, interactive, input)? {
true => Ok(Some(
Box::pin(cli_core::account::merge(session, &args.values)).await?,
)),
false => Ok(None),
pub async fn run(session: &mut Session, args: &MergeArgs) -> Result<(), CommandError> {
let stdin = std::io::stdin();
match merge_confirmed(session, args, stdin.is_terminal(), stdin.lock()).await? {
Some(id) => println!("{id}"),
None => eprintln!("Cancelled"),
#[cfg(test)]
#[path = "account_merge_tests.rs"]
mod tests;