use clap::Parser;
use link2aws::arn_to_link;
#[derive(Parser, Debug)]
#[command(author, version, verbatim_doc_comment)]
struct Cli {
#[arg()]
arns: Vec<String>,
#[arg(long)]
stdin: bool,
#[arg(short, long)]
quiet: bool,
}
fn main() {
let cli = Cli::parse();
let all_ok: bool = if cli.stdin {
handle_all(std::io::stdin().lines().map_while(Result::ok), cli.quiet)
} else {
handle_all(cli.arns.iter(), cli.quiet)
};
std::process::exit(if all_ok { 0 } else { 1 });
}
fn handle_all<I>(lines: I, quiet: bool) -> bool
where
I: IntoIterator,
I::Item: AsRef<str>,
{
let mut all_ok = true;
for line in lines {
match arn_to_link(line.as_ref()) {
Ok(link) => println!("{}", link),
Err(err) => {
all_ok = false;
if !quiet {
eprintln!("link2aws: {:?}: {}", line.as_ref(), err);
}
}
}
}
all_ok
}