Skip to content

Commit bfd3858

Browse files
committed
Replace remaining early exits with proper ErrorMessage returns
1 parent a94c1c3 commit bfd3858

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

src/help/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::process;
1+
use crate::ErrorMessage;
22

33
const HELP: &str = include_str!("help.txt");
44
const BUILD_HELP: &str = include_str!("build_help.txt");
@@ -31,9 +31,7 @@ pub(crate) fn test_help() {
3131
print!("{}", TEST_HELP);
3232
}
3333

34-
pub(crate) fn no_subcommand() -> ! {
35-
println!("Please invoke `bootimage` with a subcommand (e.g. `bootimage build`).");
36-
println!();
37-
println!("See `bootimage --help` for more information.");
38-
process::exit(1);
34+
pub(crate) fn no_subcommand() -> ErrorMessage {
35+
"Please invoke `bootimage` with a subcommand (e.g. `bootimage build`).\n\n\
36+
See `bootimage --help` for more information.".into()
3937
}

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ pub fn run() -> Result<Option<i32>, ErrorMessage> {
6464
Command::Run(args) => subcommand::run::run(args).map(Some),
6565
Command::Test(args) => subcommand::test::test(args).map(none),
6666
Command::Runner(args) => subcommand::runner::runner(args).map(Some),
67-
Command::NoSubcommand => help::no_subcommand(),
6867
Command::Help => Ok(help::help()).map(none),
6968
Command::BuildHelp => Ok(help::build_help()).map(none),
7069
Command::CargoBootimageHelp => Ok(help::cargo_bootimage_help()).map(none),
7170
Command::RunHelp => Ok(help::run_help()).map(none),
7271
Command::RunnerHelp => Ok(help::runner_help()).map(none),
7372
Command::TestHelp => Ok(help::test_help()).map(none),
7473
Command::Version => Ok(println!("bootimage {}", env!("CARGO_PKG_VERSION"))).map(none),
74+
Command::NoSubcommand => Err(help::no_subcommand()),
7575
}
7676
}
7777

src/subcommand/build.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub(crate) fn build_impl(
1515
args: &Args,
1616
quiet: bool,
1717
) -> Result<Vec<PathBuf>, ErrorMessage> {
18-
run_cargo_fetch(&args);
18+
run_cargo_fetch(&args).map_err(|()| "cargo fetch failed")?;
1919

2020
let executables = builder.build_kernel(&args.cargo_args, quiet)?;
2121
if executables.len() == 0 {
@@ -40,14 +40,16 @@ pub(crate) fn build_impl(
4040
Ok(bootimages)
4141
}
4242

43-
fn run_cargo_fetch(args: &Args) {
43+
fn run_cargo_fetch(args: &Args) -> Result<(), ()> {
4444
let mut command = process::Command::new("cargo");
4545
command.arg("fetch");
4646
if let Some(manifest_path) = args.manifest_path() {
4747
command.arg("--manifest-path");
4848
command.arg(manifest_path);
4949
}
50-
if !command.status().map(|s| s.success()).unwrap_or(false) {
51-
process::exit(1);
50+
if command.status().map(|s| s.success()).unwrap_or(false) {
51+
Ok(())
52+
} else {
53+
Err(())
5254
}
5355
}

src/subcommand/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub(crate) fn test(mut args: Args) -> Result<(), ErrorMessage> {
9494
for test in tests.iter().filter(|t| t.1 != TestResult::Ok) {
9595
writeln!(io::stderr(), " {}: {:?}", test.0, test.1)?;
9696
}
97-
process::exit(1);
97+
Err("Some tests failed".into())
9898
}
9999
}
100100

0 commit comments

Comments
 (0)