use anyhow::{Context, Result};
use clap::Parser;
use std::process::Command;
#[derive(Parser, Debug)]
pub struct ExploreArgs {
#[arg(default_value = "http://localhost:3000/admin/status")]
pub url: String,
#[arg(long, default_value_t = 500)]
pub refresh_ms: u64,
}
#[derive(Parser, Debug)]
pub struct LogsArgs {
#[arg(long, default_value = "docker")]
pub source: String,
#[arg(long)]
pub service: Option<String>,
}
#[derive(Parser, Debug)]
pub struct HealthArgs {
#[arg(short, long, default_value_t = 5)]
pub interval: u64,
}
#[derive(Parser, Debug)]
pub struct DeployArgs {
#[arg(long, default_value = "dev")]
pub env: String,
#[arg(long)]
pub k8s: bool,
}
#[derive(Parser, Debug)]
pub struct CleanArgs {
#[arg(long, default_value_t = false)]
pub dry_run: bool,
}
#[derive(Parser, Debug)]
pub struct AsmArgs {
#[arg(long, conflicts_with = "dir")]
pub file: Option<String>,
#[arg(long, conflicts_with = "file")]
pub dir: Option<String>,
#[arg(long, default_value_t = false)]
pub recursive: bool,
#[arg(long)]
pub ext: Option<String>,
#[arg(long)]
pub config: Option<String>,
#[arg(long, default_value_t = false)]
pub no_cache: bool,
#[arg(long, default_value_t = false)]
pub rebuild_cache: bool,
#[arg(long, default_value_t = false)]
pub no_disasm: bool,
#[arg(long)]
pub max_functions: Option<usize>,
#[arg(long, default_value_t = false)]
pub tui: bool,
#[arg(long, default_value_t = false)]
pub plain: bool,
#[arg(long, default_value_t = false)]
pub json: bool,
}
pub async fn run_explore(args: ExploreArgs) -> Result<()> {
run_tool(
"resq-perf",
&[&args.url, "--refresh-ms", &args.refresh_ms.to_string()],
)
}
pub async fn run_logs(args: LogsArgs) -> Result<()> {
let mut cmd_args = vec!["--source", &args.source];
if let Some(ref s) = args.service {
cmd_args.push("--service");
cmd_args.push(s);
}
run_tool("resq-logs", &cmd_args)
}
pub async fn run_health(args: HealthArgs) -> Result<()> {
run_tool("resq-health", &["--interval", &args.interval.to_string()])
}
pub async fn run_deploy(args: DeployArgs) -> Result<()> {
let mut cmd_args = vec!["--env", &args.env];
if args.k8s {
cmd_args.push("--k8s");
}
run_tool("resq-deploy", &cmd_args)
}
pub async fn run_clean(args: CleanArgs) -> Result<()> {
let mut cmd_args = Vec::new();
if args.dry_run {
cmd_args.push("--dry-run");
}
run_tool("resq-clean", &cmd_args)
}
pub async fn run_asm(args: AsmArgs) -> Result<()> {
let mut cmd_args = Vec::new();
if let Some(ref file) = args.file {
cmd_args.push("--file");
cmd_args.push(file);
}
if let Some(ref dir) = args.dir {
cmd_args.push("--dir");
cmd_args.push(dir);
}
if args.recursive {
cmd_args.push("--recursive");
}
if let Some(ref ext) = args.ext {
cmd_args.push("--ext");
cmd_args.push(ext);
}
if let Some(ref config) = args.config {
cmd_args.push("--config");
cmd_args.push(config);
}
if args.no_cache {
cmd_args.push("--no-cache");
}
if args.rebuild_cache {
cmd_args.push("--rebuild-cache");
}
if args.no_disasm {
cmd_args.push("--no-disasm");
}
let max_functions = args.max_functions.map(|v| v.to_string());
if let Some(ref max_functions) = max_functions {
cmd_args.push("--max-functions");
cmd_args.push(max_functions);
}
if args.tui {
cmd_args.push("--tui");
}
if args.plain {
cmd_args.push("--plain");
}
if args.json {
cmd_args.push("--json");
}
run_tool("resq-bin", &cmd_args)
}
fn run_tool(name: &str, args: &[&str]) -> Result<()> {
let mut child = Command::new("cargo")
.arg("run")
.arg("-q")
.arg("-p")
.arg(name)
.arg("--")
.args(args)
.spawn()
.with_context(|| format!("Failed to launch tool: {name}"))?;
let status = child.wait().context("Tool crashed or was interrupted")?;
if !status.success() {
}
Ok(())
}