Skip to content

Commit f7c86e5

Browse files
committed
cmds: update GARDEN_CMD_VERBOSE and GARDEN_CMD_QUIET in sub-commands
"garden my-command -v" should behave the same as "garden -v my-command". Repeat the variable initialization after the sub-commands have been parsed to make the behavior match.
1 parent cc0e369 commit f7c86e5

File tree

6 files changed

+68
-15
lines changed

6 files changed

+68
-15
lines changed

doc/src/changelog.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## Upcoming
4+
5+
**Features**:
6+
7+
- `GARDEN_CMD_VERBOSE` and `GARDEN_CMD_QUIET` are now updated when using
8+
`garden <command> -v ...` and `garden cmd <command> -v ...`.
9+
Previously, these variables were only set when `-v` was used against the `garden`
10+
command directly, before any sub-commands; e.g. `garden -v ...`.
11+
12+
313
## v1.9.0
414

515
*Released 2024-10-11*

src/cmds/cmd.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ pub fn main_cmd(app_context: &model::ApplicationContext, options: &mut CmdOption
140140
app_context
141141
.get_root_config_mut()
142142
.apply_defines(&options.define);
143+
app_context
144+
.get_root_config_mut()
145+
.update_quiet_and_verbose_variables(options.quiet, options.verbose);
143146
if app_context.options.debug_level(constants::DEBUG_LEVEL_CMD) > 0 {
144147
debug!("jobs: {:?}", options.num_jobs);
145148
debug!("query: {}", options.query);
@@ -275,6 +278,9 @@ pub fn main_custom(app_context: &model::ApplicationContext, arguments: &Vec<Stri
275278
app_context
276279
.get_root_config_mut()
277280
.apply_defines(&options.define);
281+
app_context
282+
.get_root_config_mut()
283+
.update_quiet_and_verbose_variables(options.quiet, options.verbose);
278284
if !app_context.get_root_config().shell_exit_on_error {
279285
options.exit_on_error = false;
280286
}

src/config/reader.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,10 @@ fn parse_recursive(
139139
}
140140

141141
// Provide GARDEN_CMD_QUIET and GARDEN_CMD_VERBOSE.
142-
let quiet_string = if config.quiet { "--quiet" } else { "" }.to_string();
143-
let verbose_string = if config.verbose > 0 {
144-
// -v can be specified multiple times, e.g. "-vv".
145-
format!("-{}", "v".repeat(config.verbose.into()))
146-
} else {
147-
string!("")
148-
};
149-
config.variables.insert(
150-
string!(constants::GARDEN_CMD_QUIET),
151-
model::Variable::new(quiet_string.clone(), Some(quiet_string)),
152-
);
153-
config.variables.insert(
154-
string!(constants::GARDEN_CMD_VERBOSE),
155-
model::Variable::new(verbose_string.clone(), Some(verbose_string)),
156-
);
142+
// When commands call update_quiet_and_verbose_variables() they are adding
143+
// additional "-v" options beyond what has been parsed at this point.
144+
// We use 0 here so that the config's verbosity level is maintained.
145+
config.update_quiet_and_verbose_variables(config.quiet, 0);
157146

158147
// Variables are read early to make them available to config.eval_config_pathbuf_from_include().
159148
// Variables are reloaded after "includes" to give the current garden file the highest priority.

src/model.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,26 @@ impl Configuration {
800800
}
801801
}
802802

803+
/// Apply the quiet and verbose options to resolve GARDEN_CMD_VERBOSE and GARDEN_CMD_QUIET.
804+
pub(crate) fn update_quiet_and_verbose_variables(&mut self, quiet: bool, verbose: u8) {
805+
// Provide GARDEN_CMD_QUIET and GARDEN_CMD_VERBOSE.
806+
let quiet_string = if self.quiet || quiet { "--quiet" } else { "" }.to_string();
807+
self.variables.insert(
808+
string!(constants::GARDEN_CMD_QUIET),
809+
Variable::new(quiet_string.clone(), Some(quiet_string)),
810+
);
811+
let verbose = self.verbose + verbose;
812+
let verbose_string = if verbose > 0 {
813+
format!("-{}", "v".repeat(verbose.into()))
814+
} else {
815+
string!("")
816+
};
817+
self.variables.insert(
818+
string!(constants::GARDEN_CMD_VERBOSE),
819+
Variable::new(verbose_string.clone(), Some(verbose_string)),
820+
);
821+
}
822+
803823
pub(crate) fn reset(&mut self) {
804824
// Reset variables to allow for tree-scope evaluation
805825
self.reset_variables();

tests/data/garden.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ trees:
8989
do
9090
echo $arg
9191
done
92+
echo-quiet-verbose: |
93+
printf 'verbose=%s\n' "${GARDEN_CMD_VERBOSE}"
94+
printf 'quiet=%s\n' "${GARDEN_CMD_QUIET}"
9295
9396
example/shallow:
9497
path: example/tree/shallow

tests/integration_test.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,31 @@ fn eval_builtin_command_variables() {
760760
assert_eq!(output, expect);
761761
}
762762

763+
/// `garden custom-command` handles ${GARDEN_CMD_QUIET} and ${GARDEN_CMD_VERBOSE}.
764+
#[test]
765+
fn eval_builtin_command_variables_in_subcommand() {
766+
// garden echo-quiet-verbose -v
767+
let output = garden_capture(&[
768+
"--config",
769+
"tests/data/garden.yaml",
770+
"echo-quiet-verbose",
771+
"-v",
772+
]);
773+
let expect = "verbose=-v\nquiet=";
774+
assert_eq!(output, expect);
775+
// garden -v echo-quiet-verbose -v --quiet
776+
let output = garden_capture(&[
777+
"--verbose",
778+
"--config",
779+
"tests/data/garden.yaml",
780+
"echo-quiet-verbose",
781+
"--verbose",
782+
"--quiet",
783+
]);
784+
let expect = "verbose=-vv\nquiet=--quiet";
785+
assert_eq!(output, expect);
786+
}
787+
763788
/// `garden eval` evaluates overridden variables.
764789
#[test]
765790
fn eval_override_variables() {

0 commit comments

Comments
 (0)