mirror of
https://github.com/Orange-OpenSource/hurl.git
synced 2024-11-22 15:42:20 +03:00
Clean ouput module interfaces.
This commit is contained in:
parent
5b8a6fb95c
commit
af82555636
@ -16,7 +16,7 @@
|
||||
*
|
||||
*/
|
||||
|
||||
use crate::report;
|
||||
use crate::{output, report};
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
|
||||
@ -67,6 +67,12 @@ impl From<report::Error> for CliError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<output::Error> for CliError {
|
||||
fn from(e: output::Error) -> Self {
|
||||
Self { message: e.message }
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for CliError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.message)
|
||||
|
@ -15,9 +15,8 @@
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
use crate::cli::CliError;
|
||||
use crate::output::write_output;
|
||||
use crate::output;
|
||||
use crate::output::Error;
|
||||
use crate::runner::HurlResult;
|
||||
|
||||
/// Writes the `hurl_result` JSON representation to the file `filename`.
|
||||
@ -29,9 +28,9 @@ pub fn write_json(
|
||||
hurl_result: &HurlResult,
|
||||
content: &str,
|
||||
filename: &Option<String>,
|
||||
) -> Result<(), CliError> {
|
||||
) -> Result<(), Error> {
|
||||
let json_result = hurl_result.to_json(content);
|
||||
let serialized = serde_json::to_string(&json_result).unwrap();
|
||||
let s = format!("{serialized}\n");
|
||||
write_output(&s.into_bytes(), filename)
|
||||
output::write_output(&s.into_bytes(), filename)
|
||||
}
|
||||
|
@ -15,27 +15,42 @@
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
//! Serialize a Hurl run result to a file.
|
||||
//!
|
||||
//! Therea are two supported serialisation:
|
||||
//! - JSON: the whole run is serialized to JSON (like the [HAR](https://en.wikipedia.org/wiki/HAR_(file_format)) format)
|
||||
//! - raw: the last response of a run is serialized to a file. The body can be automatically uncompress
|
||||
//! or written as it.
|
||||
mod json;
|
||||
mod raw;
|
||||
mod stdout;
|
||||
|
||||
pub use self::json::write_json;
|
||||
pub use self::raw::write_body;
|
||||
use crate::cli::CliError;
|
||||
use crate::output::stdout::write_stdout;
|
||||
use std::fmt;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Error {
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.message)
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes `bytes` to the file `filename` or stdout by default.
|
||||
fn write_output(bytes: &Vec<u8>, filename: &Option<String>) -> Result<(), CliError> {
|
||||
fn write_output(bytes: &Vec<u8>, filename: &Option<String>) -> Result<(), Error> {
|
||||
match filename {
|
||||
None => write_stdout(bytes.as_slice()),
|
||||
None => stdout::write_stdout(bytes.as_slice()),
|
||||
Some(filename) => {
|
||||
let path = Path::new(filename.as_str());
|
||||
let mut file = match std::fs::File::create(path) {
|
||||
Err(why) => {
|
||||
return Err(CliError {
|
||||
return Err(Error {
|
||||
message: format!("Issue writing to {}: {:?}", path.display(), why),
|
||||
});
|
||||
}
|
||||
|
@ -15,9 +15,8 @@
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
use crate::cli::CliError;
|
||||
use crate::http::Response;
|
||||
use crate::output::write_output;
|
||||
use crate::output;
|
||||
use crate::runner;
|
||||
use crate::runner::{HurlResult, RunnerError};
|
||||
use crate::util::logger::Logger;
|
||||
@ -35,7 +34,7 @@ pub fn write_body(
|
||||
color: bool,
|
||||
filename: &Option<String>,
|
||||
logger: &Logger,
|
||||
) -> Result<(), CliError> {
|
||||
) -> Result<(), output::Error> {
|
||||
// By default, we output the body response bytes of the last entry
|
||||
if let Some(entry_result) = hurl_result.entries.last() {
|
||||
if let Some(call) = entry_result.calls.last() {
|
||||
@ -61,16 +60,16 @@ pub fn write_body(
|
||||
assert: false,
|
||||
};
|
||||
let message = error.fixme();
|
||||
return Err(CliError { message });
|
||||
return Err(output::Error { message });
|
||||
}
|
||||
}
|
||||
} else {
|
||||
response.body.clone()
|
||||
};
|
||||
output.append(&mut body);
|
||||
let result = write_output(&output, filename);
|
||||
let result = output::write_output(&output, filename);
|
||||
if result.is_err() {
|
||||
return Err(CliError {
|
||||
return Err(output::Error {
|
||||
message: "Undefined error".to_string(),
|
||||
});
|
||||
}
|
||||
|
@ -15,30 +15,30 @@
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
use crate::cli::CliError;
|
||||
use crate::output::Error;
|
||||
#[cfg(target_family = "windows")]
|
||||
use atty::Stream;
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
|
||||
#[cfg(target_family = "unix")]
|
||||
pub(crate) fn write_stdout(buf: &[u8]) -> Result<(), CliError> {
|
||||
pub(crate) fn write_stdout(buf: &[u8]) -> Result<(), Error> {
|
||||
let stdout = io::stdout();
|
||||
let mut handle = stdout.lock();
|
||||
handle.write_all(buf).map_err(|_| CliError {
|
||||
handle.write_all(buf).map_err(|_| Error {
|
||||
message: "Error writing output".to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(target_family = "windows")]
|
||||
pub(crate) fn write_stdout(buf: &[u8]) -> Result<(), CliError> {
|
||||
pub(crate) fn write_stdout(buf: &[u8]) -> Result<(), Error> {
|
||||
if atty::is(Stream::Stdout) {
|
||||
println!("{}", String::from_utf8_lossy(buf));
|
||||
Ok(())
|
||||
} else {
|
||||
let stdout = io::stdout();
|
||||
let mut handle = stdout.lock();
|
||||
handle.write_all(buf).map_err(|_| CliError {
|
||||
handle.write_all(buf).map_err(|_| Error {
|
||||
message: "Error writing output".to_string(),
|
||||
})
|
||||
}
|
||||
|
@ -16,7 +16,10 @@
|
||||
*
|
||||
*/
|
||||
|
||||
//! Various reports for Hurl runs (JUnit, HTML etc...)
|
||||
//! Various reports for Hurl runs (JUnit, HTML etc...) A report aggregates multiple runs into
|
||||
//! a single unit.
|
||||
|
||||
use std::fmt;
|
||||
|
||||
pub mod html;
|
||||
pub mod junit;
|
||||
@ -25,3 +28,9 @@ pub mod junit;
|
||||
pub struct Error {
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.message)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user