use std::path::PathBuf;
use abscissa_core::{status_err, Application, Command, Runnable, Shutdown};
use anyhow::Result;
use clap::Parser;
use rhai::{Engine, EvalAltResult};
use crate::application::ORGANIZE_APP;
use crate::scripting::add;
#[derive(Command, Debug, Parser)]
pub struct RunScriptCmd {
#[arg(long)]
path: PathBuf,
}
impl Runnable for RunScriptCmd {
fn run(&self) {
match start_scripting_engine(&self.path) {
Ok(_) => {}
Err(err) => {
status_err!("failed to execute script: {}", err);
ORGANIZE_APP.shutdown(Shutdown::Crash)
}
};
}
}
fn start_scripting_engine(path: impl Into<PathBuf>) -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine.register_fn("add", add);
engine.run_file(path.into())?;
Ok(())
}