Skip to content

Commit

Permalink
Merge pull request #101 from webern/exec
Browse files Browse the repository at this point in the history
allow cmd exec quiet
  • Loading branch information
webern authored Oct 9, 2023
2 parents 652f788 + f84cdc0 commit 414cc84
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 414cc84

Please sign in to comment.