Skip to content

Commit

Permalink
allow cmd exec quiet
Browse files Browse the repository at this point in the history
Make it possible in Twoliter to exec an external command quietly.
Previously this looked at the log level filter to determine whether or
not to send the command output to stdout/stderr. Now there is a form
of the exec command that allows you to specify whether or not to send
the output to stdout/stderr.
  • Loading branch information
webern committed Oct 9, 2023
1 parent c7af698 commit f84cdc0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 32 deletions.
4 changes: 2 additions & 2 deletions twoliter/src/cmd/make.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::exec;
use crate::common::exec_log;
use crate::docker::ImageArchUri;
use crate::project;
use crate::project::Project;
Expand Down Expand Up @@ -87,7 +87,7 @@ impl Make {
args.push(cargo_make_arg.clone());
}

exec(Command::new("cargo").args(args)).await
exec_log(Command::new("cargo").args(args)).await
}
}

Expand Down
63 changes: 35 additions & 28 deletions twoliter/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,45 @@ use log::{self, debug, LevelFilter};
use tokio::process::Command;

/// Run a `tokio::process::Command` and return a `Result` letting us know whether or not it worked.
pub(crate) async fn exec(cmd: &mut Command) -> Result<()> {
debug!("Running: {:?}", cmd);
/// Pipes stdout/stderr when logging `LevelFilter` is more verbose than `Warn`.
pub(crate) async fn exec_log(cmd: &mut Command) -> Result<()> {
let quiet = matches!(
log::max_level(),
LevelFilter::Off | LevelFilter::Error | LevelFilter::Warn
);
exec(cmd, quiet).await
}

match log::max_level() {
/// Run a `tokio::process::Command` and return a `Result` letting us know whether or not it worked.
/// `quiet` determines whether or not the command output will be piped to `stdout/stderr`. When
/// `quiet=true`, no output will be shown.
pub(crate) async fn exec(cmd: &mut Command, quiet: bool) -> Result<()> {
debug!("Running: {:?}", cmd);
if quiet {
// For quiet levels of logging we capture stdout and stderr
LevelFilter::Off | LevelFilter::Error | LevelFilter::Warn => {
let output = cmd
.output()
.await
.context(format!("Unable to start command"))?;
ensure!(
output.status.success(),
"Command was unsuccessful, exit code {}:\n{}\n{}",
output.status.code().unwrap_or(1),
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
}

let output = cmd
.output()
.await
.context(format!("Unable to start command"))?;
ensure!(
output.status.success(),
"Command was unsuccessful, exit code {}:\n{}\n{}",
output.status.code().unwrap_or(1),
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
} else {
// For less quiet log levels we stream to stdout and stderr.
LevelFilter::Info | LevelFilter::Debug | LevelFilter::Trace => {
let status = cmd
.status()
.await
.context(format!("Unable to start command"))?;
let status = cmd
.status()
.await
.context(format!("Unable to start command"))?;

ensure!(
status.success(),
"Command was unsuccessful, exit code {}",
status.code().unwrap_or(1),
);
}
ensure!(
status.success(),
"Command was unsuccessful, exit code {}",
status.code().unwrap_or(1),
);
}
Ok(())
}
4 changes: 2 additions & 2 deletions twoliter/src/docker/commands.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::exec;
use crate::common::exec_log;
use crate::docker::ImageUri;
use anyhow::Result;
use std::collections::HashMap;
Expand Down Expand Up @@ -84,7 +84,7 @@ impl DockerBuild {
.map(|(k, v)| format!("--build-arg={}={}", k, v)),
);
args.push(self.context_dir.display().to_string());
exec(
exec_log(
Command::new("docker")
.args(args)
.env("DOCKER_BUILDKIT", "1"),
Expand Down

0 comments on commit f84cdc0

Please sign in to comment.