cli: Add option for opening browser windows.
This commit is contained in:
parent
4fd04a4051
commit
7274c3020f
|
@ -1093,6 +1093,14 @@ name = "opaque-debug"
|
|||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "opener"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "owning_ref"
|
||||
version = "0.4.0"
|
||||
|
@ -1508,6 +1516,7 @@ dependencies = [
|
|||
"env_logger 0.5.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"fake 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"opener 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"regex 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
@ -2334,6 +2343,7 @@ dependencies = [
|
|||
"checksum num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "630de1ef5cc79d0cdd78b7e33b81f083cbfe90de0f4b2b2f07f905867c70e9fe"
|
||||
"checksum num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bcef43580c035376c0705c42792c294b66974abbfd2789b511784023f71f3273"
|
||||
"checksum opaque-debug 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "93f5bb2e8e8dec81642920ccff6b61f1eb94fa3020c5a325c9851ff604152409"
|
||||
"checksum opener 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "13117407ca9d0caf3a0e74f97b490a7e64c0ae3aa90a8b7085544d0c37b6f3ae"
|
||||
"checksum owning_ref 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "49a4b8ea2179e6a2e27411d3bca09ca6dd630821cf6894c6c7c8467a8ee7ef13"
|
||||
"checksum parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ab41b4aed082705d1056416ae4468b6ea99d52599ecf3169b00088d43113e337"
|
||||
"checksum parking_lot 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fa7767817701cce701d5585b9c4db3cdd02086398322c1d7e8bf5094a96a2ce7"
|
||||
|
|
|
@ -23,6 +23,7 @@ directories = "2.0"
|
|||
env_logger = "0.5"
|
||||
fake = "1.2"
|
||||
log = "0.4"
|
||||
opener = "0.4"
|
||||
rand = "0.5"
|
||||
regex = "1.0"
|
||||
serde = "1.0"
|
||||
|
|
|
@ -8,7 +8,7 @@ Usage:
|
|||
rookeries --version
|
||||
rookeries init [<project>] [-q | --quiet | -v | --verbose]
|
||||
rookeries build [<project>] [-q | --quiet | -v | --verbose]
|
||||
rookeries serve [<project>] [--port=<port>] [-q | --quiet | -v | --verbose]
|
||||
rookeries serve [<project>] [--port=<port>] [--open] [-q | --quiet | -v | --verbose]
|
||||
|
||||
Options:
|
||||
-h --help Show this help screen.
|
||||
|
@ -16,6 +16,7 @@ Options:
|
|||
--port=<port> The port number to serve a built site on [defaults: 8000].
|
||||
-q, --quiet No output printed to console.
|
||||
-v, --verbose Use verbose output.
|
||||
--open Open the served site in the default web browser.
|
||||
|
||||
Commands:
|
||||
init [<project>] Initialize a project in the selected directory [defaults: .].
|
||||
|
@ -37,6 +38,7 @@ pub struct Args {
|
|||
pub flag_port: u16,
|
||||
pub flag_quiet: bool,
|
||||
pub flag_verbose: bool,
|
||||
pub flag_open: bool,
|
||||
}
|
||||
|
||||
pub fn initialize_logging(args: &Args) {
|
||||
|
|
|
@ -21,6 +21,7 @@ pub enum RookeriesError {
|
|||
SiteTomlDeserializationError(toml::de::Error),
|
||||
ExtractArchiveError(zip::result::ZipError, FileType),
|
||||
UnableToAccessRookeriesHome,
|
||||
OpenBrowserFailure(opener::OpenError),
|
||||
}
|
||||
|
||||
impl fmt::Display for RookeriesError {
|
||||
|
@ -120,12 +121,20 @@ impl fmt::Display for RookeriesError {
|
|||
write!(f, "Exact cause: {}", err)
|
||||
}
|
||||
RookeriesError::ExtractArchiveError(ref err, ref file_type) => {
|
||||
writeln!(f, "Unable to extract the {} installer archive.", file_type.to_string().red())?;
|
||||
writeln!(
|
||||
f,
|
||||
"Unable to extract the {} installer archive.",
|
||||
file_type.to_string().red()
|
||||
)?;
|
||||
write!(f, "Exact cause: {}", err)
|
||||
}
|
||||
RookeriesError::UnableToAccessRookeriesHome => {
|
||||
write!(f, "Unable to access ROOKERIES_HOME.")
|
||||
}
|
||||
RookeriesError::OpenBrowserFailure(ref err) => {
|
||||
writeln!(f, "Could not open the locally served site in a browser.")?;
|
||||
write!(f, "Exact cause: {}", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -149,6 +158,7 @@ impl error::Error for RookeriesError {
|
|||
RookeriesError::SiteTomlDeserializationError(ref err) => Some(err),
|
||||
RookeriesError::ExtractArchiveError(ref err, _) => Some(err),
|
||||
RookeriesError::UnableToAccessRookeriesHome => None,
|
||||
RookeriesError::OpenBrowserFailure(ref err) => Some(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
22
src/main.rs
22
src/main.rs
|
@ -9,6 +9,7 @@ extern crate actix_web;
|
|||
extern crate colored;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate opener;
|
||||
extern crate tera;
|
||||
extern crate toml;
|
||||
|
||||
|
@ -40,6 +41,7 @@ use std::{
|
|||
fs::{read_dir, read_to_string, remove_dir_all, write},
|
||||
path::{Path, PathBuf},
|
||||
process::Command,
|
||||
thread::spawn,
|
||||
};
|
||||
|
||||
const ROOKERIES_UNKNOWN_PLACEHOLDER: &str = "???";
|
||||
|
@ -158,12 +160,32 @@ fn serve_site(args: &Args) -> Result {
|
|||
"Serving project at local port {}",
|
||||
port.to_string().green()
|
||||
));
|
||||
if args.flag_open {
|
||||
let success_port = port.clone();
|
||||
spawn(move || open_browser_tab(success_port));
|
||||
}
|
||||
Ok(res)
|
||||
})?
|
||||
.run()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn open_browser_tab(port: u16) {
|
||||
let url_to_open = format!("http://localhost:{}/", port);
|
||||
match opener::open(&url_to_open).map_err(|err| RookeriesError::OpenBrowserFailure(err)) {
|
||||
Ok(_) => success_message(&format!(
|
||||
"Opened {} in the default web browser.",
|
||||
&url_to_open.green()
|
||||
)),
|
||||
// TODO: Make a nicer error setup after refactoring fatal_error.
|
||||
Err(err) => caution_message(&format!(
|
||||
"Failed to open {} in the default web browser. \n Cause:{}",
|
||||
&url_to_open.yellow(),
|
||||
err,
|
||||
)),
|
||||
};
|
||||
}
|
||||
|
||||
fn build_site(args: &Args) -> Result {
|
||||
header_message("Building the site...");
|
||||
let project_directory = get_project_directory(&args)?;
|
||||
|
|
Loading…
Reference in New Issue