use std::fs::OpenOptions;
use std::io::Write;
use std::path::Path;
use crate::report::ReportError;
use crate::runner::HurlResult;
use crate::util::path::create_dir_all;
use crate::util::redacted::Redact;
pub fn write_curl(
hurl_results: &[&HurlResult],
filename: &Path,
secrets: &[&str],
) -> Result<(), ReportError> {
create_dir_all(filename)
.map_err(|e| ReportError::from_io_error(&e, filename, "Issue creating curl export"))?;
let mut file = OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.append(false)
.open(filename)
.map_err(|e| ReportError::from_io_error(&e, filename, "Issue creating curl export"))?;
let mut cmds = hurl_results
.iter()
.flat_map(|h| &h.entries)
.map(|e| e.curl_cmd.to_string().redact(secrets))
.collect::<Vec<_>>()
.join("\n");
cmds.push('\n');
file.write_all(cmds.as_bytes())
.map_err(|e| ReportError::from_io_error(&e, filename, "Issue writing curl export"))?;
Ok(())
}