Skip to content

Commit

Permalink
Fixes #57. add -o cli param to write stdout to a file.
Browse files Browse the repository at this point in the history
  • Loading branch information
René Perschon committed Apr 7, 2023
1 parent 1225856 commit 084cca5
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,10 @@ Can be overwritten by env var FHTTP_PROFILE_FILE.
| --timeout-ms
| Set a timeout in ms per request.

| -o
| --out
| Path to write stdout output to.

Will create set file or overwrite contents of existing file.

|===
26 changes: 24 additions & 2 deletions fhttp/src/bin/fhttp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::collections::HashMap;
use std::fs::OpenOptions;
use std::io::Write;
use std::{env, mem};
use std::path::PathBuf;
use std::str::FromStr;
Expand All @@ -19,15 +21,17 @@ fn main() -> Result<()> {
let files = mem::take(&mut args.files);
let profile = mem::take(&mut args.profile);
let profile_file = mem::take(&mut args.profile_file);
let out = get_target_writer(&args.out)?;
let config = args.into();

do_it(files, profile, profile_file, config)
do_it(files, profile, profile_file, out, config)
}

fn do_it(
files: Vec<String>,
profile: Option<String>,
profile_file: Option<String>,
mut out: Box<dyn Write>,
config: Config,
) -> Result<()> {
let profile = parse_profile(profile, profile_file)?;
Expand Down Expand Up @@ -83,7 +87,8 @@ fn do_it(
preprocessor.notify_response(&path, resp.body());

if !dependency {
println!("{}", resp.body());
writeln!(&mut out, "{}", resp.body())?;
// println!("{}", resp.body());
}
}
}
Expand Down Expand Up @@ -193,3 +198,20 @@ fn parse_profile(
default.override_with(profile);
Ok(default)
}

fn get_target_writer(out: &Option<String>) -> Result<Box<dyn Write>> {
match out {
None => Ok(Box::new(std::io::stdout())),
Some(path) => {
Ok(
Box::new(
OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(path)?
)
)
},
}
}
3 changes: 3 additions & 0 deletions fhttp/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pub struct Args {

#[arg(short, long, help = "print curl commands instead of executing given requests. Dependencies are still executed")]
pub curl: bool,

#[arg(short, long, help = "redirect output to the specified file")]
pub out: Option<String>,
}

impl Into<Config> for Args {
Expand Down
61 changes: 61 additions & 0 deletions fhttp/tests/output_to_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use anyhow::Result;
use assert_cmd::Command;
use fhttp_test_utils::write_test_file;
use mockito::mock;
use rstest::rstest;
use temp_dir::TempDir;

#[rstest]
async fn should_output_to_new_file() -> Result<()> {
let workdir = TempDir::new()?;

let req = write_test_file(&workdir, "req.http", "GET ${env(URL)}/foo")?;
let out = workdir.path().join("output.txt");

let request = mock("GET", "/foo")
.with_body("OK")
.create();

Command::cargo_bin("fhttp")
.unwrap()
.env("URL", &mockito::server_url())
.arg("-o")
.arg(out.to_str().unwrap())
.arg(req.to_str())
.assert()
.success();

let result = std::fs::read_to_string(out)?;
assert_eq!(&result, "OK\n");
request.assert();

Ok(())
}

#[rstest]
async fn should_output_to_and_overwrite_existing_file() -> Result<()> {
let workdir = TempDir::new()?;

let req = write_test_file(&workdir, "req.http", "GET ${env(URL)}/foo")?;
let out = workdir.path().join("output.txt");
std::fs::write(&out, "original content")?;

let request = mock("GET", "/foo")
.with_body("OK")
.create();

Command::cargo_bin("fhttp")
.unwrap()
.env("URL", &mockito::server_url())
.arg("-o")
.arg(out.to_str().unwrap())
.arg(req.to_str())
.assert()
.success();

let result = std::fs::read_to_string(&out)?;
assert_eq!(&result, "OK\n");
request.assert();

Ok(())
}

0 comments on commit 084cca5

Please sign in to comment.