From e799a7ccba4cbf12736362a6e7d47330a845fbd1 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Tue, 30 Jan 2024 15:04:37 +0100 Subject: [PATCH] httpd: Add `--path` param to `rad-web` This passes a `path` variable to the `auth_url` so consumers can open desired pages directly. Also checks the cwd to start rad web with a path corresponding to it. --- radicle-httpd/src/commands/web.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/radicle-httpd/src/commands/web.rs b/radicle-httpd/src/commands/web.rs index 513bacd4..d14654de 100644 --- a/radicle-httpd/src/commands/web.rs +++ b/radicle-httpd/src/commands/web.rs @@ -6,7 +6,7 @@ use std::time::Duration; use anyhow::{anyhow, Context}; use serde::{Deserialize, Serialize}; -use url::Url; +use url::{Position, Url}; use radicle::crypto::{PublicKey, Signature, Signer}; @@ -28,6 +28,7 @@ Options --listen, -l Address to bind the HTTP daemon to (default: 127.0.0.1:8080) --connect, -c [] Connect the explorer to an already running daemon (default: 127.0.0.1:8080) + --path, -p Path to be opened in the explorer after authentication --[no-]open Open the authentication URL automatically (default: open) --help Print help "#, @@ -44,6 +45,7 @@ pub struct SessionInfo { pub struct Options { pub app_url: Url, pub listen: SocketAddr, + pub path: Option, pub connect: Option, pub open: bool, } @@ -55,6 +57,7 @@ impl Args for Options { let mut parser = lexopt::Parser::from_args(args); let mut listen = None; let mut connect = None; + let mut path = None; // SAFETY: This is a valid URL. #[allow(clippy::unwrap_used)] let mut app_url = Url::parse("https://app.radicle.xyz").unwrap(); @@ -66,6 +69,10 @@ impl Args for Options { let val = parser.value()?; listen = Some(term::args::socket_addr(&val)?); } + Long("path") | Short('p') if path.is_none() => { + let val = parser.value()?; + path = Some(term::args::string(&val)); + } Long("connect") | Short('c') if connect.is_none() => { if let Ok(val) = parser.value() { connect = Some(term::args::socket_addr(&val)?); @@ -99,6 +106,7 @@ impl Args for Options { IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080, )), + path, connect, }, vec![], @@ -168,6 +176,21 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> { .append_pair("sig", &signature.to_string()) .append_pair("addr", &connect.to_string()); + let pathname = radicle::rad::cwd().ok().and_then(|(_, rid)| { + Url::parse( + &profile + .config + .public_explorer + .url(options.listen, rid) + .to_string(), + ) + .map(|x| x[Position::BeforePath..].to_string()) + .ok() + }); + if let Some(path) = options.path.or(pathname) { + auth_url.query_pairs_mut().append_pair("path", &path); + } + if options.open { #[cfg(target_os = "macos")] let cmd_name = "open";