Skip to content

Commit 675c8ac

Browse files
committed
Pass through exit code of run commands
1 parent 5aad6cd commit 675c8ac

File tree

5 files changed

+33
-40
lines changed

5 files changed

+33
-40
lines changed

src/bin/cargo-bootimage.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
use std::process;
2-
31
pub fn main() {
4-
if let Err(err) = bootimage::run() {
5-
eprintln!("Error: {}", err.message);
6-
process::exit(err.exit_code);
7-
}
2+
bootimage::lib_main();
83
}

src/lib.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use args::{Args, RunnerArgs, TesterArgs};
2-
use std::fmt;
2+
use std::{fmt, process};
33

44
pub mod args;
55
pub mod builder;
@@ -25,20 +25,34 @@ enum Command {
2525
Version,
2626
}
2727

28-
pub fn run() -> Result<(), ErrorString> {
28+
pub fn lib_main() {
29+
match run() {
30+
Err(err) => {
31+
eprintln!("Error: {}", err.message);
32+
process::exit(err.exit_code);
33+
}
34+
Ok(Some(exit_code)) => {
35+
process::exit(exit_code);
36+
}
37+
Ok(None) => {}
38+
}
39+
}
40+
41+
pub fn run() -> Result<Option<i32>, ErrorString> {
2942
let command = args::parse_args()?;
43+
let none = |()| None;
3044
match command {
31-
Command::Build(args) => subcommand::build::build(args),
32-
Command::Run(args) => subcommand::run::run(args),
33-
Command::Test(args) => subcommand::test::test(args),
34-
Command::Runner(args) => subcommand::runner::runner(args),
35-
Command::Tester(args) => subcommand::tester::tester(args),
45+
Command::Build(args) => subcommand::build::build(args).map(none),
46+
Command::Run(args) => subcommand::run::run(args).map(Some),
47+
Command::Test(args) => subcommand::test::test(args).map(none),
48+
Command::Runner(args) => subcommand::runner::runner(args).map(Some),
49+
Command::Tester(args) => subcommand::tester::tester(args).map(none),
3650
Command::NoSubcommand => help::no_subcommand(),
37-
Command::Help => Ok(help::help()),
38-
Command::BuildHelp => Ok(help::build_help()),
39-
Command::RunHelp => Ok(help::run_help()),
40-
Command::TestHelp => Ok(help::test_help()),
41-
Command::Version => Ok(println!("bootimage {}", env!("CARGO_PKG_VERSION"))),
51+
Command::Help => Ok(help::help()).map(none),
52+
Command::BuildHelp => Ok(help::build_help()).map(none),
53+
Command::RunHelp => Ok(help::run_help()).map(none),
54+
Command::TestHelp => Ok(help::test_help()).map(none),
55+
Command::Version => Ok(println!("bootimage {}", env!("CARGO_PKG_VERSION"))).map(none),
4256
Command::RunnerHelp | Command::TesterHelp | Command::CargoBootimageHelp => unimplemented!(),
4357
}
4458
}

src/main.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
use std::process;
2-
31
pub fn main() {
4-
if let Err(err) = bootimage::run() {
5-
eprintln!("Error: {}", err.message);
6-
process::exit(err.exit_code);
7-
}
2+
bootimage::lib_main();
83
}

src/subcommand/run.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{args::Args, builder::Builder, config, ErrorString};
22
use std::process;
33

4-
pub(crate) fn run(mut args: Args) -> Result<(), ErrorString> {
4+
pub(crate) fn run(mut args: Args) -> Result<i32, ErrorString> {
55
use crate::subcommand::build;
66

77
let builder = Builder::new(args.manifest_path().clone())?;
@@ -27,11 +27,11 @@ pub(crate) fn run(mut args: Args) -> Result<(), ErrorString> {
2727
);
2828
}
2929
command.args(&args.run_args);
30-
command.status().map_err(|err| {
30+
let exit_status = command.status().map_err(|err| {
3131
ErrorString::from(format!(
3232
"Failed to execute run command `{:?}`: {}",
3333
command, err
3434
))
3535
})?;
36-
Ok(())
36+
Ok(exit_status.code().unwrap_or(1))
3737
}

src/subcommand/runner.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{args::RunnerArgs, builder::Builder, ErrorString};
22
use std::{fs, process};
33

4-
pub(crate) fn runner(args: RunnerArgs) -> Result<(), ErrorString> {
4+
pub(crate) fn runner(args: RunnerArgs) -> Result<i32, ErrorString> {
55
let builder = Builder::new(None)?;
66

77
let bootimage_bin = {
@@ -58,16 +58,5 @@ pub(crate) fn runner(args: RunnerArgs) -> Result<(), ErrorString> {
5858
.output()
5959
.map_err(|e| format!("Failed to execute `{:?}`: {}", command, e))?;
6060

61-
if !output.status.success() {
62-
return Err(ErrorString {
63-
exit_code: output.status.code().unwrap_or(1),
64-
message: Box::new(format!(
65-
"Command `{:?}` failed:\n{}",
66-
command,
67-
String::from_utf8_lossy(&output.stderr)
68-
)),
69-
});
70-
}
71-
72-
Ok(())
61+
Ok(output.status.code().unwrap_or(1))
7362
}

0 commit comments

Comments
 (0)