Skip to content

Feature/support config file #3970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions integration/hurl/tests_ok/config_file.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GET http://localhost:8000/hello

2 changes: 2 additions & 0 deletions integration/hurl/tests_ok/config_file.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello World!Hello World!
Hello World!
7 changes: 7 additions & 0 deletions integration/hurl/tests_ok/config_file.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'

$env:XDG_CONFIG_HOME = Split-Path -Parent $MyInvocation.MyCommand.Path
hurl tests_ok/config_file.hurl
Write-Output ""
hurl --repeat 1 tests_ok/config_file.hurl
7 changes: 7 additions & 0 deletions integration/hurl/tests_ok/config_file.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
set -Eeuo pipefail

export XDG_CONFIG_HOME="$(dirname "$0")"
hurl tests_ok/config_file.hurl
echo
hurl --repeat 1 tests_ok/config_file.hurl
1 change: 1 addition & 0 deletions integration/hurl/tests_ok/hurlrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--repeat=2
4 changes: 2 additions & 2 deletions packages/hurl/src/cli/options/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,11 @@ pub fn proxy(arg_matches: &ArgMatches) -> Option<String> {
get::<String>(arg_matches, "proxy")
}

pub fn repeat(arg_matches: &ArgMatches) -> Option<Count> {
pub fn repeat(arg_matches: &ArgMatches, default_value: Option<Count>) -> Option<Count> {
match get::<i32>(arg_matches, "repeat") {
Some(-1) => Some(Count::Infinite),
Some(n) => Some(Count::Finite(n as usize)),
None => None,
None => default_value,
}
}

Expand Down
55 changes: 52 additions & 3 deletions packages/hurl/src/cli/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use crate::cli;
use crate::runner::{RunnerOptions, RunnerOptionsBuilder, Value};

/// Represents the list of all options that can be used in Hurl command line.
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub struct CliOptions {
pub aws_sigv4: Option<String>,
pub cacert_file: Option<String>,
Expand Down Expand Up @@ -113,6 +113,12 @@ pub enum ErrorFormat {
Long,
}

impl Default for ErrorFormat {
fn default() -> Self {
ErrorFormat::Short
}
}

impl From<ErrorFormat> for hurl::util::logger::ErrorFormat {
fn from(value: ErrorFormat) -> Self {
match value {
Expand Down Expand Up @@ -275,7 +281,15 @@ pub fn parse(allow_color: bool) -> Result<CliOptions, CliOptionsError> {
return Err(CliOptionsError::NoInput(help));
}

let opts = parse_matches(&arg_matches, allow_color)?;
let config_options = if let Some(hurlrc_file) = get_config_file() {
parse_config_file(&hurlrc_file)?
} else {
//eprintln!(">>> config file does not exist");
CliOptions::default()
};
//eprintln!(">>> config option repeat={:?}", config_options.repeat);

let opts = parse_matches(&arg_matches, allow_color, config_options)?;
if opts.input_files.is_empty() {
return Err(CliOptionsError::Error(
"No input files provided".to_string(),
Expand All @@ -285,9 +299,39 @@ pub fn parse(allow_color: bool) -> Result<CliOptions, CliOptionsError> {
Ok(opts)
}

fn get_config_file() -> Option<PathBuf> {
let config_home = match env::var("XDG_CONFIG_HOME") {
Ok(val) => val,
Err(_) => match env::var("HOME") {
Ok(val) => format!("{val}/.config"),
Err(_) => return None,
},
};
let config_file = Path::new(&config_home).join("hurlrc").to_owned();
//eprintln!(">>> config_file={}", config_file.display());
if config_file.exists() {
Some(config_file)
} else {
None
}
}

impl CliOptions {
pub fn repeat(&mut self, value: Option<Count>) {
self.repeat = value;
}
}

fn parse_config_file(_config_file: &Path) -> Result<CliOptions, CliOptionsError> {
let mut options = CliOptions::default();
options.repeat(Some(Count::Finite(2)));
Ok(options)
}

fn parse_matches(
arg_matches: &ArgMatches,
allow_color: bool,
config_options: CliOptions,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to default_options

) -> Result<CliOptions, CliOptionsError> {
let aws_sigv4 = matches::aws_sigv4(arg_matches);
let cacert_file = matches::cacert_file(arg_matches)?;
Expand Down Expand Up @@ -331,7 +375,7 @@ fn parse_matches(
let proxy = matches::proxy(arg_matches);
let output = matches::output(arg_matches);
let output_type = matches::output_type(arg_matches);
let repeat = matches::repeat(arg_matches);
let repeat = matches::repeat(arg_matches, config_options.repeat);
let resolves = matches::resolves(arg_matches);
let retry = matches::retry(arg_matches);
let retry_interval = matches::retry_interval(arg_matches)?;
Expand Down Expand Up @@ -419,6 +463,11 @@ pub enum OutputType {
/// Nothing is outputted on standard output when a Hurl file run is completed.
NoOutput,
}
impl Default for OutputType {
fn default() -> Self {
OutputType::ResponseBody
}
}

impl CliOptions {
/// Converts this instance of [`CliOptions`] to an instance of [`RunnerOptions`]
Expand Down
5 changes: 5 additions & 0 deletions packages/hurl_core/src/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ pub enum Count {
Infinite,
}

impl Default for Count {
fn default() -> Self {
Count::Finite(0)
}
}
/// Represent a duration
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Duration {
Expand Down
Loading