Add --open cli flag

This commit is contained in:
Joscha 2023-08-10 21:10:07 +02:00
parent 6ed6ff1a36
commit b5bdd49d9c
4 changed files with 57 additions and 1 deletions

37
Cargo.lock generated
View file

@ -1759,6 +1759,15 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "is-docker"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "928bae27f42bc99b60d9ac7334e3a21d10ad8f1835a4e12ec3ec0464765ed1b3"
dependencies = [
"once_cell",
]
[[package]] [[package]]
name = "is-terminal" name = "is-terminal"
version = "0.4.9" version = "0.4.9"
@ -1770,6 +1779,16 @@ dependencies = [
"windows-sys", "windows-sys",
] ]
[[package]]
name = "is-wsl"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "173609498df190136aa7dea1a91db051746d339e18476eed5ca40521f02d7aa5"
dependencies = [
"is-docker",
"once_cell",
]
[[package]] [[package]]
name = "itertools" name = "itertools"
version = "0.10.5" version = "0.10.5"
@ -2041,6 +2060,17 @@ version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]]
name = "open"
version = "5.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfabf1927dce4d6fdf563d63328a0a506101ced3ec780ca2135747336c98cef8"
dependencies = [
"is-wsl",
"libc",
"pathdiff",
]
[[package]] [[package]]
name = "option-ext" name = "option-ext"
version = "0.2.0" version = "0.2.0"
@ -2082,6 +2112,12 @@ version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c"
[[package]]
name = "pathdiff"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd"
[[package]] [[package]]
name = "pem-rfc7468" name = "pem-rfc7468"
version = "0.7.0" version = "0.7.0"
@ -2856,6 +2892,7 @@ dependencies = [
"humantime", "humantime",
"humantime-serde", "humantime-serde",
"mime_guess", "mime_guess",
"open",
"rand", "rand",
"rust-embed", "rust-embed",
"serde", "serde",

View file

@ -15,6 +15,7 @@ gethostname = "0.4.3"
humantime = "2.1.0" humantime = "2.1.0"
humantime-serde = "1.1.1" humantime-serde = "1.1.1"
mime_guess = "2.0.4" mime_guess = "2.0.4"
open = "5.0.0"
rand = "0.8.5" rand = "0.8.5"
rust-embed = { version = "6.8.1", features = ["interpolate-folder-path"] } rust-embed = { version = "6.8.1", features = ["interpolate-folder-path"] }
serde = { version = "1.0.181", features = ["derive"] } serde = { version = "1.0.181", features = ["derive"] }

View file

@ -14,6 +14,10 @@ pub struct ServerCommand {
/// Path to the bench repo. /// Path to the bench repo.
#[arg(long, short)] #[arg(long, short)]
pub bench_repo: Option<PathBuf>, pub bench_repo: Option<PathBuf>,
/// Open the UI in your browser.
#[arg(long, short)]
pub open: bool,
} }
#[derive(Debug, clap::Parser)] #[derive(Debug, clap::Parser)]

View file

@ -6,7 +6,7 @@ mod server;
mod shared; mod shared;
mod somehow; mod somehow;
use std::{io, process}; use std::{io, process, time::Duration};
use clap::Parser; use clap::Parser;
use tokio::{select, signal::unix::SignalKind}; use tokio::{select, signal::unix::SignalKind};
@ -82,6 +82,16 @@ async fn die_on_signal() -> io::Result<()> {
process::exit(1); process::exit(1);
} }
async fn open_in_browser(config: &Config) {
// Wait a bit to ensure the server is ready to serve requests.
tokio::time::sleep(Duration::from_millis(100)).await;
let url = format!("http://{}{}", config.web_address, config.web_base);
if let Err(e) = open::that_detached(&url) {
error!("Error opening {url} in browser: {e:?}");
}
}
async fn run() -> somehow::Result<()> { async fn run() -> somehow::Result<()> {
let args = Args::parse(); let args = Args::parse();
@ -92,6 +102,10 @@ async fn run() -> somehow::Result<()> {
match args.command { match args.command {
Command::Server(command) => { Command::Server(command) => {
if command.open {
tokio::task::spawn(open_in_browser(config));
}
let server = Server::new(config, command).await?; let server = Server::new(config, command).await?;
select! { select! {