Initial impl
This commit is contained in:
commit
4b729c5d32
133 files changed
+20475
No files matched your search
@@ -0,0 +1,24 @@
|
||||
[package]
|
||||
name = "reqlite-cli"
|
||||
description = "Run .http files from the command line."
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
rust-version.workspace = true
|
||||
publish.workspace = true
|
||||
|
||||
[[bin]]
|
||||
name = "reqlite"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
reqlite-core.workspace = true
|
||||
reqlite-http-curl.workspace = true
|
||||
anyhow.workspace = true
|
||||
clap.workspace = true
|
||||
serde_json.workspace = true
|
||||
tokio.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile.workspace = true
|
||||
wiremock.workspace = true
|
||||
@@ -0,0 +1,280 @@
|
||||
//! `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::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,
|
||||
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 envs = match find_env_dir(&std::fs::canonicalize(&base_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,
|
||||
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 = l.file.request(i).unwrap();
|
||||
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(
|
||||
l.file.request(i).unwrap(),
|
||||
&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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
use std::process::Command;
|
||||
|
||||
use wiremock::matchers::{body_string, header, method, path};
|
||||
use wiremock::{Mock, MockServer, ResponseTemplate};
|
||||
|
||||
fn reqlite() -> Command {
|
||||
Command::new(env!("CARGO_BIN_EXE_reqlite"))
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn run_uses_env_files_cookies_and_names() {
|
||||
let server = MockServer::start().await;
|
||||
Mock::given(method("POST"))
|
||||
.and(path("/login"))
|
||||
.and(body_string("{\"user\": \"ada\"}"))
|
||||
.respond_with(
|
||||
ResponseTemplate::new(200)
|
||||
.insert_header("set-cookie", "session=s1; Path=/")
|
||||
.set_body_string("{\"ok\":true}"),
|
||||
)
|
||||
.mount(&server)
|
||||
.await;
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/me"))
|
||||
.and(header("cookie", "session=s1"))
|
||||
.and(header("authorization", "Bearer secret-token"))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_string("me"))
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
std::fs::write(
|
||||
dir.path().join("http-client.env.json"),
|
||||
format!(
|
||||
r#"{{ "dev": {{ "base_url": "{}" }}, "prod": {{ "base_url": "http://unused" }} }}"#,
|
||||
server.uri()
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::write(
|
||||
dir.path().join("http-client.private.env.json"),
|
||||
r#"{ "dev": { "token": "secret-token" } }"#,
|
||||
)
|
||||
.unwrap();
|
||||
let file = dir.path().join("api.http");
|
||||
std::fs::write(
|
||||
&file,
|
||||
"### Login\nPOST {{base_url}}/login\nContent-Type: application/json\n\n{\"user\": \"ada\"}\n\n### Me\nGET {{base_url}}/me\nAuthorization: Bearer {{token}}\n",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let out = reqlite()
|
||||
.args(["run", file.to_str().unwrap(), "--env", "dev", "--fail"])
|
||||
.output()
|
||||
.unwrap();
|
||||
let stdout = String::from_utf8_lossy(&out.stdout);
|
||||
assert!(
|
||||
out.status.success(),
|
||||
"{stdout}\n{}",
|
||||
String::from_utf8_lossy(&out.stderr)
|
||||
);
|
||||
assert!(stdout.contains("HTTP/1.1 200 OK"), "{stdout}");
|
||||
assert!(stdout.contains("{\"ok\":true}"));
|
||||
assert!(stdout.trim_end().ends_with("me"), "{stdout}");
|
||||
|
||||
let out = reqlite()
|
||||
.args([
|
||||
"run",
|
||||
file.to_str().unwrap(),
|
||||
"--env",
|
||||
"dev",
|
||||
"--name",
|
||||
"Login",
|
||||
"--body-only",
|
||||
"--pretty",
|
||||
])
|
||||
.output()
|
||||
.unwrap();
|
||||
assert_eq!(String::from_utf8_lossy(&out.stdout), "{\n \"ok\": true\n}\n");
|
||||
|
||||
// Missing variables are reported, not sent.
|
||||
let out = reqlite().args(["run", file.to_str().unwrap()]).output().unwrap();
|
||||
assert!(!out.status.success());
|
||||
assert!(String::from_utf8_lossy(&out.stderr).contains("unknown variable `base_url`"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_curl_prints_http() {
|
||||
let out = reqlite()
|
||||
.args(["import", "curl", "curl -X PUT https://x/y -H 'A: b' -d 'z=1'"])
|
||||
.output()
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
String::from_utf8_lossy(&out.stdout),
|
||||
"PUT https://x/y\nA: b\nContent-Type: application/x-www-form-urlencoded\n\nz=1\n"
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user