Skip to content

Commit f543f8d

Browse files
committed
Refacto env vars configuration management.
1 parent 4032c50 commit f543f8d

3 files changed

Lines changed: 338 additions & 177 deletions

File tree

packages/hurl/src/cli/options/args.rs

Lines changed: 26 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,9 @@ fn parse_arg_matches(
190190
let digest = digest(arg_matches, default_options.digest);
191191
let error_format = error_format(arg_matches, default_options.error_format)?;
192192
let file_root = file_root(arg_matches, default_options.file_root);
193-
let (follow_location, follow_location_trusted) = follow_location(
194-
arg_matches,
195-
(
196-
default_options.follow_location,
197-
default_options.follow_location_trusted,
198-
),
199-
);
193+
let follow_location = follow_location(arg_matches, default_options.follow_location);
194+
let follow_location_trusted =
195+
follow_location_trusted(arg_matches, default_options.follow_location_trusted);
200196
let from_entry = from_entry(arg_matches, default_options.from_entry);
201197
let headers = headers(arg_matches, default_options.headers);
202198
let html_dir = html_dir(arg_matches, default_options.html_dir)?;
@@ -241,23 +237,7 @@ fn parse_arg_matches(
241237
let user = user(arg_matches, default_options.user);
242238
let user_agent = user_agent(arg_matches, default_options.user_agent);
243239
let variables = variables(arg_matches, default_options.variables)?;
244-
245-
let verbose = verbose(
246-
arg_matches,
247-
default_options.verbosity == Some(Verbosity::Verbose),
248-
);
249-
let very_verbose = very_verbose(
250-
arg_matches,
251-
default_options.verbosity == Some(Verbosity::Debug),
252-
);
253-
// --verbose and --very-verbose flags are prioritized over --verbosity enum.
254-
let verbosity = if verbose {
255-
Some(Verbosity::Verbose)
256-
} else if very_verbose {
257-
Some(Verbosity::Debug)
258-
} else {
259-
verbosity(arg_matches, default_options.verbosity)
260-
};
240+
let verbosity = verbosity(arg_matches, default_options.verbosity);
261241

262242
Ok(CliOptions {
263243
aws_sigv4,
@@ -387,12 +367,12 @@ fn client_key_file(
387367

388368
/// Returns true if Hurl output uses ANSI code and false otherwise.
389369
fn color(arg_matches: &ArgMatches, default_value: bool) -> bool {
390-
if has_flag(arg_matches, "color") {
391-
return true;
392-
}
393370
if has_flag(arg_matches, "no_color") {
394371
return false;
395372
}
373+
if has_flag(arg_matches, "color") {
374+
return true;
375+
}
396376
default_value
397377
}
398378

@@ -471,12 +451,18 @@ fn file_root(arg_matches: &ArgMatches, default_value: Option<String>) -> Option<
471451
get::<String>(arg_matches, "file_root").or(default_value)
472452
}
473453

474-
fn follow_location(arg_matches: &ArgMatches, default_value: (bool, bool)) -> (bool, bool) {
475-
let follow_location = has_flag(arg_matches, "follow_location")
476-
|| has_flag(arg_matches, "follow_location_trusted");
477-
let follow_location_trusted = has_flag(arg_matches, "follow_location_trusted");
478-
if follow_location || follow_location_trusted {
479-
(follow_location, follow_location_trusted)
454+
fn follow_location(arg_matches: &ArgMatches, default_value: bool) -> bool {
455+
if has_flag(arg_matches, "follow_location") || has_flag(arg_matches, "follow_location_trusted")
456+
{
457+
true
458+
} else {
459+
default_value
460+
}
461+
}
462+
463+
fn follow_location_trusted(arg_matches: &ArgMatches, default_value: bool) -> bool {
464+
if has_flag(arg_matches, "follow_location_trusted") {
465+
true
480466
} else {
481467
default_value
482468
}
@@ -973,25 +959,15 @@ fn variables(
973959
}
974960

975961
fn verbosity(arg_matches: &ArgMatches, default_value: Option<Verbosity>) -> Option<Verbosity> {
976-
match get::<String>(arg_matches, "verbosity") {
977-
Some(value) => Verbosity::from_str(&value).ok(),
978-
None => default_value,
979-
}
980-
}
981-
982-
fn verbose(arg_matches: &ArgMatches, default_value: bool) -> bool {
983962
if has_flag(arg_matches, "verbose") {
984-
true
985-
} else {
986-
default_value
987-
}
988-
}
989-
990-
fn very_verbose(arg_matches: &ArgMatches, default_value: bool) -> bool {
991-
if has_flag(arg_matches, "very_verbose") {
992-
true
963+
Some(Verbosity::Verbose)
964+
} else if has_flag(arg_matches, "very_verbose") {
965+
Some(Verbosity::Debug)
993966
} else {
994-
default_value
967+
match get::<String>(arg_matches, "verbosity") {
968+
Some(value) => Verbosity::from_str(&value).ok(),
969+
None => default_value,
970+
}
995971
}
996972
}
997973

packages/hurl/src/cli/options/context.rs

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -262,22 +262,26 @@ impl RunContext {
262262
.collect()
263263
}
264264

265-
/// Returns `Some(true)` if ANSI escape codes are explicitly enabled, `Some(false)` if ANSI escape
266-
/// codes are explicitly disabled, `None` otherwise. .
267-
pub fn use_color_env_var(&self) -> Option<bool> {
268-
// According to the NO_COLOR spec, any presence of the variable should disable color, but to
269-
// maintain backward compatibility with code < 7.1.0, we check that the NO_COLOR env is at
270-
// least not empty.
265+
/// Returns `Some(true)` if color is set through env, `Some(false)` if color is disable through env,
266+
/// `None` otherwise.
267+
pub fn color_env_var(&self) -> Option<bool> {
268+
self.get_env_var_bool(HURL_COLOR)
269+
}
270+
271+
/// Returns `Some(true)` if no color is set through env, `Some(false)` if no color is disable through env,
272+
/// `None` otherwise.
273+
pub fn no_color_env_var(&self) -> Option<bool> {
271274
if let Some(v) = self.env_vars.get("NO_COLOR") {
275+
// According to the NO_COLOR spec, any presence of the variable should disable color, but to
276+
// maintain backward compatibility with code < 7.1.0, we check that the NO_COLOR env is at
277+
// least not empty.
272278
if !v.is_empty() {
273-
Some(false)
279+
Some(true)
274280
} else {
275281
None
276282
}
277-
} else if let Some(v) = self.get_env_var_bool(HURL_NO_COLOR) {
278-
Some(!v)
279283
} else {
280-
self.get_env_var_bool(HURL_COLOR)
284+
self.get_env_var_bool(HURL_NO_COLOR)
281285
}
282286
}
283287

@@ -367,28 +371,16 @@ mod tests {
367371

368372
let env_vars = HashMap::from([("A".to_string(), "B".to_string())]);
369373
let ctx = RunContext::new(env_vars, stdin_term, stdout_term, stderr_term);
370-
assert!(ctx.use_color_env_var().is_none());
374+
assert!(ctx.color_env_var().is_none());
371375
}
372376

373377
#[test]
374-
fn context_has_env_var_color() {
378+
fn context_has_color_env_var() {
375379
let stdin_term = true;
376380
let stdout_term = true;
377381
let stderr_term = true;
378382

379383
let data = [
380-
("NO_COLOR", "0", Some(false)),
381-
("NO_COLOR", "1", Some(false)),
382-
("NO_COLOR", "true", Some(false)),
383-
("NO_COLOR", "TRUE", Some(false)),
384-
("NO_COLOR", "false", Some(false)),
385-
("NO_COLOR", "FALSE", Some(false)),
386-
("HURL_NO_COLOR", "0", Some(true)),
387-
("HURL_NO_COLOR", "1", Some(false)),
388-
("HURL_NO_COLOR", "true", Some(false)),
389-
("HURL_NO_COLOR", "TRUE", Some(false)),
390-
("HURL_NO_COLOR", "false", Some(true)),
391-
("HURL_NO_COLOR", "FALSE", Some(true)),
392384
("HURL_COLOR", "0", Some(false)),
393385
("HURL_COLOR", "1", Some(true)),
394386
("HURL_COLOR", "true", Some(true)),
@@ -401,7 +393,41 @@ mod tests {
401393
let env_vars = HashMap::from([(name.to_string(), value.to_string())]);
402394
let ctx = RunContext::new(env_vars, stdin_term, stdout_term, stderr_term);
403395
assert_eq!(
404-
ctx.use_color_env_var(),
396+
ctx.color_env_var(),
397+
expected,
398+
"test env var {}={}",
399+
name,
400+
value
401+
);
402+
}
403+
}
404+
405+
#[test]
406+
fn context_has_no_color_env_var() {
407+
let stdin_term = true;
408+
let stdout_term = true;
409+
let stderr_term = true;
410+
411+
let data = [
412+
("NO_COLOR", "0", Some(true)),
413+
("NO_COLOR", "1", Some(true)),
414+
("NO_COLOR", "true", Some(true)),
415+
("NO_COLOR", "TRUE", Some(true)),
416+
("NO_COLOR", "false", Some(true)),
417+
("NO_COLOR", "FALSE", Some(true)),
418+
("HURL_NO_COLOR", "0", Some(false)),
419+
("HURL_NO_COLOR", "1", Some(true)),
420+
("HURL_NO_COLOR", "true", Some(true)),
421+
("HURL_NO_COLOR", "TRUE", Some(true)),
422+
("HURL_NO_COLOR", "false", Some(false)),
423+
("HURL_NO_COLOR", "FALSE", Some(false)),
424+
];
425+
426+
for (name, value, expected) in data {
427+
let env_vars = HashMap::from([(name.to_string(), value.to_string())]);
428+
let ctx = RunContext::new(env_vars, stdin_term, stdout_term, stderr_term);
429+
assert_eq!(
430+
ctx.no_color_env_var(),
405431
expected,
406432
"test env var {}={}",
407433
name,

0 commit comments

Comments
 (0)