From b4f59c226c6916a3e45f1a52dc6a9b15c800297a Mon Sep 17 00:00:00 2001 From: fx Date: Wed, 18 Oct 2023 15:11:44 +0200 Subject: basic cli, only start and get device --- src/config.rs | 19 ++++++++++++++++ src/error.rs | 19 ++++++++++++++++ src/main.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/requests/get.rs | 21 ++++++++++++++++++ src/requests/mod.rs | 2 ++ src/requests/start.rs | 34 +++++++++++++++++++++++++++++ 6 files changed, 155 insertions(+) create mode 100644 src/config.rs create mode 100644 src/error.rs create mode 100644 src/main.rs create mode 100644 src/requests/get.rs create mode 100644 src/requests/mod.rs create mode 100644 src/requests/start.rs (limited to 'src') diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..9a9e44b --- /dev/null +++ b/src/config.rs @@ -0,0 +1,19 @@ +use config::Config; +use once_cell::sync::Lazy; + +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(), + )); + + #[cfg(debug_assertions)] + let builder = Config::builder().add_source(config::File::with_name("webol-cli.toml")); + + builder + .add_source(config::Environment::with_prefix("WEBOL_CLI_").separator("_")) + .build() + .unwrap() +} diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..9aa8c08 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,19 @@ +use std::fmt::Debug; + +pub enum CliError { + Reqwest(reqwest::Error), + Config(config::ConfigError), + Serde(serde_json::Error), + // Cli(Box), +} + +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::Cli(err) => {err.fmt(f)}, + } + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..d2f0c3a --- /dev/null +++ b/src/main.rs @@ -0,0 +1,60 @@ +use clap::{Parser, Subcommand}; +use config::SETTINGS; +use error::CliError; +use requests::{start::start, get::get}; +use reqwest::header::{HeaderMap, HeaderValue}; + +mod config; +mod error; +mod requests; + +/// webol http client +#[derive(Parser)] +#[command(author, version, about, long_about = None)] +struct Args { + #[command(subcommand)] + commands: Commands, +} + +#[derive(Subcommand)] +enum Commands { + Start { + /// id of the device + id: String + }, + Get { + id: String + } +} + +fn main() -> Result<(), CliError> { + let cli = Args::parse(); + + match cli.commands { + Commands::Start { id } => { + start(id)?; + }, + Commands::Get { id } => { + get(id)?; + } + } + + Ok(()) +} + +fn default_headers() -> 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( + "Authorization", + HeaderValue::from_str( + SETTINGS.get_string("key") + .map_err(CliError::Config)? + .as_str() + ).unwrap() + ); + + Ok(map) + +} diff --git a/src/requests/get.rs b/src/requests/get.rs new file mode 100644 index 0000000..225562d --- /dev/null +++ b/src/requests/get.rs @@ -0,0 +1,21 @@ +use crate::{error::CliError, config::SETTINGS, default_headers}; + +pub fn get(id: String) -> Result<(), CliError> { + let res = reqwest::blocking::Client::new() + .get( + format!( + "{}/start", + SETTINGS.get_string("server").map_err(CliError::Config)? + ) + ) + .headers(default_headers()?) + .body( + format!(r#"{{"id": "{}"}}"#, id) + ) + .send() + .map_err(CliError::Reqwest)? + .text(); + + println!("{:?}", res); + Ok(()) +} diff --git a/src/requests/mod.rs b/src/requests/mod.rs new file mode 100644 index 0000000..ddf0ea9 --- /dev/null +++ b/src/requests/mod.rs @@ -0,0 +1,2 @@ +pub mod start; +pub mod get; diff --git a/src/requests/start.rs b/src/requests/start.rs new file mode 100644 index 0000000..1a8cb6c --- /dev/null +++ b/src/requests/start.rs @@ -0,0 +1,34 @@ +use serde::Deserialize; + +use crate::{config::SETTINGS, error::CliError, default_headers}; + +pub fn start(id: String) -> Result<(), CliError> { + let res = reqwest::blocking::Client::new() + .post( + format!( + "{}/start", + SETTINGS.get_string("server").map_err(CliError::Config)? + ) + ) + .headers(default_headers()?) + .body( + format!(r#"{{"id": "{}"}}"#, id) + ) + .send() + .map_err(CliError::Reqwest)? + .text(); + + let res = serde_json::from_str::(&res.map_err(CliError::Reqwest)?).map_err(CliError::Serde)?; + + if res.boot { + println!("successfully started {}", res.id); + } + + Ok(()) +} + +#[derive(Debug, Deserialize)] +struct StartResponse { + boot: bool, + id: String, +} -- cgit v1.2.3