Skip to content

Commit

Permalink
support quiet mode execution for fud2 (#2339)
Browse files Browse the repository at this point in the history
  • Loading branch information
rachitnigam authored Nov 6, 2024
1 parent 6b48969 commit 351af02
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
20 changes: 12 additions & 8 deletions fud2/fud-core/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub enum Subcommand<T: CliExt> {

#[derive(FromArgs)]
/// A generic compiler driver.
pub struct FakeArgs<T: CliExt> {
pub struct FudArgs<T: CliExt> {
#[argh(subcommand)]
pub sub: Option<Subcommand<T>>,

Expand Down Expand Up @@ -190,6 +190,10 @@ pub struct FakeArgs<T: CliExt> {
#[argh(switch, short = 'v')]
verbose: Option<bool>,

/// quiet mode
#[argh(switch, short = 'q')]
quiet: bool,

/// log level for debugging fud internal
#[argh(option, long = "log", default = "log::LevelFilter::Warn")]
pub log_level: log::LevelFilter,
Expand Down Expand Up @@ -229,7 +233,7 @@ fn get_states_with_errors(

fn from_states<T: CliExt>(
driver: &Driver,
args: &FakeArgs<T>,
args: &FudArgs<T>,
) -> anyhow::Result<Vec<StateRef>> {
get_states_with_errors(
driver,
Expand All @@ -243,7 +247,7 @@ fn from_states<T: CliExt>(

fn to_state<T: CliExt>(
driver: &Driver,
args: &FakeArgs<T>,
args: &FudArgs<T>,
) -> anyhow::Result<Vec<StateRef>> {
get_states_with_errors(
driver,
Expand All @@ -257,7 +261,7 @@ fn to_state<T: CliExt>(

fn get_request<T: CliExt>(
driver: &Driver,
args: &FakeArgs<T>,
args: &FudArgs<T>,
) -> anyhow::Result<Request> {
// The default working directory (if not specified) depends on the mode.
let workdir = args.dir.clone().unwrap_or_else(|| match args.mode {
Expand Down Expand Up @@ -407,7 +411,7 @@ impl<T: CliExt> CliStart<T> for T {
fn config_from_cli_ext<T: CliExt>(
name: &str,
) -> anyhow::Result<figment::Figment> {
let args: FakeArgs<T> = argh::from_env();
let args: FudArgs<T> = argh::from_env();
let mut config = config::load_config(name);

// Use `--set` arguments to override configuration values.
Expand All @@ -428,7 +432,7 @@ fn cli_ext<T: CliExt>(
driver: &Driver,
config: &figment::Figment,
) -> anyhow::Result<()> {
let args: FakeArgs<T> = argh::from_env();
let args: FudArgs<T> = argh::from_env();
// Configure logging.
env_logger::Builder::new()
.format_timestamp(None)
Expand Down Expand Up @@ -479,8 +483,8 @@ fn cli_ext<T: CliExt>(
Mode::ShowDot => run.show_dot(),
Mode::EmitNinja => run.emit_to_stdout()?,
Mode::Generate => run.emit_to_dir(&workdir)?.keep(),
Mode::Run => run.emit_and_run(&workdir, false)?,
Mode::Cmds => run.emit_and_run(&workdir, true)?,
Mode::Run => run.emit_and_run(&workdir, false, args.quiet)?,
Mode::Cmds => run.emit_and_run(&workdir, true, false)?,
}

Ok(())
Expand Down
13 changes: 11 additions & 2 deletions fud2/fud-core/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,12 @@ impl<'a> Run<'a> {
/// Emit `build.ninja` to a temporary directory and then actually execute Ninja.
///
/// If `print_cmds` is true, Ninja will print commands it is to run instead of executing them.
pub fn emit_and_run(&self, dir: &Utf8Path, print_cmds: bool) -> EmitResult {
pub fn emit_and_run(
&self,
dir: &Utf8Path,
print_cmds: bool,
quiet_mode: bool,
) -> EmitResult {
// Emit the Ninja file.
let dir = self.emit_to_dir(dir)?;

Expand Down Expand Up @@ -375,7 +380,11 @@ impl<'a> Run<'a> {
cmd.arg("-tcommands");
}

cmd.stdout(std::io::stderr()); // Send Ninja's stdout to our stderr.
if !quiet_mode {
cmd.stdout(std::io::stderr()); // Send Ninja's stdout to our stderr.
} else {
cmd.stdout(std::process::Stdio::null());
}
let status = cmd.status().map_err(ninja_cmd_io_error)?;

// Emit to stdout, only when Ninja succeeded.
Expand Down

0 comments on commit 351af02

Please sign in to comment.