Files
reqlite/crates/cli/src/main.rs
T

287 lines
10 KiB
Rust

//! `reqlite` command line: run requests from `.http` files and import from
//! curl or Postman.
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::time::{Duration, SystemTime};
use anyhow::{bail, Context, Result};
use clap::{Parser, Subcommand};
use reqlite_core::cookies::CookieJar;
use reqlite_core::env::{find_env_dir, Environments};
use reqlite_core::folder::{inherited_auth_on_disk, with_inherited_auth, InheritedAuth};
use reqlite_core::http::{HttpClient, RequestOptions};
use reqlite_core::httpfile::{render_request, HttpFile};
use reqlite_core::resolve::resolve_request;
use reqlite_core::CancelToken;
use reqlite_http_curl::CurlClient;
#[derive(Parser)]
#[command(name = "reqlite", version, about = "Run requests from .http files")]
struct Cli {
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
/// Send the requests in a .http file (all of them, or the one named).
Run {
file: PathBuf,
/// Only run the request with this `@name` or `### title`.
#[arg(long, short)]
name: Option<String>,
/// Environment from http-client.env.json.
#[arg(long, short)]
env: Option<String>,
/// Skip TLS certificate verification.
#[arg(long, short = 'k')]
insecure: bool,
/// Default timeout in seconds (a request's `@timeout` wins).
#[arg(long)]
timeout: Option<u64>,
/// Print only the response body.
#[arg(long, short)]
body_only: bool,
/// Pretty-print JSON bodies.
#[arg(long, short)]
pretty: bool,
/// Exit with an error when a response has status >= 400.
#[arg(long, short)]
fail: bool,
},
/// Print the requests of a .http file as curl commands.
Curl {
file: PathBuf,
#[arg(long, short)]
name: Option<String>,
#[arg(long, short)]
env: Option<String>,
},
/// Import from other tools.
#[command(subcommand)]
Import(Import),
}
#[derive(Subcommand)]
enum Import {
/// Convert a curl command to a .http request (printed to stdout).
Curl {
/// The command; read from stdin when omitted.
command: Option<String>,
},
/// Convert a Postman v2.1 collection into a folder of .http files.
Postman {
collection: PathBuf,
out_dir: PathBuf,
/// Postman environment exports to turn into environments.
#[arg(long = "env")]
envs: Vec<PathBuf>,
},
}
fn main() -> ExitCode {
let cli = Cli::parse();
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.expect("tokio runtime");
match rt.block_on(run(cli)) {
Ok(code) => code,
Err(e) => {
eprintln!("error: {e:#}");
ExitCode::FAILURE
}
}
}
struct Loaded {
file: HttpFile,
base_dir: PathBuf,
envs: Environments,
/// Folder auth for requests without an Authorization header.
auth: Option<InheritedAuth>,
indices: Vec<usize>,
}
fn load(file: &Path, name: Option<&str>, env: Option<&str>) -> Result<Loaded> {
let text = std::fs::read_to_string(file).with_context(|| format!("reading {}", file.display()))?;
let parsed = HttpFile::parse(text);
let base_dir = file
.parent()
.map(Path::to_path_buf)
.unwrap_or_else(|| PathBuf::from("."));
let base_dir = if base_dir.as_os_str().is_empty() {
PathBuf::from(".")
} else {
base_dir
};
let abs_dir = std::fs::canonicalize(&base_dir)?;
let auth = inherited_auth_on_disk(&abs_dir)?;
let envs = match find_env_dir(&abs_dir) {
Some(d) => Environments::load(&d)?,
None => Environments::default(),
};
if let Some(e) = env {
if !envs.names().iter().any(|n| n == e) {
bail!("unknown environment `{e}` (have: {})", envs.names().join(", "));
}
}
let indices: Vec<usize> = match name {
Some(n) => vec![parsed
.find_by_name(n)
.with_context(|| format!("no request named `{n}` in {}", file.display()))?],
None => (0..parsed.request_count()).collect(),
};
if indices.is_empty() {
bail!("{} has no requests", file.display());
}
Ok(Loaded {
file: parsed,
base_dir,
envs,
auth,
indices,
})
}
async fn run(cli: Cli) -> Result<ExitCode> {
match cli.command {
Command::Run {
file,
name,
env,
insecure,
timeout,
body_only,
pretty,
fail,
} => {
let l = load(&file, name.as_deref(), env.as_deref())?;
let resolver = l.envs.resolver(env.as_deref(), &l.file.variables());
let defaults = RequestOptions {
verify_tls: !insecure,
timeout: timeout.map(Duration::from_secs),
..RequestOptions::default()
};
let client = CurlClient::new();
let mut jar = CookieJar::new();
let mut code = ExitCode::SUCCESS;
let multiple = l.indices.len() > 1;
let mut out = std::io::stdout().lock();
for i in l.indices {
let req = with_inherited_auth(l.file.request(i).unwrap(), l.auth.as_ref());
let label = req
.display_name()
.map(str::to_string)
.unwrap_or_else(|| format!("#{}", i + 1));
let mut resolved = resolve_request(&req, &resolver, &l.base_dir, &defaults)
.with_context(|| format!("request {label}"))?;
if !req.directives.no_cookie_jar {
if let Some(c) = jar.header_for(&resolved.url, SystemTime::now()) {
if !resolved.headers.iter().any(|(n, _)| n.eq_ignore_ascii_case("cookie")) {
resolved.headers.push(("Cookie".into(), c));
}
}
}
if multiple {
eprintln!("### {label}: {} {}", resolved.method, resolved.url);
}
let url = resolved.url.clone();
let resp = match client.send(resolved, CancelToken::new()).await {
Ok(r) => r,
Err(e) => {
eprintln!("{label}: {e}");
code = ExitCode::FAILURE;
continue;
}
};
if !req.directives.no_cookie_jar {
jar.store(&url, resp.headers_named("set-cookie"), SystemTime::now());
}
if !body_only {
writeln!(out, "{} {} {}", resp.http_version, resp.status, resp.reason)?;
for (n, v) in &resp.headers {
writeln!(out, "{n}: {v}")?;
}
writeln!(out)?;
}
let body = resp.body.read_all()?;
let printed = if pretty {
serde_json::from_slice::<serde_json::Value>(&body)
.ok()
.and_then(|v| serde_json::to_vec_pretty(&v).ok())
} else {
None
};
out.write_all(printed.as_deref().unwrap_or(&body))?;
if !body.ends_with(b"\n") || printed.is_some() {
writeln!(out)?;
}
eprintln!(
"{label}: {} in {} ms, {} bytes",
resp.status,
resp.timings.total.as_millis(),
resp.size
);
if fail && resp.status >= 400 {
code = ExitCode::FAILURE;
}
}
Ok(code)
}
Command::Curl { file, name, env } => {
let l = load(&file, name.as_deref(), env.as_deref())?;
let resolver = l.envs.resolver(env.as_deref(), &l.file.variables());
for i in l.indices {
let r = resolve_request(
&with_inherited_auth(l.file.request(i).unwrap(), l.auth.as_ref()),
&resolver,
&l.base_dir,
&RequestOptions::default(),
)?;
println!("{}", reqlite_core::export::curl::to_curl(&r));
}
Ok(ExitCode::SUCCESS)
}
Command::Import(Import::Curl { command }) => {
let cmd = match command {
Some(c) => c,
None => std::io::read_to_string(std::io::stdin())?,
};
let req = reqlite_core::import::curl::from_curl(&cmd)?;
print!("{}", render_request(&req, "\n"));
Ok(ExitCode::SUCCESS)
}
Command::Import(Import::Postman {
collection,
out_dir,
envs,
}) => {
let json = std::fs::read_to_string(&collection)?;
let env_texts: Vec<String> = envs.iter().map(std::fs::read_to_string).collect::<Result<_, _>>()?;
let env_refs: Vec<&str> = env_texts.iter().map(String::as_str).collect();
let imp = reqlite_core::import::postman::import(&json, &env_refs)?;
for (rel, text) in &imp.files {
let p = out_dir.join(rel);
std::fs::create_dir_all(p.parent().unwrap())?;
std::fs::write(&p, text)?;
println!("wrote {}", p.display());
}
if let Some(e) = &imp.public_env {
std::fs::write(out_dir.join(reqlite_core::env::PUBLIC_FILE), e)?;
println!("wrote {}", reqlite_core::env::PUBLIC_FILE);
}
if let Some(e) = &imp.private_env {
std::fs::write(out_dir.join(reqlite_core::env::PRIVATE_FILE), e)?;
println!("wrote {} (keep it out of git)", reqlite_core::env::PRIVATE_FILE);
}
for w in &imp.warnings {
eprintln!("warning: {w}");
}
Ok(ExitCode::SUCCESS)
}
}
}