Skip to content

Commit

Permalink
Merge pull request #32 from apogeeoak/exit_status
Browse files Browse the repository at this point in the history
Handle unsuccessful exit status in main.rs.
  • Loading branch information
Byron committed Jul 16, 2021
2 parents ac09da1 + 215227a commit 81d8c40
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions src/main.rs
@@ -1,28 +1,30 @@
extern crate open;

use std::{
env,
io::{stderr, Write},
process,
};
use std::{env, process};

fn main() {
let path_or_url = match env::args().nth(1) {
Some(arg) => arg,
None => {
writeln!(stderr(), "usage: open <path-or-url>").ok();
eprintln!("usage: open <path-or-url>");
process::exit(1);
}
};

if let Err(err) = open::that(&path_or_url) {
writeln!(
stderr(),
"An error occourred when opening '{}': {}",
path_or_url,
err
)
.ok();
process::exit(3);
match open::that(&path_or_url) {
Ok(status) if status.success() => (),
Ok(status) => match status.code() {
Some(code) => {
print_error_and_exit(code, &path_or_url, &format!("error code: {}", code))
}
None => print_error_and_exit(3, &path_or_url, "error unknown"),
},
Err(err) => print_error_and_exit(3, &path_or_url, &err.to_string()),
}
}

fn print_error_and_exit(code: i32, path: &str, error_message: &str) -> ! {
eprintln!(
"An error occurred when opening '{}': {}",
path, error_message
);
process::exit(code);
}

0 comments on commit 81d8c40

Please sign in to comment.