From 48d0708aa6bb9af7ec4d8091727a36b69bb2cf93 Mon Sep 17 00:00:00 2001 From: Brian Bowman Date: Wed, 8 Aug 2018 23:37:56 -0500 Subject: [PATCH] Handle opening browser with `opener` crate Fixes #5701 --- Cargo.toml | 1 + src/cargo/lib.rs | 1 + src/cargo/ops/cargo_doc.rs | 53 +++++--------------------------------- 3 files changed, 9 insertions(+), 46 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0fe5da513..a3bfbc240 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,7 @@ libc = "0.2" log = "0.4" libgit2-sys = "0.7.5" num_cpus = "1.0" +opener = "0.2.0" rustfix = "0.4.2" same-file = "1" semver = { version = "0.9.0", features = ["serde"] } diff --git a/src/cargo/lib.rs b/src/cargo/lib.rs index 36f64c949..1f35f2d8d 100644 --- a/src/cargo/lib.rs +++ b/src/cargo/lib.rs @@ -42,6 +42,7 @@ extern crate libgit2_sys; #[macro_use] extern crate log; extern crate num_cpus; +extern crate opener; extern crate rustfix; extern crate same_file; extern crate semver; diff --git a/src/cargo/ops/cargo_doc.rs b/src/cargo/ops/cargo_doc.rs index 33c759d03..e12784df9 100644 --- a/src/cargo/ops/cargo_doc.rs +++ b/src/cargo/ops/cargo_doc.rs @@ -1,7 +1,9 @@ use std::collections::HashMap; use std::fs; use std::path::Path; -use std::process::Command; + +use failure::Fail; +use opener; use core::Workspace; use ops; @@ -97,13 +99,10 @@ pub fn doc(ws: &Workspace, options: &DocOptions) -> CargoResult<()> { if fs::metadata(&path).is_ok() { let mut shell = options.compile_opts.config.shell(); shell.status("Opening", path.display())?; - match open_docs(&path) { - Ok(m) => shell.status("Launching", m)?, - Err(e) => { - shell.warn("warning: could not determine a browser to open docs with, tried:")?; - for method in e { - shell.warn(format!("\t{}", method))?; - } + if let Err(e) = opener::open(&path) { + shell.warn(format!("Couldn't open docs: {}", e))?; + for cause in (&e as &Fail).iter_chain() { + shell.warn(format!("Caused by:\n {}", cause))?; } } } @@ -111,41 +110,3 @@ pub fn doc(ws: &Workspace, options: &DocOptions) -> CargoResult<()> { Ok(()) } - -#[cfg(not(any(target_os = "windows", target_os = "macos")))] -fn open_docs(path: &Path) -> Result<&'static str, Vec<&'static str>> { - use std::env; - let mut methods = Vec::new(); - // trying $BROWSER - if let Ok(name) = env::var("BROWSER") { - match Command::new(name).arg(path).status() { - Ok(_) => return Ok("$BROWSER"), - Err(_) => methods.push("$BROWSER"), - } - } - - for m in ["xdg-open", "gnome-open", "kde-open"].iter() { - match Command::new(m).arg(path).status() { - Ok(_) => return Ok(m), - Err(_) => methods.push(m), - } - } - - Err(methods) -} - -#[cfg(target_os = "windows")] -fn open_docs(path: &Path) -> Result<&'static str, Vec<&'static str>> { - match Command::new("cmd").arg("/C").arg(path).status() { - Ok(_) => Ok("cmd /C"), - Err(_) => Err(vec!["cmd /C"]), - } -} - -#[cfg(target_os = "macos")] -fn open_docs(path: &Path) -> Result<&'static str, Vec<&'static str>> { - match Command::new("open").arg(path).status() { - Ok(_) => Ok("open"), - Err(_) => Err(vec!["open"]), - } -}