Skip to content

Commit 7db0e7b

Browse files
authored
Merge pull request #122 from aliev/feat/post-hook-print-stdout
feat: add option to print post-hook stdout
2 parents d857153 + c8cd277 commit 7db0e7b

6 files changed

Lines changed: 62 additions & 5 deletions

File tree

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,23 @@ This feature is especially helpful on Windows where scripts such as `.ps1`
684684
cannot be launched directly, and on Unix when you want to force a specific
685685
interpreter (e.g., Python, Node.js, Bash).
686686

687+
### Displaying Post-hook Stdout
688+
689+
By default, Baker stores `post` hook `stdout` in debug logs only. If you want
690+
users to see post-hook messages directly in the terminal (for example, welcome
691+
text or next-step instructions), enable `post_hook_print_stdout`:
692+
693+
```yaml
694+
post_hook_filename: post.py
695+
post_hook_runner:
696+
- python3
697+
post_hook_print_stdout: true
698+
```
699+
700+
When enabled, Baker prints the post-hook `stdout` to the screen after the hook
701+
finishes. Keep in mind this output becomes visible in CI logs and terminal
702+
history, so hooks should avoid printing secrets.
703+
687704
### Available Platform Variables
688705

689706
Baker provides these platform variables that can be used in templates and hook filenames:

src/cli/hooks.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub fn run_hook<P: AsRef<Path>>(
4040
hook_path: P,
4141
answers: Option<&serde_json::Value>,
4242
runner: &[String],
43+
inherit_stdout: bool,
4344
) -> Result<Option<String>> {
4445
let hook_path = hook_path.as_ref();
4546

@@ -70,7 +71,7 @@ pub fn run_hook<P: AsRef<Path>>(
7071

7172
let mut child = command
7273
.stdin(Stdio::piped())
73-
.stdout(Stdio::piped())
74+
.stdout(if inherit_stdout { Stdio::inherit() } else { Stdio::piped() })
7475
.stderr(Stdio::inherit())
7576
.spawn()?;
7677

@@ -151,6 +152,7 @@ mod tests {
151152
&script_path,
152153
None,
153154
&["sh".to_string()],
155+
false,
154156
)
155157
.expect("hook execution")
156158
.expect("stdout");
@@ -177,10 +179,16 @@ mod tests {
177179
"-File".to_string(),
178180
];
179181

180-
let output =
181-
run_hook(temp_dir.path(), temp_dir.path(), &script_path, None, &runner)
182-
.expect("hook execution")
183-
.expect("stdout");
182+
let output = run_hook(
183+
temp_dir.path(),
184+
temp_dir.path(),
185+
&script_path,
186+
None,
187+
&runner,
188+
false,
189+
)
190+
.expect("hook execution")
191+
.expect("stdout");
184192

185193
assert!(output.contains("windows_runner"));
186194
}

src/cli/processor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ mod tests {
288288
pre_hook_filename: "pre".into(),
289289
post_hook_runner: Vec::new(),
290290
pre_hook_runner: Vec::new(),
291+
post_hook_print_stdout: false,
291292
follow_symlinks,
292293
},
293294
skip_confirms,

src/cli/runner.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ impl Runner {
112112
)?;
113113
let pre_hook_runner = config.pre_hook_runner.clone();
114114
let post_hook_runner = config.post_hook_runner.clone();
115+
let post_hook_print_stdout = config.post_hook_print_stdout;
115116

116117
let execute_hooks = self.confirm_hook_execution(
117118
context.template_root(),
@@ -139,6 +140,7 @@ impl Runner {
139140
execute_hooks,
140141
pre_hook_runner,
141142
post_hook_runner,
143+
post_hook_print_stdout,
142144
})
143145
}
144146

@@ -170,6 +172,7 @@ impl Runner {
170172
&hook_plan.pre_hook_file,
171173
None,
172174
&runner,
175+
false,
173176
)
174177
} else {
175178
Ok(None)
@@ -236,6 +239,7 @@ impl Runner {
236239
&hook_plan.post_hook_file,
237240
Some(context.answers()),
238241
&runner,
242+
hook_plan.post_hook_print_stdout,
239243
)?;
240244

241245
if let Some(result) = post_hook_stdout {
@@ -452,6 +456,7 @@ struct HookPlan {
452456
execute_hooks: bool,
453457
pre_hook_runner: Vec<String>,
454458
post_hook_runner: Vec<String>,
459+
post_hook_print_stdout: bool,
455460
}
456461

457462
fn render_hook_runner(

src/config/loader.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ pub struct ConfigV1 {
3434
pub post_hook_runner: Vec<String>,
3535
#[serde(default = "get_default_pre_hook_runner")]
3636
pub pre_hook_runner: Vec<String>,
37+
#[serde(default = "get_default_post_hook_print_stdout")]
38+
pub post_hook_print_stdout: bool,
3739
#[serde(default = "get_default_follow_symlinks")]
3840
pub follow_symlinks: bool,
3941
}
@@ -110,6 +112,10 @@ fn get_default_pre_hook_runner() -> Vec<String> {
110112
Vec::new()
111113
}
112114

115+
fn get_default_post_hook_print_stdout() -> bool {
116+
false
117+
}
118+
113119
fn get_default_loop_separator() -> String {
114120
DEFAULT_LOOP_SEPARATOR.to_string()
115121
}
@@ -172,6 +178,25 @@ questions: {}"#;
172178
assert!(!cfg.follow_symlinks);
173179
}
174180

181+
#[test]
182+
fn post_hook_print_stdout_defaults_false() {
183+
let raw = r#"schemaVersion: v1
184+
questions: {}"#;
185+
let config: Config = serde_yaml::from_str(raw).expect("valid config");
186+
let Config::V1(cfg) = config;
187+
assert!(!cfg.post_hook_print_stdout);
188+
}
189+
190+
#[test]
191+
fn post_hook_print_stdout_parses_true() {
192+
let raw = r#"schemaVersion: v1
193+
post_hook_print_stdout: true
194+
questions: {}"#;
195+
let config: Config = serde_yaml::from_str(raw).expect("valid config");
196+
let Config::V1(cfg) = config;
197+
assert!(cfg.post_hook_print_stdout);
198+
}
199+
175200
#[test]
176201
fn follow_symlinks_parses_true() {
177202
let raw = r#"schemaVersion: v1

src/template/processor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ mod tests {
427427
pre_hook_filename: "pre".into(),
428428
post_hook_runner: Vec::new(),
429429
pre_hook_runner: Vec::new(),
430+
post_hook_print_stdout: false,
430431
follow_symlinks: false,
431432
},
432433
Vec::new(),

0 commit comments

Comments
 (0)