Lines
84 %
Functions
40.91 %
Branches
100 %
//! `script` subcommands: list, create, edit, enable, disable, delete and run of scripts.
use clap::Subcommand;
use cli_core::CommandError;
use cli_core::script as ops;
use rpc::Session;
use sqlx::types::Uuid;
use std::io::{BufRead, IsTerminal};
use std::path::PathBuf;
use crate::eval::{RenderMode, build_delete_script_form, eval_print};
use crate::template::read_path;
use crate::transaction::confirmed;
#[derive(Subcommand, Debug)]
pub enum ScriptCmd {
/// List the scripts: id, name, state and whether the source is stored
List,
/// Create a script from a source
Create {
#[arg(long)]
name: Option<String>,
/// File holding the source; `-` reads standard input
#[arg(long, value_name = "PATH")]
file: PathBuf,
},
/// Edit a script; omitted fields stay as stored
Edit {
/// Id of the script
id: Uuid,
/// File holding the new source; `-` reads standard input
file: Option<PathBuf>,
/// Let the script run on triggers again
Enable {
/// Stop the script from running on triggers
Disable {
/// Delete a script
Delete {
/// Delete without asking; required when stdin is not a terminal
yes: bool,
/// Run a script over the transactions of an account
Run {
/// Account id or name
account: String,
}
/// Deletes the script unless the answer declines; returns whether it was deleted.
pub async fn delete_script_confirmed(
session: &mut Session,
interactive: bool,
input: impl BufRead,
) -> Result<bool, CommandError> {
let question = format!("Delete script {id}?");
if !confirmed(&question, yes, interactive, input)? {
return Ok(false);
let form = build_delete_script_form(&id.to_string());
eval_print(session, &form, RenderMode::BoolSuccess).await?;
Ok(true)
/// Prints the run report; a run with failures is an error.
fn report(run: &ops::RunReport) -> Result<(), CommandError> {
run.lines().iter().for_each(|line| println!("{line}"));
match run.failures.len() {
0 => Ok(()),
count => Err(CommandError::Command(format!(
"the script failed on {count} transaction(s)"
))),
pub async fn run(session: &mut Session, cmd: ScriptCmd) -> Result<(), CommandError> {
match cmd {
ScriptCmd::List => ops::list(session)
.await?
.iter()
.for_each(|script| println!("{}", script.line())),
ScriptCmd::Create { name, file } => {
let source = read_path(&file, std::io::stdin().lock())?;
println!("{}", ops::create(session, name.as_deref(), &source).await?);
ScriptCmd::Edit { id, name, file } => {
let source = file
.map(|file| read_path(&file, std::io::stdin().lock()))
.transpose()?;
let id = id.to_string();
let edited = ops::edit(session, &id, name.as_deref(), source.as_deref(), None);
println!("{}", edited.await?);
ScriptCmd::Enable { id } => toggle(session, id, true).await?,
ScriptCmd::Disable { id } => toggle(session, id, false).await?,
ScriptCmd::Delete { id, yes } => {
let stdin = std::io::stdin();
if !delete_script_confirmed(session, id, yes, stdin.is_terminal(), stdin.lock()).await?
{
eprintln!("Cancelled");
ScriptCmd::Run { id, account } => {
report(&ops::run(session, &id.to_string(), &account).await?)?;
Ok(())
async fn toggle(session: &mut Session, id: Uuid, enabled: bool) -> Result<(), CommandError> {
let edited = ops::edit(session, &id.to_string(), None, None, Some(enabled)).await?;
println!("{edited}");
#[cfg(test)]
mod tests;