mod agents;
mod aws;
mod aws_budget;
mod aws_plane;
mod cloudflare;
mod commands;
mod dashboard;
mod image;
mod live;
mod logos;
mod modal;
mod plane;
mod store;
mod view;
pub type Result<T> = std::result::Result<T, String>;
fn main() {
let raw: Vec<String> = std::env::args().skip(1).collect();
let json = raw.iter().any(|a| a == "--json");
let fail = |e: commands::Fail, topic: &str| -> ! {
let (said, half, unconfirmed) = (e.said().to_string(), matches!(e, commands::Fail::Usage(_)), matches!(e, commands::Fail::Unconfirmed(_)));
let needs = commands::needs(&said);
if json { println!("{}", serde_json::json!({ "ok": false, "error": said, "needs": needs })) }
else if half { eprintln!("superci: {said}\n\n{}", commands::help(topic)) }
else { eprintln!("superci: {said}") }
std::process::exit(if needs.is_some() { 3 } else if half || unconfirmed { 2 } else { 1 })
};
let args = match commands::Args::parse(&raw) { Ok(a) => a, Err(e) => fail(e, "") };
if args.has("version") { println!("superci {}", env!("CARGO_PKG_VERSION")); return }
if args.words.is_empty() { println!("{}", commands::help("")); return }
if args.word(0) == "help" { println!("{}", commands::help(args.word(1))); return }
if args.has("help") { println!("{}", commands::help(args.word(0))); return }
if let Some(done) = commands::in_browser(&args) {
if let Err(e) = done { fail(e, args.word(0)) }
return
}
let mut steps = vec![];
let result = commands::run(&args, &mut |step: &str| { if !json { println!("{step}…") } steps.push(step.to_string()) });
match result {
Ok(done) if json => {
let mut data = done.data;
if !steps.is_empty() { if let Some(o) = data.as_object_mut() { o.insert("steps".into(), steps.into()); } }
println!("{}", serde_json::to_string_pretty(&data).unwrap_or_default())
}
Ok(done) => for line in done.said { println!("{line}") },
Err(e) => fail(e, args.word(0)),
}
}