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

View file

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

View file

@ -6,7 +6,7 @@ mod server;
mod shared;
mod somehow;
use std::{io, process};
use std::{io, process, time::Duration};
use clap::Parser;
use tokio::{select, signal::unix::SignalKind};
@ -82,6 +82,16 @@ async fn die_on_signal() -> io::Result<()> {
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<()> {
let args = Args::parse();
@ -92,6 +102,10 @@ async fn run() -> somehow::Result<()> {
match args.command {
Command::Server(command) => {
if command.open {
tokio::task::spawn(open_in_browser(config));
}
let server = Server::new(config, command).await?;
select! {