Skip to content

Commit 3a076c5

Browse files
committed
reinstall: Cleaner formatting of podman bootc install message
This makes it easier to copy/paste (and read) the `podman ... bootc install ...` command from the CLI output. Signed-off-by: ckyrouac <ckyrouac@redhat.com>
1 parent 6e1ccb0 commit 3a076c5

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

system-reinstall-bootc/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ fn run() -> Result<()> {
4444
podman::reinstall_command(&config.bootc_image, ssh_key_file_path);
4545

4646
println!();
47-
println!("Going to run command {:?}", reinstall_podman_command);
47+
println!("Going to run command:");
48+
println!();
49+
println!("{}", reinstall_podman_command.to_string_pretty());
4850

4951
prompt::temporary_developer_protection_prompt()?;
5052

utils/src/command.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Helpers intended for [`std::process::Command`] and related structures.
22
33
use std::{
4+
fmt::Write,
45
io::{Read, Seek},
56
os::unix::process::CommandExt,
67
process::Command,
@@ -33,6 +34,9 @@ pub trait CommandRunExt {
3334
/// Execute the child process, parsing its stdout as JSON. This uses `run` internally
3435
/// and will return an error if the child process exits abnormally.
3536
fn run_and_parse_json<T: serde::de::DeserializeOwned>(&mut self) -> Result<T>;
37+
38+
/// Print the command as it would be typed into a terminal
39+
fn to_string_pretty(&mut self) -> String;
3640
}
3741

3842
/// Helpers intended for [`std::process::ExitStatus`].
@@ -146,6 +150,19 @@ impl CommandRunExt for Command {
146150
// representation that the user can copy paste into their shell
147151
.context(format!("Failed to run command: {self:#?}"))
148152
}
153+
154+
fn to_string_pretty(&mut self) -> String {
155+
std::iter::once(self.get_program())
156+
.chain(self.get_args())
157+
.fold(String::new(), |mut acc, element| {
158+
if !acc.is_empty() {
159+
acc.push(' ');
160+
}
161+
// SAFETY: Writes to string can't fail
162+
write!(&mut acc, "{}", crate::PathQuotedDisplay::new(&element)).unwrap();
163+
acc
164+
})
165+
}
149166
}
150167

151168
/// Helpers intended for [`tokio::process::Command`].
@@ -224,4 +241,27 @@ mod tests {
224241
success.unwrap();
225242
assert!(fail.is_err());
226243
}
244+
245+
#[test]
246+
fn to_string_pretty() {
247+
let mut cmd = Command::new("podman");
248+
cmd.args([
249+
"run",
250+
"--privileged",
251+
"--pid=host",
252+
"--user=root:root",
253+
"-v",
254+
"/var/lib/containers:/var/lib/containers",
255+
"-v",
256+
"this has spaces",
257+
"label=type:unconfined_t",
258+
"--env=RUST_LOG=trace",
259+
"quay.io/ckyrouac/bootc-dev",
260+
"bootc",
261+
"install",
262+
"to-existing-root",
263+
]);
264+
265+
assert_eq!(cmd.to_string_pretty(), "podman run --privileged '--pid=host' '--user=root:root' -v /var/lib/containers:/var/lib/containers -v 'this has spaces' 'label=type:unconfined_t' '--env=RUST_LOG=trace' quay.io/ckyrouac/bootc-dev bootc install to-existing-root");
266+
}
227267
}

0 commit comments

Comments
 (0)