Skip to content

Commit

Permalink
feat(packager): always show command output, enhance formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog-crabnebula committed Apr 10, 2024
1 parent 8e54d00 commit 6a48667
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changes/enhance-shell-output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cargo-packager": patch
---

Always show command output and enhance formatting.
3 changes: 3 additions & 0 deletions crates/packager/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ where

if !cli.quite {
init_tracing_subscriber(cli.verbose);
if std::env::var_os("CARGO_TERM_COLOR").is_none() {
std::env::set_var("CARGO_TERM_COLOR", "always");
}
}

run_cli(cli)
Expand Down
46 changes: 46 additions & 0 deletions crates/packager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,55 @@ pub fn init_tracing_subscriber(verbosity: u8) {
.with_line_number(tracing)
.with_file(tracing)
.with_max_level(level)
.event_format(TracingFormatter {
formatter: tracing_subscriber::fmt::format().compact(),
})
.init();
}

struct TracingFormatter {
formatter: tracing_subscriber::fmt::format::Format<tracing_subscriber::fmt::format::Compact>,
}

struct ShellFieldVisitor {
message: String,
}

impl tracing::field::Visit for ShellFieldVisitor {
fn record_str(&mut self, field: &tracing::field::Field, value: &str) {
if field.name() == "message" {
self.message = value.to_string();
}
}

fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
if field.name() == "message" {
self.message = format!("{value:?}");
}
}
}

impl<S, N> tracing_subscriber::fmt::FormatEvent<S, N> for TracingFormatter
where
S: tracing::Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
N: for<'a> tracing_subscriber::fmt::FormatFields<'a> + 'static,
{
fn format_event(
&self,
ctx: &tracing_subscriber::fmt::FmtContext<'_, S, N>,
mut writer: tracing_subscriber::fmt::format::Writer<'_>,
event: &tracing::Event<'_>,
) -> std::fmt::Result {
if event.fields().any(|f| f.name() == "shell") {
let mut visitor = ShellFieldVisitor { message: "".into() };
event.record(&mut visitor);
writeln!(writer, "{}", visitor.message)
} else {
self.formatter.format_event(ctx, writer, event)
}
}
}

/// Sign the specified packages and return the signatures paths.
///
/// If `packages` contain a directory in the case of [`PackageFormat::App`]
Expand Down
12 changes: 10 additions & 2 deletions crates/packager/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ impl CommandExt for Command {
if let Ok(0) = stdout.read_until(b'\n', &mut buf) {
break;
}
tracing::debug!("{}", String::from_utf8_lossy(&buf[..buf.len() - 1]));
tracing::info!(
shell = "stdout",
"{}",
String::from_utf8_lossy(&buf[..buf.len() - 1])
);
lines.extend(&buf);
}
});
Expand All @@ -59,7 +63,11 @@ impl CommandExt for Command {
if let Ok(0) = stderr.read_until(b'\n', &mut buf) {
break;
}
tracing::debug!("{}", String::from_utf8_lossy(&buf[..buf.len() - 1]));
tracing::info!(
shell = "stderr",
"{}",
String::from_utf8_lossy(&buf[..buf.len() - 1])
);
lines.extend(&buf);
}
});
Expand Down

0 comments on commit 6a48667

Please sign in to comment.