Skip to content

Commit 5c9e00a

Browse files
committed
task,install: Add even more verbosity support
Let's print out the full command lines for a few select things. I thought about doing this for the `sgdisk` but that feels a bit too verbose right now. Signed-off-by: Colin Walters <[email protected]>
1 parent e82f97f commit 5c9e00a

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

lib/src/install/baseline.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ fn mkfs<'a>(
143143
t.cmd.arg(dev);
144144
// All the mkfs commands are unnecessarily noisy by default
145145
t.cmd.stdout(Stdio::null());
146-
t.run()?;
146+
// But this one is notable so let's print the whole thing with verbose()
147+
t.verbose().run()?;
147148
Ok(u)
148149
}
149150

@@ -345,10 +346,12 @@ pub(crate) fn install_create_rootfs(
345346
.args([base_rootdev.as_str()])
346347
.run()?;
347348
// The --wipe-slot=all removes our temporary passphrase, and binds to the local TPM device.
349+
// We also use .verbose() here as the details are important/notable.
348350
Task::new("Enrolling root device with TPM", "systemd-cryptenroll")
349351
.args(["--wipe-slot=all", "--tpm2-device=auto", "--unlock-key-file"])
350352
.args([tmp_keyfile])
351353
.args([base_rootdev.as_str()])
354+
.verbose()
352355
.run_with_stdin_buf(dummy_passphrase_input)?;
353356
Task::new("Opening root LUKS device", "cryptsetup")
354357
.args(["luksOpen", base_rootdev.as_str(), luks_name])

lib/src/task.rs

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,21 @@ use cap_std::fs::Dir;
99
use cap_std_ext::cap_std;
1010
use cap_std_ext::prelude::CapStdExtCommandExt;
1111

12+
/// How much information we output
13+
#[derive(Debug, PartialEq, Eq, Default)]
14+
enum CmdVerbosity {
15+
/// Nothing is output
16+
Quiet,
17+
/// Only the task description is output
18+
#[default]
19+
Description,
20+
/// The task description and the full command line are output
21+
Verbose,
22+
}
23+
1224
pub(crate) struct Task {
1325
description: String,
14-
quiet: bool,
26+
verbosity: CmdVerbosity,
1527
quiet_output: bool,
1628
pub(crate) cmd: Command,
1729
}
@@ -40,14 +52,21 @@ impl Task {
4052
cmd.stdin(Stdio::null());
4153
Self {
4254
description,
43-
quiet: false,
55+
verbosity: Default::default(),
4456
quiet_output: false,
4557
cmd,
4658
}
4759
}
4860

61+
/// Don't output description by default
4962
pub(crate) fn quiet(mut self) -> Self {
50-
self.quiet = true;
63+
self.verbosity = CmdVerbosity::Quiet;
64+
self
65+
}
66+
67+
/// Output description and cmdline
68+
pub(crate) fn verbose(mut self) -> Self {
69+
self.verbosity = CmdVerbosity::Verbose;
5170
self
5271
}
5372

@@ -72,13 +91,38 @@ impl Task {
7291
self.run_with_stdin_buf(None)
7392
}
7493

94+
fn pre_run_output(&self) {
95+
match self.verbosity {
96+
CmdVerbosity::Quiet => {}
97+
CmdVerbosity::Description => {
98+
println!("{}", self.description);
99+
}
100+
CmdVerbosity::Verbose => {
101+
// Output the description first
102+
println!("{}", self.description);
103+
104+
// Lock stdout so we buffer
105+
let mut stdout = std::io::stdout().lock();
106+
let cmd_args = std::iter::once(self.cmd.get_program())
107+
.chain(self.cmd.get_args())
108+
.map(|arg| arg.to_string_lossy());
109+
// We unwrap() here to match the default for println!() even though
110+
// arguably that's wrong
111+
stdout.write_all(b">").unwrap();
112+
for s in cmd_args {
113+
stdout.write_all(b" ").unwrap();
114+
stdout.write_all(s.as_bytes()).unwrap();
115+
}
116+
stdout.write_all(b"\n").unwrap();
117+
}
118+
}
119+
}
120+
75121
/// Run the command with optional stdin buffer, returning an error if the command does not exit successfully.
76122
pub(crate) fn run_with_stdin_buf(self, stdin: Option<&[u8]>) -> Result<()> {
123+
self.pre_run_output();
77124
let description = self.description;
78125
let mut cmd = self.cmd;
79-
if !self.quiet {
80-
println!("{description}");
81-
}
82126
let mut output = None;
83127
if self.quiet_output {
84128
let tmpf = tempfile::tempfile()?;
@@ -117,11 +161,9 @@ impl Task {
117161

118162
/// Like [`run()`], but return stdout.
119163
pub(crate) fn read(self) -> Result<String> {
164+
self.pre_run_output();
120165
let description = self.description;
121166
let mut cmd = self.cmd;
122-
if !self.quiet {
123-
println!("{description}");
124-
}
125167
tracing::debug!("exec: {cmd:?}");
126168
cmd.stdout(Stdio::piped());
127169
let child = cmd

0 commit comments

Comments
 (0)