Skip to content

reinstall: Cleaner formatting of podman bootc install message #1298

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

Merged
merged 1 commit into from
May 5, 2025
Merged
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
4 changes: 3 additions & 1 deletion system-reinstall-bootc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ fn run() -> Result<()> {
podman::reinstall_command(&config.bootc_image, ssh_key_file_path);

println!();
println!("Going to run command {:?}", reinstall_podman_command);
println!("Going to run command:");
println!();
println!("{}", reinstall_podman_command.to_string_pretty());

prompt::temporary_developer_protection_prompt()?;

Expand Down
40 changes: 40 additions & 0 deletions utils/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Helpers intended for [`std::process::Command`] and related structures.

use std::{
fmt::Write,
io::{Read, Seek},
os::unix::process::CommandExt,
process::Command,
Expand Down Expand Up @@ -33,6 +34,9 @@ pub trait CommandRunExt {
/// Execute the child process, parsing its stdout as JSON. This uses `run` internally
/// and will return an error if the child process exits abnormally.
fn run_and_parse_json<T: serde::de::DeserializeOwned>(&mut self) -> Result<T>;

/// Print the command as it would be typed into a terminal
fn to_string_pretty(&self) -> String;
}

/// Helpers intended for [`std::process::ExitStatus`].
Expand Down Expand Up @@ -146,6 +150,19 @@ impl CommandRunExt for Command {
// representation that the user can copy paste into their shell
.context(format!("Failed to run command: {self:#?}"))
}

fn to_string_pretty(&self) -> String {
std::iter::once(self.get_program())
.chain(self.get_args())
.fold(String::new(), |mut acc, element| {
if !acc.is_empty() {
acc.push(' ');
}
// SAFETY: Writes to string can't fail
write!(&mut acc, "{}", crate::PathQuotedDisplay::new(&element)).unwrap();
acc
})
}
}

/// Helpers intended for [`tokio::process::Command`].
Expand Down Expand Up @@ -224,4 +241,27 @@ mod tests {
success.unwrap();
assert!(fail.is_err());
}

#[test]
fn to_string_pretty() {
let mut cmd = Command::new("podman");
cmd.args([
"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",
]);

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");
}
}