Skip to content

Commit

Permalink
Add very-verbose option.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcamiel authored and fabricereix committed Jun 19, 2022
1 parent e354efb commit 75b34f8
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 63 deletions.
79 changes: 43 additions & 36 deletions packages/hurl/src/cli/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub struct CliOptions {
pub user_agent: Option<String>,
pub variables: HashMap<String, Value>,
pub verbose: bool,
pub very_verbose: bool,
}

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -297,10 +298,15 @@ pub fn app(version: &str) -> Command {
.long("verbose")
.help("Turn on verbose output"),
)
.arg(
clap::Arg::new("very_verbose")
.long("very-verbose")
.help("Turn on verbose output, uncluding HTTP response"),
)
}

pub fn parse_options(matches: &ArgMatches) -> Result<CliOptions, CliError> {
let cacert_file = match get_string(&matches, "cacert_file") {
let cacert_file = match get_string(matches, "cacert_file") {
None => None,
Some(filename) => {
if !Path::new(&filename).is_file() {
Expand All @@ -311,9 +317,9 @@ pub fn parse_options(matches: &ArgMatches) -> Result<CliOptions, CliError> {
}
}
};
let color = output_color(matches.clone());
let compressed = has_flag(&matches, "compressed");
let connect_timeout = match get_string(&matches, "connect_timeout") {
let color = output_color(matches);
let compressed = has_flag(matches, "compressed");
let connect_timeout = match get_string(matches, "connect_timeout") {
None => ClientOptions::default().connect_timeout,
Some(s) => match s.parse::<u64>() {
Ok(n) => Duration::from_secs(n),
Expand All @@ -324,14 +330,13 @@ pub fn parse_options(matches: &ArgMatches) -> Result<CliOptions, CliError> {
}
},
};
let cookie_input_file = get_string(&matches, "cookies_input_file");
let cookie_output_file = get_string(&matches, "cookies_output_file");

let fail_fast = !has_flag(&matches, "fail_at_end");
let file_root = get_string(&matches, "file_root");
let follow_location = has_flag(&matches, "follow_location");
let glob_files = match_glob_files(&matches)?;
let report_html = get_string(&matches, "report_html");
let cookie_input_file = get_string(matches, "cookies_input_file");
let cookie_output_file = get_string(matches, "cookies_output_file");
let fail_fast = !has_flag(matches, "fail_at_end");
let file_root = get_string(matches, "file_root");
let follow_location = has_flag(matches, "follow_location");
let glob_files = match_glob_files(matches)?;
let report_html = get_string(matches, "report_html");
let html_dir = if let Some(dir) = report_html {
let path = Path::new(&dir);
if !path.exists() {
Expand All @@ -353,12 +358,12 @@ pub fn parse_options(matches: &ArgMatches) -> Result<CliOptions, CliError> {
} else {
None
};
let ignore_asserts = has_flag(&matches, "ignore_asserts");
let include = has_flag(&matches, "include");
let insecure = has_flag(&matches, "insecure");
let interactive = has_flag(&matches, "interactive");
let junit_file = get_string(&matches, "junit");
let max_redirect = match get_string(&matches, "max_redirects").as_deref() {
let ignore_asserts = has_flag(matches, "ignore_asserts");
let include = has_flag(matches, "include");
let insecure = has_flag(matches, "insecure");
let interactive = has_flag(matches, "interactive");
let junit_file = get_string(matches, "junit");
let max_redirect = match get_string(matches, "max_redirects").as_deref() {
None => Some(50),
Some("-1") => None,
Some(s) => match s.parse::<usize>() {
Expand All @@ -370,20 +375,20 @@ pub fn parse_options(matches: &ArgMatches) -> Result<CliOptions, CliError> {
}
},
};
let no_proxy = get_string(&matches, "proxy");
let output = get_string(&matches, "output");
let test = has_flag(&matches, "test");
let output_type = if has_flag(&matches, "json") {
let no_proxy = get_string(matches, "proxy");
let output = get_string(matches, "output");
let test = has_flag(matches, "test");
let output_type = if has_flag(matches, "json") {
OutputType::Json
} else if has_flag(&matches, "no_output") || test {
} else if has_flag(matches, "no_output") || test {
OutputType::NoOutput
} else {
OutputType::ResponseBody
};
let progress = has_flag(&matches, "progress") || test;
let proxy = get_string(&matches, "proxy");
let summary = has_flag(&matches, "summary") || test;
let timeout = match get_string(&matches, "max_time") {
let progress = has_flag(matches, "progress") || test;
let proxy = get_string(matches, "proxy");
let summary = has_flag(matches, "summary") || test;
let timeout = match get_string(matches, "max_time") {
None => ClientOptions::default().timeout,
Some(s) => match s.parse::<u64>() {
Ok(n) => Duration::from_secs(n),
Expand All @@ -394,11 +399,12 @@ pub fn parse_options(matches: &ArgMatches) -> Result<CliOptions, CliError> {
}
},
};
let to_entry = to_entry(matches.clone())?;
let user = get_string(&matches, "user");
let user_agent = get_string(&matches, "user_agent");
let to_entry = to_entry(matches)?;
let user = get_string(matches, "user");
let user_agent = get_string(matches, "user_agent");
let variables = variables(matches.clone())?;
let verbose = has_flag(&matches, "verbose") || has_flag(&matches, "interactive");
let very_verbose = has_flag(matches, "very_verbose");
let verbose = has_flag(matches, "verbose") || has_flag(matches, "interactive") || very_verbose;

Ok(CliOptions {
cacert_file,
Expand Down Expand Up @@ -430,21 +436,22 @@ pub fn parse_options(matches: &ArgMatches) -> Result<CliOptions, CliError> {
user_agent,
variables,
verbose,
very_verbose,
})
}

pub fn output_color(matches: ArgMatches) -> bool {
if has_flag(&matches, "color") {
pub fn output_color(matches: &ArgMatches) -> bool {
if has_flag(matches, "color") {
true
} else if has_flag(&matches, "no_color") {
} else if has_flag(matches, "no_color") {
false
} else {
atty::is(Stream::Stdout)
}
}

fn to_entry(matches: ArgMatches) -> Result<Option<usize>, CliError> {
match get_string(&matches, "to_entry") {
fn to_entry(matches: &ArgMatches) -> Result<Option<usize>, CliError> {
match get_string(matches, "to_entry") {
Some(value) => match value.parse() {
Ok(v) => Ok(Some(v)),
Err(_) => Err(CliError {
Expand Down
13 changes: 8 additions & 5 deletions packages/hurl/src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use super::options::ClientOptions;
use super::request::*;
use super::request_spec::*;
use super::response::*;
use crate::http::HttpError;
use crate::http::{HttpError, Verbosity};
use std::str::FromStr;
use url::Url;

Expand Down Expand Up @@ -70,8 +70,11 @@ impl Client {
}
}

/// Execute an HTTP request and returns a list
///
/// Execute an http request
/// # Arguments
///
/// * request - A request specification
///
pub fn execute_with_redirect(
&mut self,
Expand Down Expand Up @@ -153,7 +156,7 @@ impl Client {

self.set_headers(request);

let verbose = self.options.verbose;
let verbose = self.options.verbosity != None;
let mut request_headers: Vec<Header> = vec![];

let start = Instant::now();
Expand Down Expand Up @@ -493,7 +496,7 @@ impl Client {
/// Add cookie to Cookiejar
///
pub fn add_cookie(&mut self, cookie: Cookie) {
if self.options.verbose {
if self.options.verbosity != None {
eprintln!("* add to cookie store: {}", cookie);
}
self.handle
Expand All @@ -505,7 +508,7 @@ impl Client {
/// Clear cookie storage
///
pub fn clear_cookie_storage(&mut self) {
if self.options.verbose {
if self.options.verbosity != None {
eprintln!("* clear cookie storage");
}
self.handle.cookie_list("ALL").unwrap();
Expand Down
2 changes: 1 addition & 1 deletion packages/hurl/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use self::client::Client;
pub use self::cookie::{CookieAttribute, ResponseCookie};
pub use self::core::{Cookie, Header, Param, RequestCookie};
pub use self::error::HttpError;
pub use self::options::ClientOptions;
pub use self::options::{ClientOptions, Verbosity};
pub use self::request::Request;
#[cfg(test)]
pub use self::request_spec::tests::*;
Expand Down
12 changes: 9 additions & 3 deletions packages/hurl/src/http/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct ClientOptions {
pub cookie_input_file: Option<String>,
pub proxy: Option<String>,
pub no_proxy: Option<String>,
pub verbose: bool,
pub verbosity: Option<Verbosity>,
pub insecure: bool,
pub timeout: Duration,
pub connect_timeout: Duration,
Expand All @@ -36,6 +36,12 @@ pub struct ClientOptions {
pub context_dir: PathBuf,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Verbosity {
Verbose,
VeryVerbose,
}

impl Default for ClientOptions {
fn default() -> Self {
ClientOptions {
Expand All @@ -45,7 +51,7 @@ impl Default for ClientOptions {
cookie_input_file: None,
proxy: None,
no_proxy: None,
verbose: false,
verbosity: None,
insecure: false,
timeout: Duration::from_secs(300),
connect_timeout: Duration::from_secs(300),
Expand Down Expand Up @@ -130,7 +136,7 @@ mod tests {
cookie_input_file: Some("cookie_file".to_string()),
proxy: Some("localhost:3128".to_string()),
no_proxy: None,
verbose: true,
verbosity: None,
insecure: true,
timeout: Duration::from_secs(10),
connect_timeout: Duration::from_secs(20),
Expand Down
17 changes: 13 additions & 4 deletions packages/hurl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use colored::*;
use hurl::cli;
use hurl::cli::{CliError, CliOptions, OutputType};
use hurl::http;
use hurl::http::Verbosity;
use hurl::report;
use hurl::report::canonicalize_filename;
use hurl::runner;
Expand Down Expand Up @@ -147,7 +148,11 @@ fn execute(

let cacert_file = cli_options.cacert_file.clone();
let follow_location = cli_options.follow_location;
let verbose = cli_options.verbose;
let verbosity = match (cli_options.verbose, cli_options.very_verbose) {
(true, true) => Some(Verbosity::VeryVerbose),
(true, _) => Some(Verbosity::Verbose),
_ => None,
};
let insecure = cli_options.insecure;
let max_redirect = cli_options.max_redirect;
let proxy = cli_options.proxy.clone();
Expand Down Expand Up @@ -177,7 +182,7 @@ fn execute(
cookie_input_file,
proxy,
no_proxy,
verbose,
verbosity,
insecure,
timeout,
connect_timeout,
Expand All @@ -204,12 +209,14 @@ fn execute(
let to_entry = cli_options.to_entry;
let context_dir = context_dir.to_path_buf();
let ignore_asserts = cli_options.ignore_asserts;
let very_verbose = cli_options.very_verbose;
let options = RunnerOptions {
fail_fast,
variables,
to_entry,
context_dir,
ignore_asserts,
very_verbose,
pre_entry,
post_entry,
};
Expand Down Expand Up @@ -262,9 +269,11 @@ fn main() {
let matches = app.clone().get_matches();
init_colored();

let verbose = cli::has_flag(&matches, "verbose") || cli::has_flag(&matches, "interactive");
let verbose = cli::has_flag(&matches, "verbose")
|| cli::has_flag(&matches, "very_verbose")
|| cli::has_flag(&matches, "interactive");
let log_verbose = cli::make_logger_verbose(verbose);
let color = cli::output_color(matches.clone());
let color = cli::output_color(&matches);
let log_error_message = cli::make_logger_error_message(color);
let cli_options = unwrap_or_exit(cli::parse_options(&matches), &log_error_message);

Expand Down
1 change: 1 addition & 0 deletions packages/hurl/src/runner/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct RunnerOptions {
pub to_entry: Option<usize>,
pub context_dir: PathBuf,
pub ignore_asserts: bool,
pub very_verbose: bool, // If true, log body response in verbose mode.
pub pre_entry: fn(Entry) -> bool,
pub post_entry: fn() -> bool,
}
Expand Down
Loading

0 comments on commit 75b34f8

Please sign in to comment.