Files
sausage/userland/undo/src/main.rs
T

46 lines
1.6 KiB
Rust

//! The undo CLI selects journal entries through the reserved host HTTP API.
use clap::Parser;
#[derive(Parser)]
struct Args {
/// A tag, or the list/show command.
#[arg(required_unless_present = "since")]
action: Option<String>,
#[arg(required_if_eq("action", "show"))]
tag: Option<String>,
#[arg(long)]
conversation: Option<String>,
#[arg(long, conflicts_with = "action")]
since: Option<i64>,
#[arg(long)]
force: bool,
}
fn main() {
let args = Args::parse();
let mut url = url::Url::parse("http://host.sausage/undo").unwrap();
let mut transfer = vec!["undo-http".to_owned()];
match args.action.as_deref() {
Some("list") => {
if let Some(conversation) = args.conversation {
url.query_pairs_mut()
.append_pair("conversation", &conversation);
}
}
Some("show") => {
let tag = args.tag.expect("required show tag");
url.set_path("/undo/show");
url.query_pairs_mut().append_pair("tag", &tag);
}
tag => {
let body = serde_json::json!({"tag":tag,"since":args.since,"force":args.force});
transfer.extend(["--json".into(), body.to_string()]);
}
}
transfer.extend(["--fail-with-body".into(), url.to_string()]);
let mut request = curl::Args::parse_from(transfer);
request.parts = request.json.iter().map(|s| (s.clone(), 3)).collect();
if let Err(error) = curl::run(&request) {
eprintln!("undo: {}", error.message);
std::process::exit(error.code);
}
}