From 03bea24f9de698375033af92a08762446d0e20cc Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Sun, 25 Feb 2024 16:14:56 +0100 Subject: Closes #2. Config and setup stuff --- src/config.rs | 30 +++++++------- src/error.rs | 10 ++--- src/main.rs | 84 +++++++++++++++++++++------------------- src/requests.rs | 2 + src/requests/device.rs | 62 ++++++++++++++--------------- src/requests/mod.rs | 2 - src/requests/start.rs | 103 ++++++++++++++++++++++++++++--------------------- 7 files changed, 158 insertions(+), 135 deletions(-) create mode 100644 src/requests.rs delete mode 100644 src/requests/mod.rs (limited to 'src') diff --git a/src/config.rs b/src/config.rs index 9a9e44b..78795a3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,19 +1,19 @@ -use config::Config; -use once_cell::sync::Lazy; +use serde::Deserialize; -pub static SETTINGS: Lazy = Lazy::new(setup); - -fn setup() -> Config { - #[cfg(not(debug_assertions))] - let builder = Config::builder().add_source(config::File::with_name( - format!("{}/webol-cli.toml", dirs::config_dir().unwrap().to_string_lossy()).as_str(), - )); +#[derive(Deserialize)] +pub struct Config { + pub apikey: String, + pub server: String, +} - #[cfg(debug_assertions)] - let builder = Config::builder().add_source(config::File::with_name("webol-cli.toml")); +impl Config { + pub fn load() -> Result { + let builder = config::Config::builder() + .add_source(config::File::with_name("~/.config/webol-cli.toml")) + .add_source(config::File::with_name("webol-cli.toml")) + .add_source(config::Environment::with_prefix("WEBOL_CLI_").separator("_")) + .build()?; - builder - .add_source(config::Environment::with_prefix("WEBOL_CLI_").separator("_")) - .build() - .unwrap() + builder.try_deserialize() + } } diff --git a/src/error.rs b/src/error.rs index f15c60a..531528f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -11,11 +11,11 @@ pub enum CliError { impl Debug for CliError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::Reqwest(err) => { err.fmt(f) }, - Self::Config(err) => { err.fmt(f) }, - Self::Serde(err) => { err.fmt(f) }, - Self::Parse(err) => { err.fmt(f) }, - Self::WsResponse => { f.write_str("Error in Response") }, + Self::Reqwest(err) => err.fmt(f), + Self::Config(err) => err.fmt(f), + Self::Serde(err) => err.fmt(f), + Self::Parse(err) => err.fmt(f), + Self::WsResponse => f.write_str("Error in Response"), } } } diff --git a/src/main.rs b/src/main.rs index afe6fac..0393183 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,11 @@ use std::{fmt::Display, time::Duration}; -use clap::{Parser, Command, CommandFactory, Subcommand}; -use clap_complete::{generate, Shell, Generator}; -use config::SETTINGS; +use crate::config::Config; +use clap::{Command, CommandFactory, Parser, Subcommand}; +use clap_complete::{generate, Generator, Shell}; use error::CliError; -use indicatif::{ProgressBar, ProgressStyle, MultiProgress}; -use requests::{start::start, device}; +use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; +use requests::{device, start::start}; use reqwest::header::{HeaderMap, HeaderValue}; use serde::Deserialize; @@ -35,7 +35,7 @@ enum Commands { /// id of the device id: String, #[arg(short, long)] - ping: Option + ping: Option, }, Device { #[command(subcommand)] @@ -52,7 +52,7 @@ enum DeviceCmd { id: String, mac: String, broadcast_addr: String, - ip: String + ip: String, }, Get { id: String, @@ -61,29 +61,39 @@ enum DeviceCmd { id: String, mac: String, broadcast_addr: String, - ip: String + ip: String, }, } #[tokio::main] async fn main() -> Result<(), CliError> { + let config = Config::load().map_err(CliError::Config)?; + let cli = Args::parse(); match cli.commands { Commands::Start { id, ping } => { - start(id, ping.unwrap_or(true)).await?; - }, - Commands::Device { devicecmd } => { - match devicecmd { - DeviceCmd::Add { id, mac, broadcast_addr, ip } => { - device::put(id, mac, broadcast_addr, ip).await?; - }, - DeviceCmd::Get { id } => { - device::get(id).await?; - }, - DeviceCmd::Edit { id, mac, broadcast_addr, ip } => { - device::post(id, mac, broadcast_addr, ip).await?; - }, + start(&config, id, ping.unwrap_or(true)).await?; + } + Commands::Device { devicecmd } => match devicecmd { + DeviceCmd::Add { + id, + mac, + broadcast_addr, + ip, + } => { + device::put(&config, id, mac, broadcast_addr, ip).await?; + } + DeviceCmd::Get { id } => { + device::get(&config, id).await?; + } + DeviceCmd::Edit { + id, + mac, + broadcast_addr, + ip, + } => { + device::post(&config, id, mac, broadcast_addr, ip).await?; } }, Commands::CliGen { id } => { @@ -100,29 +110,26 @@ fn print_completions(gen: G, cmd: &mut Command) { generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout()); } -fn default_headers() -> Result { +fn default_headers(config: &Config) -> Result { let mut map = HeaderMap::new(); - map.append("Accept-Content", HeaderValue::from_str("application/json").unwrap()); - map.append("Content-Type", HeaderValue::from_str("application/json").unwrap()); + map.append( + "Accept-Content", + HeaderValue::from_str("application/json").unwrap(), + ); + map.append( + "Content-Type", + HeaderValue::from_str("application/json").unwrap(), + ); map.append( "Authorization", - HeaderValue::from_str( - SETTINGS.get_string("key") - .map_err(CliError::Config)? - .as_str() - ).unwrap() + HeaderValue::from_str(&config.apikey).unwrap(), ); Ok(map) } -fn format_url(path: &str, protocol: Protocols) -> Result { - Ok(format!( - "{}://{}/{}", - protocol, - SETTINGS.get_string("server").map_err(CliError::Config)?, - path - )) +fn format_url(config: &Config, path: &str, protocol: Protocols) -> Result { + Ok(format!("{}://{}/{}", protocol, config.server, path)) } fn add_pb(mp: &MultiProgress, template: &str, message: String) -> ProgressBar { @@ -137,7 +144,6 @@ fn add_pb(mp: &MultiProgress, template: &str, message: String) -> ProgressBar { fn finish_pb(pb: ProgressBar, message: String, template: &str) { pb.set_style(ProgressStyle::with_template(template).unwrap()); pb.finish_with_message(message); - } enum Protocols { @@ -149,12 +155,12 @@ impl Display for Protocols { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::Http => f.write_str("http"), - Self::Websocket => f.write_str("ws") + Self::Websocket => f.write_str("ws"), } } } #[derive(Debug, Deserialize)] struct ErrorResponse { - error: String + error: String, } diff --git a/src/requests.rs b/src/requests.rs new file mode 100644 index 0000000..6855db1 --- /dev/null +++ b/src/requests.rs @@ -0,0 +1,2 @@ +pub mod start; +pub mod device; diff --git a/src/requests/device.rs b/src/requests/device.rs index cbc838e..5003c4a 100644 --- a/src/requests/device.rs +++ b/src/requests/device.rs @@ -1,20 +1,21 @@ -use crate::{error::CliError, default_headers, format_url, Protocols}; +use crate::{config::Config, default_headers, error::CliError, format_url, Protocols}; -pub async fn put(id: String, mac: String, broadcast_addr: String, ip: String) -> Result<(), CliError> { - let url = format_url("device", Protocols::Http)?; +pub async fn put( + config: &Config, + id: String, + mac: String, + broadcast_addr: String, + ip: String, +) -> Result<(), CliError> { + let url = format_url(config, "device", Protocols::Http)?; println!("{}", url); let res = reqwest::Client::new() .put(url) - .headers(default_headers()?) - .body( - format!( - r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}", "ip": "{}"}}"#, - id, - mac, - broadcast_addr, - ip - ) - ) + .headers(default_headers(config)?) + .body(format!( + r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}", "ip": "{}"}}"#, + id, mac, broadcast_addr, ip + )) .send() .await .map_err(CliError::Reqwest)? @@ -25,13 +26,11 @@ pub async fn put(id: String, mac: String, broadcast_addr: String, ip: String) -> Ok(()) } -pub async fn get(id: String) -> Result<(), CliError> { +pub async fn get(config: &Config, id: String) -> Result<(), CliError> { let res = reqwest::Client::new() - .get(format_url("device", Protocols::Http)?) - .headers(default_headers()?) - .body( - format!(r#"{{"id": "{}"}}"#, id) - ) + .get(format_url(config, "device", Protocols::Http)?) + .headers(default_headers(config)?) + .body(format!(r#"{{"id": "{}"}}"#, id)) .send() .await .map_err(CliError::Reqwest)? @@ -42,19 +41,20 @@ pub async fn get(id: String) -> Result<(), CliError> { Ok(()) } -pub async fn post(id: String, mac: String, broadcast_addr: String, ip: String) -> Result<(), CliError> { +pub async fn post( + config: &Config, + id: String, + mac: String, + broadcast_addr: String, + ip: String, +) -> Result<(), CliError> { let res = reqwest::Client::new() - .post(format_url("device", Protocols::Http)?) - .headers(default_headers()?) - .body( - format!( - r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}", "ip": "{}"}}"#, - id, - mac, - broadcast_addr, - ip - ) - ) + .post(format_url(config, "device", Protocols::Http)?) + .headers(default_headers(config)?) + .body(format!( + r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}", "ip": "{}"}}"#, + id, mac, broadcast_addr, ip + )) .send() .await .map_err(CliError::Reqwest)? diff --git a/src/requests/mod.rs b/src/requests/mod.rs deleted file mode 100644 index 6855db1..0000000 --- a/src/requests/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod start; -pub mod device; diff --git a/src/requests/start.rs b/src/requests/start.rs index ca4ca44..bc63303 100644 --- a/src/requests/start.rs +++ b/src/requests/start.rs @@ -1,25 +1,26 @@ -use futures_util::{StreamExt, SinkExt}; +use futures_util::{SinkExt, StreamExt}; use indicatif::{MultiProgress, ProgressBar}; use reqwest::StatusCode; use serde::Deserialize; use tokio_tungstenite::{connect_async, tungstenite::Message}; -use crate::{error::CliError, default_headers, ErrorResponse, format_url, Protocols, OVERVIEW_STYLE, DEFAULT_STYLE, DONE_STYLE, finish_pb, ERROR_STYLE, OVERVIEW_ERROR, OVERVIEW_DONE, add_pb}; - -pub async fn start(id: String, ping: bool) -> Result<(), CliError> { +use crate::{ + add_pb, config::Config, default_headers, error::CliError, finish_pb, format_url, ErrorResponse, + Protocols, DEFAULT_STYLE, DONE_STYLE, ERROR_STYLE, OVERVIEW_DONE, OVERVIEW_ERROR, + OVERVIEW_STYLE, +}; +pub async fn start(config: &Config, id: String, ping: bool) -> Result<(), CliError> { let send_start = MultiProgress::new(); let overview = add_pb(&send_start, OVERVIEW_STYLE, format!(") start {}", id)); // TODO: calculate average start-time on server - let url = format_url("start", Protocols::Http)?; + let url = format_url(config, "start", Protocols::Http)?; let connect = add_pb(&send_start, DEFAULT_STYLE, format!("connect to {}", url)); let res = reqwest::Client::new() .post(url) - .headers(default_headers()?) - .body( - format!(r#"{{"id": "{}", "ping": {}}}"#, id, ping) - ) + .headers(default_headers(config)?) + .body(format!(r#"{{"id": "{}", "ping": {}}}"#, id, ping)) .send() .await .map_err(CliError::Reqwest)?; @@ -29,7 +30,7 @@ pub async fn start(id: String, ping: bool) -> Result<(), CliError> { match res.status() { StatusCode::OK => { let body = serde_json::from_str::( - &res.text().await.map_err(CliError::Reqwest)? + &res.text().await.map_err(CliError::Reqwest)?, ) .map_err(CliError::Serde)?; @@ -38,17 +39,25 @@ pub async fn start(id: String, ping: bool) -> Result<(), CliError> { } if ping { - let status = status_socket(body.uuid, &send_start, &overview, id).await?; + let status = status_socket(config, body.uuid, &send_start, &overview, id).await?; if status { - finish_pb(overview, format!("successfully started {}", body.id), OVERVIEW_DONE); + finish_pb( + overview, + format!("successfully started {}", body.id), + OVERVIEW_DONE, + ); } else { - finish_pb(overview, format!("error while starting {}", body.id), OVERVIEW_ERROR); + finish_pb( + overview, + format!("error while starting {}", body.id), + OVERVIEW_ERROR, + ); } } - }, + } _ => { let body = serde_json::from_str::( - &res.text().await.map_err(CliError::Reqwest)? + &res.text().await.map_err(CliError::Reqwest)?, ) .map_err(CliError::Serde)?; @@ -59,16 +68,22 @@ pub async fn start(id: String, ping: bool) -> Result<(), CliError> { Ok(()) } -async fn status_socket(uuid: String, pb: &MultiProgress, overview: &ProgressBar, id: String) -> Result { - // TODO: Remove unwraps +async fn status_socket( + config: &Config, + uuid: String, + pb: &MultiProgress, + overview: &ProgressBar, + id: String, +) -> Result { let ws_pb = add_pb(pb, DEFAULT_STYLE, "connect to websocket".to_string()); - let (mut ws_stream, _response) = connect_async(format_url("status", Protocols::Websocket)?) - .await - .expect("Failed to connect"); + let (mut ws_stream, _response) = + connect_async(format_url(config, "status", Protocols::Websocket)?) + .await + .expect("Failed to connect"); finish_pb(ws_pb, "connected to websocket".to_string(), DONE_STYLE); - + ws_stream.send(Message::Text(uuid.clone())).await.unwrap(); - + // Get ETA let eta_msg = ws_stream.next().await.unwrap().unwrap(); let eta = get_eta(eta_msg.into_text().unwrap(), uuid.clone())? + overview.elapsed().as_secs(); @@ -86,29 +101,29 @@ async fn status_socket(uuid: String, pb: &MultiProgress, overview: &ProgressBar, Verified::WrongUuid => { finish_pb(v_pb, "returned wrong uuid".to_string(), ERROR_STYLE); Ok(false) - }, - Verified::ResponseType(res_type) => { - match res_type { - ResponseType::Start => { - finish_pb(v_pb, "device started".to_string(), DONE_STYLE); - Ok(true) - }, - ResponseType::Timeout => { - finish_pb(v_pb, "ping timed out".to_string(), ERROR_STYLE); - Ok(false) - }, - ResponseType::NotFound => { - finish_pb(v_pb, "unknown uuid".to_string(), ERROR_STYLE); - Ok(false) - }, - } } + Verified::ResponseType(res_type) => match res_type { + ResponseType::Start => { + finish_pb(v_pb, "device started".to_string(), DONE_STYLE); + Ok(true) + } + ResponseType::Timeout => { + finish_pb(v_pb, "ping timed out".to_string(), ERROR_STYLE); + Ok(false) + } + ResponseType::NotFound => { + finish_pb(v_pb, "unknown uuid".to_string(), ERROR_STYLE); + Ok(false) + } + }, } } fn get_eta(msg: String, uuid: String) -> Result { let spl: Vec<&str> = msg.split('_').collect(); - if (spl[0] != "eta") || (spl[2] != uuid) { return Err(CliError::WsResponse); }; + if (spl[0] != "eta") || (spl[2] != uuid) { + return Err(CliError::WsResponse); + }; Ok(u64::from_str_radix(spl[1], 10).map_err(CliError::Parse)?) } @@ -116,9 +131,11 @@ fn verify_response(res: String, org_uuid: String) -> Result let spl: Vec<&str> = res.split('_').collect(); let res_type = spl[0]; let uuid = spl[1]; - - if uuid != org_uuid { return Ok(Verified::WrongUuid) }; - + + if uuid != org_uuid { + return Ok(Verified::WrongUuid); + }; + Ok(Verified::ResponseType(ResponseType::from(res_type)?)) } @@ -131,7 +148,7 @@ struct StartResponse { enum Verified { ResponseType(ResponseType), - WrongUuid + WrongUuid, } enum ResponseType { -- cgit v1.2.3 From 465a71b6780921fb7ec19682702cbe864decd212 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Sun, 25 Feb 2024 16:54:03 +0100 Subject: Closes #3. Use thiserror. Fix clippy stuff --- Cargo.lock | 21 +++++----- Cargo.toml | 1 + src/config.rs | 4 +- src/error.rs | 47 +++++++++++++-------- src/main.rs | 22 +++++----- src/requests/device.rs | 39 ++++++++---------- src/requests/start.rs | 108 ++++++++++++++++++++++--------------------------- 7 files changed, 121 insertions(+), 121 deletions(-) (limited to 'src') diff --git a/Cargo.lock b/Cargo.lock index ad227e6..c0f07f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1021,18 +1021,18 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "proc-macro2" -version = "1.0.69" +version = "1.0.78" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.33" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] @@ -1315,9 +1315,9 @@ checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01" [[package]] name = "syn" -version = "2.0.39" +version = "2.0.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" +checksum = "74f1bdc9872430ce9b75da68329d1c1746faf50ffac5f19e02b71e37ff881ffb" dependencies = [ "proc-macro2", "quote", @@ -1360,18 +1360,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.50" +version = "1.0.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2" +checksum = "1e45bcbe8ed29775f228095caf2cd67af7a4ccf756ebff23a306bf3e8b47b24b" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.50" +version = "1.0.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8" +checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81" dependencies = [ "proc-macro2", "quote", @@ -1734,6 +1734,7 @@ dependencies = [ "reqwest", "serde", "serde_json", + "thiserror", "tokio", "tokio-tungstenite", ] diff --git a/Cargo.toml b/Cargo.toml index 461b0bf..a60d788 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,5 +19,6 @@ indicatif = "0.17" reqwest = { version = "0.11", features = ["blocking"] } serde = "1.0" serde_json = "1.0" +thiserror = "1.0.57" tokio = { version = "1.36", features = ["macros", "rt-multi-thread", "io-std"] } tokio-tungstenite = "0.21" diff --git a/src/config.rs b/src/config.rs index 78795a3..d28e111 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,8 +9,8 @@ pub struct Config { impl Config { pub fn load() -> Result { let builder = config::Config::builder() - .add_source(config::File::with_name("~/.config/webol-cli.toml")) - .add_source(config::File::with_name("webol-cli.toml")) + .add_source(config::File::with_name("~/.config/webol-cli").required(false)) + .add_source(config::File::with_name("webol-cli").required(false)) .add_source(config::Environment::with_prefix("WEBOL_CLI_").separator("_")) .build()?; diff --git a/src/error.rs b/src/error.rs index 531528f..15e4308 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,21 +1,34 @@ use std::{fmt::Debug, num::ParseIntError}; -pub enum CliError { - Reqwest(reqwest::Error), - Config(config::ConfigError), - Serde(serde_json::Error), - Parse(ParseIntError), - WsResponse, -} +use reqwest::header::InvalidHeaderValue; -impl Debug for CliError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::Reqwest(err) => err.fmt(f), - Self::Config(err) => err.fmt(f), - Self::Serde(err) => err.fmt(f), - Self::Parse(err) => err.fmt(f), - Self::WsResponse => f.write_str("Error in Response"), - } - } +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error("request: {source}")] + Reqwest { + #[from] + source: reqwest::Error, + }, + #[error("config: {source}")] + Config { + #[from] + source: config::ConfigError, + }, + #[error("serde: {source}")] + Serde { + #[from] + source: serde_json::Error, + }, + #[error("parse int: {source}")] + Parse { + #[from] + source: ParseIntError, + }, + #[error("parse header: {source}")] + InvalidHeaderValue { + #[from] + source: InvalidHeaderValue + }, + #[error("ws")] + WsResponse, } diff --git a/src/main.rs b/src/main.rs index 0393183..cdca6cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use std::{fmt::Display, time::Duration}; use crate::config::Config; use clap::{Command, CommandFactory, Parser, Subcommand}; use clap_complete::{generate, Generator, Shell}; -use error::CliError; +use error::Error; use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use requests::{device, start::start}; use reqwest::header::{HeaderMap, HeaderValue}; @@ -66,8 +66,8 @@ enum DeviceCmd { } #[tokio::main] -async fn main() -> Result<(), CliError> { - let config = Config::load().map_err(CliError::Config)?; +async fn main() -> Result<(), Error> { + let config = Config::load()?; let cli = Args::parse(); @@ -99,7 +99,7 @@ async fn main() -> Result<(), CliError> { Commands::CliGen { id } => { eprintln!("Generating completion file for {id:?}..."); let mut cmd = Args::command(); - print_completions(id, &mut cmd) + print_completions(id, &mut cmd); } } @@ -110,26 +110,26 @@ fn print_completions(gen: G, cmd: &mut Command) { generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout()); } -fn default_headers(config: &Config) -> Result { +fn default_headers(config: &Config) -> Result { let mut map = HeaderMap::new(); map.append( "Accept-Content", - HeaderValue::from_str("application/json").unwrap(), + HeaderValue::from_str("application/json")? ); map.append( "Content-Type", - HeaderValue::from_str("application/json").unwrap(), + HeaderValue::from_str("application/json")? ); map.append( "Authorization", - HeaderValue::from_str(&config.apikey).unwrap(), + HeaderValue::from_str(&config.apikey)? ); Ok(map) } -fn format_url(config: &Config, path: &str, protocol: Protocols) -> Result { - Ok(format!("{}://{}/{}", protocol, config.server, path)) +fn format_url(config: &Config, path: &str, protocol: &Protocols) -> String { + format!("{}://{}/{}", protocol, config.server, path) } fn add_pb(mp: &MultiProgress, template: &str, message: String) -> ProgressBar { @@ -141,7 +141,7 @@ fn add_pb(mp: &MultiProgress, template: &str, message: String) -> ProgressBar { pb } -fn finish_pb(pb: ProgressBar, message: String, template: &str) { +fn finish_pb(pb: &ProgressBar, message: String, template: &str) { pb.set_style(ProgressStyle::with_template(template).unwrap()); pb.finish_with_message(message); } diff --git a/src/requests/device.rs b/src/requests/device.rs index 5003c4a..a612978 100644 --- a/src/requests/device.rs +++ b/src/requests/device.rs @@ -1,4 +1,4 @@ -use crate::{config::Config, default_headers, error::CliError, format_url, Protocols}; +use crate::{config::Config, default_headers, error::Error, format_url, Protocols}; pub async fn put( config: &Config, @@ -6,38 +6,35 @@ pub async fn put( mac: String, broadcast_addr: String, ip: String, -) -> Result<(), CliError> { - let url = format_url(config, "device", Protocols::Http)?; - println!("{}", url); +) -> Result<(), Error> { + let url = format_url(config, "device", &Protocols::Http); + println!("{url}"); let res = reqwest::Client::new() .put(url) .headers(default_headers(config)?) .body(format!( - r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}", "ip": "{}"}}"#, - id, mac, broadcast_addr, ip + r#"{{"id": "{id}", "mac": "{mac}", "broadcast_addr": "{broadcast_addr}", "ip": "{ip}"}}"#, )) .send() - .await - .map_err(CliError::Reqwest)? + .await? .text() .await; - println!("{:?}", res); + println!("{res:?}"); Ok(()) } -pub async fn get(config: &Config, id: String) -> Result<(), CliError> { +pub async fn get(config: &Config, id: String) -> Result<(), Error> { let res = reqwest::Client::new() - .get(format_url(config, "device", Protocols::Http)?) + .get(format_url(config, "device", &Protocols::Http)) .headers(default_headers(config)?) - .body(format!(r#"{{"id": "{}"}}"#, id)) + .body(format!(r#"{{"id": "{id}"}}"#)) .send() - .await - .map_err(CliError::Reqwest)? + .await? .text() .await; - println!("{:?}", res); + println!("{res:?}"); Ok(()) } @@ -47,20 +44,18 @@ pub async fn post( mac: String, broadcast_addr: String, ip: String, -) -> Result<(), CliError> { +) -> Result<(), Error> { let res = reqwest::Client::new() - .post(format_url(config, "device", Protocols::Http)?) + .post(format_url(config, "device", &Protocols::Http)) .headers(default_headers(config)?) .body(format!( - r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}", "ip": "{}"}}"#, - id, mac, broadcast_addr, ip + r#"{{"id": "{id}", "mac": "{mac}", "broadcast_addr": "{broadcast_addr}", "ip": "{ip}"}}"#, )) .send() - .await - .map_err(CliError::Reqwest)? + .await? .text() .await; - println!("{:?}", res); + println!("{res:?}"); Ok(()) } diff --git a/src/requests/start.rs b/src/requests/start.rs index bc63303..7abbbe0 100644 --- a/src/requests/start.rs +++ b/src/requests/start.rs @@ -5,64 +5,54 @@ use serde::Deserialize; use tokio_tungstenite::{connect_async, tungstenite::Message}; use crate::{ - add_pb, config::Config, default_headers, error::CliError, finish_pb, format_url, ErrorResponse, + add_pb, config::Config, default_headers, error::Error, finish_pb, format_url, ErrorResponse, Protocols, DEFAULT_STYLE, DONE_STYLE, ERROR_STYLE, OVERVIEW_DONE, OVERVIEW_ERROR, OVERVIEW_STYLE, }; -pub async fn start(config: &Config, id: String, ping: bool) -> Result<(), CliError> { +pub async fn start(config: &Config, id: String, ping: bool) -> Result<(), Error> { let send_start = MultiProgress::new(); - let overview = add_pb(&send_start, OVERVIEW_STYLE, format!(") start {}", id)); + let overview = add_pb(&send_start, OVERVIEW_STYLE, format!(") start {id}")); - // TODO: calculate average start-time on server - let url = format_url(config, "start", Protocols::Http)?; - let connect = add_pb(&send_start, DEFAULT_STYLE, format!("connect to {}", url)); + let url = format_url(config, "start", &Protocols::Http); + let connect = add_pb(&send_start, DEFAULT_STYLE, format!("connect to {url}")); let res = reqwest::Client::new() .post(url) .headers(default_headers(config)?) - .body(format!(r#"{{"id": "{}", "ping": {}}}"#, id, ping)) + .body(format!(r#"{{"id": "{id}", "ping": {ping}}}"#)) .send() - .await - .map_err(CliError::Reqwest)?; - finish_pb(connect, "connected, got response".to_string(), DONE_STYLE); + .await?; + finish_pb(&connect, "connected, got response".to_string(), DONE_STYLE); let res_pb = add_pb(&send_start, DEFAULT_STYLE, "analyzing response".to_string()); - match res.status() { - StatusCode::OK => { - let body = serde_json::from_str::( - &res.text().await.map_err(CliError::Reqwest)?, - ) - .map_err(CliError::Serde)?; - - if body.boot { - finish_pb(res_pb, "sent start packet".to_string(), DONE_STYLE); - } - if ping { - let status = status_socket(config, body.uuid, &send_start, &overview, id).await?; - if status { - finish_pb( - overview, - format!("successfully started {}", body.id), - OVERVIEW_DONE, - ); - } else { - finish_pb( - overview, - format!("error while starting {}", body.id), - OVERVIEW_ERROR, - ); - } - } + if res.status() == StatusCode::OK { + let body = serde_json::from_str::(&res.text().await?)?; + + if body.boot { + finish_pb(&res_pb, "sent start packet".to_string(), DONE_STYLE); } - _ => { - let body = serde_json::from_str::( - &res.text().await.map_err(CliError::Reqwest)?, - ) - .map_err(CliError::Serde)?; - res_pb.finish_with_message(format!("✗ got error: {}", body.error)); + if ping { + let status = status_socket(config, body.uuid, &send_start, &overview, id).await?; + if status { + finish_pb( + &overview, + format!("successfully started {}", body.id), + OVERVIEW_DONE, + ); + } else { + finish_pb( + &overview, + format!("error while starting {}", body.id), + OVERVIEW_ERROR, + ); + } } + } else { + let body = serde_json::from_str::(&res.text().await?)?; + + res_pb.finish_with_message(format!("✗ got error: {}", body.error)); } Ok(()) @@ -74,60 +64,60 @@ async fn status_socket( pb: &MultiProgress, overview: &ProgressBar, id: String, -) -> Result { +) -> Result { let ws_pb = add_pb(pb, DEFAULT_STYLE, "connect to websocket".to_string()); let (mut ws_stream, _response) = - connect_async(format_url(config, "status", Protocols::Websocket)?) + connect_async(format_url(config, "status", &Protocols::Websocket)) .await .expect("Failed to connect"); - finish_pb(ws_pb, "connected to websocket".to_string(), DONE_STYLE); + finish_pb(&ws_pb, "connected to websocket".to_string(), DONE_STYLE); ws_stream.send(Message::Text(uuid.clone())).await.unwrap(); // Get ETA let eta_msg = ws_stream.next().await.unwrap().unwrap(); - let eta = get_eta(eta_msg.into_text().unwrap(), uuid.clone())? + overview.elapsed().as_secs(); - overview.set_message(format!("/{}) start {}", eta, id)); + let eta = get_eta(&eta_msg.into_text().unwrap(), &uuid)? + overview.elapsed().as_secs(); + overview.set_message(format!("/{eta}) start {id}")); let msg_pb = add_pb(pb, DEFAULT_STYLE, "await message".to_string()); let msg = ws_stream.next().await.unwrap(); - finish_pb(msg_pb, "received message".to_string(), DONE_STYLE); + finish_pb(&msg_pb, "received message".to_string(), DONE_STYLE); ws_stream.close(None).await.unwrap(); let v_pb = add_pb(pb, DEFAULT_STYLE, "verify response".to_string()); - let res = verify_response(msg.unwrap().to_string(), uuid)?; + let res = verify_response(&msg.unwrap().to_string(), &uuid)?; match res { Verified::WrongUuid => { - finish_pb(v_pb, "returned wrong uuid".to_string(), ERROR_STYLE); + finish_pb(&v_pb, "returned wrong uuid".to_string(), ERROR_STYLE); Ok(false) } Verified::ResponseType(res_type) => match res_type { ResponseType::Start => { - finish_pb(v_pb, "device started".to_string(), DONE_STYLE); + finish_pb(&v_pb, "device started".to_string(), DONE_STYLE); Ok(true) } ResponseType::Timeout => { - finish_pb(v_pb, "ping timed out".to_string(), ERROR_STYLE); + finish_pb(&v_pb, "ping timed out".to_string(), ERROR_STYLE); Ok(false) } ResponseType::NotFound => { - finish_pb(v_pb, "unknown uuid".to_string(), ERROR_STYLE); + finish_pb(&v_pb, "unknown uuid".to_string(), ERROR_STYLE); Ok(false) } }, } } -fn get_eta(msg: String, uuid: String) -> Result { +fn get_eta(msg: &str, uuid: &str) -> Result { let spl: Vec<&str> = msg.split('_').collect(); if (spl[0] != "eta") || (spl[2] != uuid) { - return Err(CliError::WsResponse); + return Err(Error::WsResponse); }; - Ok(u64::from_str_radix(spl[1], 10).map_err(CliError::Parse)?) + Ok(spl[1].parse()?) } -fn verify_response(res: String, org_uuid: String) -> Result { +fn verify_response(res: &str, org_uuid: &str) -> Result { let spl: Vec<&str> = res.split('_').collect(); let res_type = spl[0]; let uuid = spl[1]; @@ -158,12 +148,12 @@ enum ResponseType { } impl ResponseType { - fn from(value: &str) -> Result { + fn from(value: &str) -> Result { match value { "start" => Ok(ResponseType::Start), "timeout" => Ok(ResponseType::Timeout), "notfound" => Ok(ResponseType::NotFound), - _ => Err(CliError::WsResponse), + _ => Err(Error::WsResponse), } } } -- cgit v1.2.3 From a192e9baca9a14beaa9f87c27a63cff96aa41c94 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Sun, 25 Feb 2024 20:00:38 +0100 Subject: Closes #4. Auth on Websocket. Small stuff --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/error.rs | 13 +++++++++++-- src/main.rs | 33 +++++++++++++++++++-------------- src/requests/device.rs | 23 ++++++++++------------- src/requests/start.rs | 24 ++++++++++++++++++------ 6 files changed, 66 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/Cargo.lock b/Cargo.lock index c0f07f7..25f23fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -65,6 +65,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "anyhow" +version = "1.0.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ad32ce52e4161730f7098c077cd2ed6229b5804ccf99e5366be1ab72a98b4e1" + [[package]] name = "async-trait" version = "0.1.74" @@ -1725,6 +1731,7 @@ dependencies = [ name = "webol-cli" version = "0.2.0" dependencies = [ + "anyhow", "clap", "clap_complete", "config", diff --git a/Cargo.toml b/Cargo.toml index a60d788..4791c6c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ name = "webol" path = "src/main.rs" [dependencies] +anyhow = "1.0" clap = { version = "4.5", features = ["derive"] } clap_complete = "4.5" config = "0.14" diff --git a/src/error.rs b/src/error.rs index 15e4308..1e6eac1 100644 --- a/src/error.rs +++ b/src/error.rs @@ -27,8 +27,17 @@ pub enum Error { #[error("parse header: {source}")] InvalidHeaderValue { #[from] - source: InvalidHeaderValue + source: InvalidHeaderValue, }, - #[error("ws")] + #[error("tungstenite: {source}")] + Tungstenite { + #[from] + source: tokio_tungstenite::tungstenite::Error, + }, + #[error("faulty websocket response")] WsResponse, + #[error("authorization failed")] + Authorization, + #[error("Http error status: {0}")] + HttpStatus(u16), } diff --git a/src/main.rs b/src/main.rs index cdca6cb..d76341f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,10 @@ use clap_complete::{generate, Generator, Shell}; use error::Error; use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use requests::{device, start::start}; -use reqwest::header::{HeaderMap, HeaderValue}; +use reqwest::{ + header::{HeaderMap, HeaderValue}, + Response, +}; use serde::Deserialize; mod config; @@ -66,7 +69,7 @@ enum DeviceCmd { } #[tokio::main] -async fn main() -> Result<(), Error> { +async fn main() -> Result<(), anyhow::Error> { let config = Config::load()?; let cli = Args::parse(); @@ -112,18 +115,9 @@ fn print_completions(gen: G, cmd: &mut Command) { fn default_headers(config: &Config) -> Result { let mut map = HeaderMap::new(); - map.append( - "Accept-Content", - HeaderValue::from_str("application/json")? - ); - map.append( - "Content-Type", - HeaderValue::from_str("application/json")? - ); - map.append( - "Authorization", - HeaderValue::from_str(&config.apikey)? - ); + map.append("Accept-Content", HeaderValue::from_str("application/json")?); + map.append("Content-Type", HeaderValue::from_str("application/json")?); + map.append("Authorization", HeaderValue::from_str(&config.apikey)?); Ok(map) } @@ -132,6 +126,17 @@ fn format_url(config: &Config, path: &str, protocol: &Protocols) -> String { format!("{}://{}/{}", protocol, config.server, path) } +async fn check_success(res: Response) -> Result { + let status = res.status(); + if status.is_success() { + Ok(res.text().await?) + } else if status.as_u16() == 401 { + Err(Error::Authorization) + } else { + Err(Error::HttpStatus(status.as_u16())) + } +} + fn add_pb(mp: &MultiProgress, template: &str, message: String) -> ProgressBar { let pb = mp.add(ProgressBar::new(1)); pb.set_style(ProgressStyle::with_template(template).unwrap()); diff --git a/src/requests/device.rs b/src/requests/device.rs index a612978..7583406 100644 --- a/src/requests/device.rs +++ b/src/requests/device.rs @@ -1,4 +1,4 @@ -use crate::{config::Config, default_headers, error::Error, format_url, Protocols}; +use crate::{check_success, config::Config, default_headers, error::Error, format_url, Protocols}; pub async fn put( config: &Config, @@ -16,11 +16,10 @@ pub async fn put( r#"{{"id": "{id}", "mac": "{mac}", "broadcast_addr": "{broadcast_addr}", "ip": "{ip}"}}"#, )) .send() - .await? - .text() - .await; + .await?; - println!("{res:?}"); + let body = check_success(res).await?; + println!("{body}"); Ok(()) } @@ -30,11 +29,10 @@ pub async fn get(config: &Config, id: String) -> Result<(), Error> { .headers(default_headers(config)?) .body(format!(r#"{{"id": "{id}"}}"#)) .send() - .await? - .text() - .await; + .await?; - println!("{res:?}"); + let body = check_success(res).await?; + println!("{body}"); Ok(()) } @@ -52,10 +50,9 @@ pub async fn post( r#"{{"id": "{id}", "mac": "{mac}", "broadcast_addr": "{broadcast_addr}", "ip": "{ip}"}}"#, )) .send() - .await? - .text() - .await; + .await?; - println!("{res:?}"); + let body = check_success(res).await?; + println!("{body}"); Ok(()) } diff --git a/src/requests/start.rs b/src/requests/start.rs index 7abbbe0..d07177e 100644 --- a/src/requests/start.rs +++ b/src/requests/start.rs @@ -2,7 +2,10 @@ use futures_util::{SinkExt, StreamExt}; use indicatif::{MultiProgress, ProgressBar}; use reqwest::StatusCode; use serde::Deserialize; -use tokio_tungstenite::{connect_async, tungstenite::Message}; +use tokio_tungstenite::{ + connect_async, + tungstenite::{http::Request, Message}, +}; use crate::{ add_pb, config::Config, default_headers, error::Error, finish_pb, format_url, ErrorResponse, @@ -66,17 +69,26 @@ async fn status_socket( id: String, ) -> Result { let ws_pb = add_pb(pb, DEFAULT_STYLE, "connect to websocket".to_string()); - let (mut ws_stream, _response) = - connect_async(format_url(config, "status", &Protocols::Websocket)) - .await - .expect("Failed to connect"); + + let request = Request::builder() + .uri(format_url(config, "status", &Protocols::Websocket)) + .header("Authorization", &config.apikey) + .header("sec-websocket-key", "") + .header("host", &config.server) + .header("upgrade", "websocket") + .header("connection", "upgrade") + .header("sec-websocket-version", 13) + .body(()) + .unwrap(); + + let (mut ws_stream, _response) = connect_async(request).await?; finish_pb(&ws_pb, "connected to websocket".to_string(), DONE_STYLE); ws_stream.send(Message::Text(uuid.clone())).await.unwrap(); // Get ETA let eta_msg = ws_stream.next().await.unwrap().unwrap(); - let eta = get_eta(&eta_msg.into_text().unwrap(), &uuid)? + overview.elapsed().as_secs(); + let eta = get_eta(&eta_msg.into_text().unwrap(), &uuid)?; overview.set_message(format!("/{eta}) start {id}")); let msg_pb = add_pb(pb, DEFAULT_STYLE, "await message".to_string()); -- cgit v1.2.3 From 3a2dc1f2741f7bf4d72c1cbe0fd1993af157ceaa Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Sun, 25 Feb 2024 20:50:50 +0100 Subject: Closes #5. Eta works --- docker-compose.yml | 2 +- src/main.rs | 6 +++--- src/requests/start.rs | 10 ++++++++-- 3 files changed, 12 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/docker-compose.yml b/docker-compose.yml index f41f4c9..ea10db2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,7 +9,7 @@ services: - RUST_LOG=info,webol=trace - WEBOL_DATABASE_URL=postgres://postgres:postgres@localhost:5432/webol - WEBOL_APIKEY=dev - - WEBOL_SERVERADDR=127.0.0.1:7229 + - WEBOL_SERVERADDR=0.0.0.0:7229 network_mode: host db: diff --git a/src/main.rs b/src/main.rs index d76341f..5a0931d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,9 +16,9 @@ mod config; mod error; mod requests; -static OVERVIEW_STYLE: &str = "{spinner:.green} ({elapsed}{wide_msg}"; -static OVERVIEW_ERROR: &str = "✗ ({elapsed}) {wide_msg}"; -static OVERVIEW_DONE: &str = "✓ ({elapsed}) {wide_msg}"; +static OVERVIEW_STYLE: &str = "{spinner:.green} ({elapsed_precise}{wide_msg}"; +static OVERVIEW_ERROR: &str = "✗ ({elapsed_precise}) {wide_msg}"; +static OVERVIEW_DONE: &str = "✓ ({elapsed_precise}) {wide_msg}"; static DEFAULT_STYLE: &str = " {spinner:.green} {wide_msg}"; static DONE_STYLE: &str = " ✓ {wide_msg}"; static ERROR_STYLE: &str = " ✗ {wide_msg}"; diff --git a/src/requests/start.rs b/src/requests/start.rs index d07177e..3afbe3a 100644 --- a/src/requests/start.rs +++ b/src/requests/start.rs @@ -121,12 +121,18 @@ async fn status_socket( } } -fn get_eta(msg: &str, uuid: &str) -> Result { +fn get_eta(msg: &str, uuid: &str) -> Result { let spl: Vec<&str> = msg.split('_').collect(); if (spl[0] != "eta") || (spl[2] != uuid) { return Err(Error::WsResponse); }; - Ok(spl[1].parse()?) + let input: u64 = spl[1].parse()?; + + let sec = input % 60; + let min = (input / 60) % 60; + let hou = (input / (60 * 60)) % 60; + + Ok(format!("{hou:0>2}:{min:0>2}:{sec:0>2}")) } fn verify_response(res: &str, org_uuid: &str) -> Result { -- cgit v1.2.3 From ffedfd00a46147b225c834187fc298e88e60d0c5 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Mon, 26 Feb 2024 12:43:35 +0100 Subject: Closes #6. Should autodetect config dir --- Cargo.toml | 2 +- src/config.rs | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/Cargo.toml b/Cargo.toml index 4791c6c..36b08c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,6 @@ indicatif = "0.17" reqwest = { version = "0.11", features = ["blocking"] } serde = "1.0" serde_json = "1.0" -thiserror = "1.0.57" +thiserror = "1.0" tokio = { version = "1.36", features = ["macros", "rt-multi-thread", "io-std"] } tokio-tungstenite = "0.21" diff --git a/src/config.rs b/src/config.rs index d28e111..769269c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,12 +8,23 @@ pub struct Config { impl Config { pub fn load() -> Result { - let builder = config::Config::builder() - .add_source(config::File::with_name("~/.config/webol-cli").required(false)) + let config_dir = dirs::config_dir(); + + let builder = config::Config::builder(); + + let builder = if let Some(conf) = config_dir { + let dir = conf.to_string_lossy(); + builder.add_source(config::File::with_name(format!("{dir}/webol-cli").as_str()).required(false)) + } else { + println!("!No config dir found"); + builder + }; + + let build = builder .add_source(config::File::with_name("webol-cli").required(false)) - .add_source(config::Environment::with_prefix("WEBOL_CLI_").separator("_")) + .add_source(config::Environment::with_prefix("WEBOL_CLI").separator("_")) .build()?; - builder.try_deserialize() + build.try_deserialize() } } -- cgit v1.2.3