Skip to content

Commit b20ffa4

Browse files
Max Huang-HobbsMax Huang-Hobbs
authored andcommitted
feat: add --hide-cached
Adds --hide-cached to the `exec` family of commands. This is used to configure the MoonReporter's behaviour for a given command run. At present the only way to configure task reporting is with the individual task's reporting settings. This change adds a new mechanism to configure the reporter behaviour from command line flags. A "clean" separation of concerns might complicate this by making a separate MoonTaskReporter that is aware of tasks, and initialize it in each of the exec-family commands. However, because the MoonReporter is aware of tasks and configured before subcommand execution, we need to read the CLI commands outside of the individual subcommands in order to initialize it before descending into subcommand execution. I'm not 100% happy with this design, but I tried plumbing overrides through TaskReportItem, and I didn't like that design because it conflated transient, global CLI config with persistent config from individual task configs. I think this is a reasonable compromise that doesn't involve rewriting the MoonReporter to be more "pure" and harder to maintain. Closes #1930
1 parent d769fca commit b20ffa4

4 files changed

Lines changed: 56 additions & 7 deletions

File tree

crates/app-macros/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ pub fn with_shared_exec_args(attr: TokenStream, item: TokenStream) -> TokenStrea
4848
)]
4949
pub summary: Option<Option<crate::app_options::SummaryOption>>
5050
},
51+
quote! {
52+
#[arg(
53+
long,
54+
short = 'x',
55+
global = true,
56+
env = "MOON_HIDE_CACHED",
57+
help = "Hide all stdout/stderr from succesful cached tasks",
58+
help_heading = super::HEADING_WORKFLOW,
59+
)]
60+
pub hide_cached: bool
61+
},
5162
// GRAPH
5263
quote! {
5364
#[arg(

crates/app/src/app.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,28 @@ pub struct Cli {
351351
}
352352

353353
impl Cli {
354+
pub fn get_reporter_config(&self) -> moon_console::MoonReporterConfig {
355+
// The way the reporter is designed makes this awkward introspection
356+
// into the CLI args necessary.
357+
//
358+
// The traditional approach would be to configure the MoonReporter with
359+
// the individual commands directly, but because the reporter is aware of
360+
// task execution, and is created as shared immutable before we start
361+
// subcommand execution, we have to introspect into the CLI args to extract
362+
// the MoonReporter's config from the CLI args.
363+
let hide_cached = match &self.command {
364+
Commands::Check(args) => args.hide_cached,
365+
Commands::Ci(args) => args.hide_cached,
366+
Commands::Exec(args) => args.hide_cached,
367+
Commands::Run(args) => args.hide_cached,
368+
_ => false,
369+
};
370+
371+
moon_console::MoonReporterConfig {
372+
hide_cached: !hide_cached,
373+
}
374+
}
375+
354376
pub fn setup_env_vars(&self) {
355377
bootstrap::setup_colors(self.color);
356378

crates/app/src/session.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ impl MoonSession {
279279
impl AppSession for MoonSession {
280280
/// Setup initial state for the session. Order is very important!!!
281281
async fn startup(&mut self) -> AppResult {
282-
self.console.set_reporter(MoonReporter::default());
282+
self.console
283+
.set_reporter(MoonReporter::new(self.cli.get_reporter_config()));
283284
self.console.set_theme(create_console_theme());
284285

285286
// Determine paths

crates/console/src/reporter.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,30 +76,42 @@ fn bold(message: &str) -> String {
7676
}
7777
}
7878

79+
#[derive(Clone, Debug, Default)]
80+
pub struct MoonReporterConfig {
81+
pub hide_cached: bool,
82+
}
83+
7984
#[derive(Debug)]
8085
pub struct MoonReporter {
8186
err: ConsoleStream,
8287
out: ConsoleStream,
88+
config: MoonReporterConfig,
8389
test_mode: bool,
8490
}
8591

8692
impl MoonReporter {
93+
pub fn new(settings: MoonReporterConfig) -> Self {
94+
Self {
95+
err: ConsoleStream::empty(ConsoleStreamType::Stderr),
96+
out: ConsoleStream::empty(ConsoleStreamType::Stdout),
97+
config: settings,
98+
test_mode: false,
99+
}
100+
}
101+
87102
pub fn new_testing() -> Self {
88103
Self {
89104
err: ConsoleStream::new_testing(ConsoleStreamType::Stderr),
90105
out: ConsoleStream::new_testing(ConsoleStreamType::Stdout),
106+
config: MoonReporterConfig::default(),
91107
test_mode: true,
92108
}
93109
}
94110
}
95111

96112
impl Default for MoonReporter {
97113
fn default() -> Self {
98-
Self {
99-
err: ConsoleStream::empty(ConsoleStreamType::Stderr),
100-
out: ConsoleStream::empty(ConsoleStreamType::Stdout),
101-
test_mode: false,
102-
}
114+
Self::new(MoonReporterConfig::default())
103115
}
104116
}
105117

@@ -653,7 +665,10 @@ impl MoonReporter {
653665
if let Some(operation) = operations.get_last_process() {
654666
// If cached, the finished event above is not fired,
655667
// so handle printing the captured logs here!
656-
if operation.is_cached() && operation.has_output() {
668+
if operation.is_cached()
669+
&& operation.has_output()
670+
&& (self.config.hide_cached || operation.has_failed())
671+
{
657672
self.print_operation_output(operation, item)?;
658673
}
659674

0 commit comments

Comments
 (0)