Remove Logger instance from main.rs (use only BaseLogger).

This commit is contained in:
jcamiel 2024-03-04 18:03:14 +01:00
parent 0f457bd03b
commit f5d65a06d9
No known key found for this signature in database
GPG Key ID: 07FF11CFD55356CC
5 changed files with 49 additions and 43 deletions

View File

@ -489,15 +489,17 @@ impl Options {
.build()
}
pub fn to_logger_options(&self, filename: &str) -> LoggerOptions {
pub fn to_logger_options(&self, filename: &str, current: usize, total: usize) -> LoggerOptions {
let verbosity = Verbosity::from(self.verbose, self.very_verbose);
LoggerOptionsBuilder::new()
.color(self.color)
.error_format(self.error_format.into())
.filename(filename)
.verbosity(verbosity)
.progress_bar(self.progress_bar)
.test(self.test)
.verbosity(verbosity)
.current(current)
.total(total)
.build()
}
}

View File

@ -25,7 +25,7 @@ use std::{env, process};
use colored::control;
use hurl::report::{html, junit, tap};
use hurl::runner::HurlResult;
use hurl::util::logger::{BaseLogger, Logger, LoggerOptionsBuilder, Verbosity};
use hurl::util::logger::BaseLogger;
use hurl::{output, runner};
use crate::cli::options::OptionsError;
@ -88,28 +88,15 @@ fn main() {
}
let content = cli::read_to_string(filename.as_str());
let content = unwrap_or_exit(content, EXIT_ERROR_PARSING, &base_logger);
let verbosity = Verbosity::from(opts.verbose, opts.very_verbose);
let logger_options = LoggerOptionsBuilder::new()
.color(opts.color)
.error_format(opts.error_format.into())
.filename(filename)
.progress_bar(opts.progress_bar)
.test(opts.test)
.verbosity(verbosity)
.build();
let logger = Logger::from(&logger_options);
let total = opts.input_files.len();
logger.test_running(current + 1, total);
// Run our Hurl file now
let hurl_result = execute(&content, filename, current_dir, &opts);
let hurl_result = execute(&content, filename, current, total, current_dir, &opts);
let hurl_result = match hurl_result {
Ok(h) => h,
Err(_) => process::exit(EXIT_ERROR_PARSING),
};
logger.test_completed(&hurl_result);
let success = hurl_result.success;
// We can output the result, either the raw body or a structured JSON representation.
@ -117,16 +104,14 @@ fn main() {
&& !opts.interactive
&& matches!(opts.output_type, cli::OutputType::ResponseBody);
if output_body {
let include_headers = opts.include;
let result = output::write_body(
&hurl_result,
filename,
include_headers,
opts.color,
&opts.output,
&logger,
);
unwrap_or_exit(result, EXIT_ERROR_RUNTIME, &base_logger);
if hurl_result.entries.last().is_some() {
let include_headers = opts.include;
let result =
output::write_body(&hurl_result, include_headers, opts.color, &opts.output);
unwrap_or_exit(result, EXIT_ERROR_RUNTIME, &base_logger);
} else {
base_logger.warning(&format!("No entry have been executed for file {filename}"));
}
}
if matches!(opts.output_type, cli::OutputType::Json) {
let result = output::write_json(&hurl_result, &content, filename, &opts.output);
@ -178,12 +163,14 @@ fn main() {
fn execute(
content: &str,
filename: &str,
current: usize,
total: usize,
current_dir: &Path,
cli_options: &cli::options::Options,
) -> Result<HurlResult, String> {
let variables = &cli_options.variables;
let runner_options = cli_options.to_runner_options(filename, current_dir);
let logger_options = cli_options.to_logger_options(filename);
let logger_options = cli_options.to_logger_options(filename, current, total);
runner::run(content, &runner_options, variables, &logger_options)
}

View File

@ -20,19 +20,16 @@ use hurl_core::ast::{Pos, SourceInfo};
use crate::output::Error;
use crate::runner;
use crate::runner::{HurlResult, Output};
use crate::util::logger::Logger;
/// Writes the `hurl_result` last body response to the file `filename_out`.
///
/// If `filename` is `None`, stdout is used. If `include_headers` is true, the last HTTP
/// If `filename_out` is `None`, stdout is used. If `include_headers` is true, the last HTTP
/// response headers are written before the body response.
pub fn write_body(
hurl_result: &HurlResult,
filename_in: &str,
include_headers: bool,
color: bool,
filename_out: &Option<Output>,
logger: &Logger,
) -> Result<(), Error> {
// By default, we output the body response bytes of the last entry
if let Some(entry_result) = hurl_result.entries.last() {
@ -66,16 +63,9 @@ pub fn write_body(
}
match filename_out {
Some(Output::File(file)) => Output::File(file.to_string()).write(&output, None)?,
_ => runner::Output::StdOut.write(&output, None)?,
_ => Output::StdOut.write(&output, None)?,
}
}
} else {
let source = if filename_in == "-" {
String::new()
} else {
format!("for file {filename_in}")
};
logger.warning(format!("No entry have been executed {source}").as_str());
}
Ok(())
}

View File

@ -80,6 +80,8 @@ pub fn run(
) -> Result<HurlResult, String> {
let mut logger = Logger::from(logger_options);
logger.test_running(logger_options.current + 1, logger_options.total);
// Try to parse the content
let hurl_file = parser::parse_hurl_file(content);
let hurl_file = match hurl_file {
@ -101,6 +103,8 @@ pub fn run(
&mut logger,
);
logger.test_completed(&result);
Ok(result)
}

View File

@ -121,18 +121,25 @@ pub struct LoggerOptions {
pub(crate) color: bool,
pub(crate) error_format: ErrorFormat,
pub(crate) filename: String,
pub(crate) verbosity: Option<Verbosity>,
// For --test reporting, will be cleaned later
pub(crate) progress_bar: bool,
pub(crate) test: bool,
pub(crate) verbosity: Option<Verbosity>,
pub(crate) current: usize, // index of the running file in the total list files
pub(crate) total: usize, // number of total files
}
pub struct LoggerOptionsBuilder {
color: bool,
error_format: ErrorFormat,
filename: String,
verbosity: Option<Verbosity>,
progress_bar: bool,
test: bool,
verbosity: Option<Verbosity>,
current: usize,
total: usize,
}
impl LoggerOptionsBuilder {
@ -179,15 +186,29 @@ impl LoggerOptionsBuilder {
self
}
/// Set the index of the running file in the total list files
pub fn current(&mut self, current: usize) -> &mut Self {
self.current = current;
self
}
/// Set the index of the running file in the total list files
pub fn total(&mut self, total: usize) -> &mut Self {
self.total = total;
self
}
/// Creates a new logger.
pub fn build(&self) -> LoggerOptions {
LoggerOptions {
color: self.color,
error_format: self.error_format,
filename: self.filename.clone(),
verbosity: self.verbosity,
progress_bar: self.progress_bar,
test: self.test,
verbosity: self.verbosity,
current: self.current,
total: self.total,
}
}
}
@ -201,6 +222,8 @@ impl Default for LoggerOptionsBuilder {
progress_bar: false,
test: false,
verbosity: None,
current: 0,
total: 0,
}
}
}