From a5218f997b54c3bd42a9fb313eea0770802b044f Mon Sep 17 00:00:00 2001 From: g Date: Sat, 21 Dec 2024 12:02:42 +0100 Subject: [PATCH] Now is possible to pass ws url as env variable OBS_WEBSOCKET_URL=obsws://localhost:4455/secret obs-cmd info fix: #52 --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 1 + src/main.rs | 15 +++++++++++++-- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d92760a..c491f7e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -586,7 +586,7 @@ dependencies = [ [[package]] name = "obs-cmd" -version = "0.17.8" +version = "0.18.1" dependencies = [ "clap", "obws", diff --git a/Cargo.toml b/Cargo.toml index 375a4c6..a801105 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "obs-cmd" -version = "0.17.8" +version = "0.18.1" edition = "2021" description = "A minimal command to control obs via obs-websocket" authors = ["Luigi Maselli "] diff --git a/README.md b/README.md index 18c0400..cee5756 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ obs-cmd replay status obs-cmd replay last-replay obs-cmd info obs-cmd --websocket obsws://localhost:4455/secret info # You can override the default `obsws` url +OBS_WEBSOCKET_URL=obsws://localhost:4455/secret obs-cmd info ``` You can override the websocket URL, which can be found in OBS -> Tools -> WebSocket Server Settings. `localhost` for the hostname will work for most, instead of the full IP address. If you set the password as `secret` you can avoid to specify the `--websocket` argument. diff --git a/src/main.rs b/src/main.rs index 5693b0e..5727d92 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,14 +11,25 @@ use obws::requests::sources::SaveScreenshot; async fn main() -> Result<(), Box> { let cli = Cli::parse(); - let client = match cli.websocket { +let client = match std::env::var("OBS_WEBSOCKET_URL") { + Ok(url) => { + let parsed_url = url::Url::parse(&url).expect("Invalid OBS_WEBSOCKET_URL format"); + let hostname = parsed_url.host_str().expect("Hostname not found in OBS_WEBSOCKET_URL").to_string(); + let port = parsed_url.port().expect("Port not found in OBS_WEBSOCKET_URL"); + let password = parsed_url.path_segments().and_then(|mut segments| segments.next()).ok_or(url::ParseError::RelativeUrlWithoutBase)?; + + // let password = parsed_url.password().map(|p| p.to_string()); + Client::connect(hostname, port, Some(password)).await? + }, + Err(_) => match cli.websocket { Some(ObsWebsocket { hostname, port, password, }) => Client::connect(hostname, port, password).await?, None => Client::connect("localhost", 4455, Some("secret")).await?, - }; + }, +}; match &cli.command { Commands::Scene(action) => {