use anyhow::Result;
use clap::Parser;
use commands::handle_commands;
use gflow::config::load_config;
use std::ffi::OsString;
use std::io::{self, IsTerminal};
mod cli;
mod commands;
pub async fn run(argv: Vec<OsString>) -> Result<()> {
let args = cli::GBatch::parse_from(argv);
let config = load_config(args.config.as_ref())?;
if let Some(commands) = args.commands {
handle_commands(&config, commands).await
} else {
let stdin_available = !io::stdin().is_terminal();
let explicit_stdin =
args.add_args.script_or_command.len() == 1 && args.add_args.script_or_command[0] == "-";
if args.add_args.script_or_command.is_empty()
&& !stdin_available
&& args.add_args.param.is_empty()
{
anyhow::bail!("The following required arguments were not provided:\n <SCRIPT_OR_COMMAND>...\n\nUsage: gbatch <SCRIPT_OR_COMMAND>...\n gbatch < script.sh\n gbatch -\n\nFor more information, try 'gbatch --help'");
}
let use_stdin =
explicit_stdin || (stdin_available && args.add_args.script_or_command.is_empty());
commands::add::handle_add(&config, args.add_args, use_stdin).await
}
}