Skip to content

Commit

Permalink
Always show command output for oneliner interface
Browse files Browse the repository at this point in the history
The one-liner interface is more likely to be used for ad-hoc commands.
So showing the output of the command is likely useful.
  • Loading branch information
danobi committed May 12, 2023
1 parent 19efcd9 commit 91f828a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,21 @@ fn config(args: &Args) -> Result<Vmtest> {
}
}

/// Whether or not to collapse command output in UI.
///
/// This is useful for one-liner invocations.
fn show_cmd(args: &Args) -> bool {
args.config.is_none()
}

fn main() -> Result<()> {
let args = Args::parse();

env_logger::init();
let vmtest = config(&args)?;
let filter = Regex::new(&args.filter).context("Failed to compile regex")?;
let ui = Ui::new(vmtest);
let failed = ui.run(&filter);
let failed = ui.run(&filter, show_cmd(&args));
let rc = i32::from(failed != 0);

exit(rc);
Expand Down
45 changes: 27 additions & 18 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl Ui {
/// UI for a single target. Must be run on its own thread.
///
/// Returns if the target was successful or not>
fn target_ui(updates: Receiver<Output>, target: String) -> bool {
fn target_ui(updates: Receiver<Output>, target: String, show_cmd: bool) -> bool {
let term = Term::stdout();
let mut stage = Stage::new(term.clone(), &heading(&target, 1), None);
let mut stages = 0;
Expand Down Expand Up @@ -201,32 +201,38 @@ impl Ui {
stages += 1;
}
Output::Command(s) => stage.print_line(s, None),
Output::CommandEnd(r) => match r {
Ok(retval) => {
if *retval != 0 {
error_out_stage(
&mut stage,
&anyhow!("Command failed with exit code: {}", retval),
);
Output::CommandEnd(r) => {
if show_cmd {
stage.expand(true);
}

match r {
Ok(retval) => {
if *retval != 0 {
error_out_stage(
&mut stage,
&anyhow!("Command failed with exit code: {}", retval),
);
errors += 1;
}
}
Err(e) => {
error_out_stage(&mut stage, e);
errors += 1;
}
}
Err(e) => {
error_out_stage(&mut stage, e);
errors += 1;
}
},
};
}
}
}

// Force stage cleanup so we can do final fixup if we want
drop(stage);

// Only clear target stages if target was successful
if errors == 0 {
if errors == 0 && !show_cmd {
clear_last_lines(&term, stages);
term.write_line("PASS").expect("Failed to write terminal");
} else {
} else if !show_cmd {
term.write_line("FAILED").expect("Failed to write terminal");
}

Expand All @@ -235,10 +241,13 @@ impl Ui {

/// Run all the targets in the provided `vmtest`
///
/// `filter` specifies the regex to filter targets by.
/// `show_cmd` specifies if the command output should always be shown.
///
/// Note this function is "infallible" b/c on error it will display
/// the appropriate error message to screen. Rather, it returns how
/// many targets failed.
pub fn run(self, filter: &Regex) -> usize {
pub fn run(self, filter: &Regex, show_cmd: bool) -> usize {
let mut failed = 0;
for (idx, target) in self
.vmtest
Expand All @@ -251,7 +260,7 @@ impl Ui {

// Start UI on its own thread b/c `Vmtest::run_one()` will block
let name = target.name.clone();
let ui = thread::spawn(move || Self::target_ui(receiver, name));
let ui = thread::spawn(move || Self::target_ui(receiver, name, show_cmd));

// Run a target
self.vmtest.run_one(idx, sender);
Expand Down

0 comments on commit 91f828a

Please sign in to comment.