From f4d3d921460b606a9ff6686c9bb9a79bf546f264 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Thu, 2 Nov 2023 21:01:16 +0100 Subject: baseline ping --- src/main.rs | 48 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index ab7e476..3e1388b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use std::fmt::Display; + use clap::{Parser, Subcommand}; use config::SETTINGS; use error::CliError; @@ -21,7 +23,9 @@ struct Args { enum Commands { Start { /// id of the device - id: String + id: String, + #[arg(short, long)] + ping: Option }, Device { #[command(subcommand)] @@ -34,7 +38,8 @@ enum DeviceCmd { Add { id: String, mac: String, - broadcast_addr: String + broadcast_addr: String, + ip: String }, Get { id: String, @@ -42,27 +47,29 @@ enum DeviceCmd { Edit { id: String, mac: String, - broadcast_addr: String + broadcast_addr: String, + ip: String }, } -fn main() -> Result<(), CliError> { +#[tokio::main] +async fn main() -> Result<(), CliError> { let cli = Args::parse(); match cli.commands { - Commands::Start { id } => { - start(id)?; + Commands::Start { id, ping } => { + start(id, ping.unwrap_or(true)).await?; }, Commands::Device { devicecmd } => { match devicecmd { - DeviceCmd::Add { id, mac, broadcast_addr } => { - device::put(id, mac, broadcast_addr)?; + DeviceCmd::Add { id, mac, broadcast_addr, ip } => { + device::put(id, mac, broadcast_addr, ip).await?; }, DeviceCmd::Get { id } => { - device::get(id)?; + device::get(id).await?; }, - DeviceCmd::Edit { id, mac, broadcast_addr } => { - device::post(id, mac, broadcast_addr)?; + DeviceCmd::Edit { id, mac, broadcast_addr, ip } => { + device::post(id, mac, broadcast_addr, ip).await?; }, } } @@ -87,14 +94,29 @@ fn default_headers() -> Result { Ok(map) } -fn format_url(path: &str) -> Result { +fn format_url(path: &str, protocol: Protocols) -> Result { Ok(format!( - "{}/{}", + "{}://{}/{}", + protocol, SETTINGS.get_string("server").map_err(CliError::Config)?, path )) } +enum Protocols { + Http, + Websocket, +} + +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") + } + } +} + #[derive(Debug, Deserialize)] struct ErrorResponse { error: String -- cgit v1.2.3