Skip to content

Commit

Permalink
Handle unsuccessful exit status.
Browse files Browse the repository at this point in the history
  • Loading branch information
apogeeoak committed Jul 15, 2021
1 parent ac09da1 commit d2d35af
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
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) => open_error(code, &path_or_url, &format!("error code: {}", code)),
None => open_error(3, &path_or_url, "error unknown"),
},
Err(err) => open_error(3, &path_or_url, &err.to_string()),
}
}

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

0 comments on commit d2d35af

Please sign in to comment.