diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/config.rs | 19 | ||||
-rw-r--r-- | src/error.rs | 19 | ||||
-rw-r--r-- | src/main.rs | 60 | ||||
-rw-r--r-- | src/requests/get.rs | 21 | ||||
-rw-r--r-- | src/requests/mod.rs | 2 | ||||
-rw-r--r-- | src/requests/start.rs | 34 |
6 files changed, 155 insertions, 0 deletions
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 @@ | |||
1 | use config::Config; | ||
2 | use once_cell::sync::Lazy; | ||
3 | |||
4 | pub static SETTINGS: Lazy<Config> = Lazy::new(setup); | ||
5 | |||
6 | fn setup() -> Config { | ||
7 | #[cfg(not(debug_assertions))] | ||
8 | let builder = Config::builder().add_source(config::File::with_name( | ||
9 | format!("{}/webol-cli.toml", dirs::config_dir().unwrap().to_string_lossy()).as_str(), | ||
10 | )); | ||
11 | |||
12 | #[cfg(debug_assertions)] | ||
13 | let builder = Config::builder().add_source(config::File::with_name("webol-cli.toml")); | ||
14 | |||
15 | builder | ||
16 | .add_source(config::Environment::with_prefix("WEBOL_CLI_").separator("_")) | ||
17 | .build() | ||
18 | .unwrap() | ||
19 | } | ||
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 @@ | |||
1 | use std::fmt::Debug; | ||
2 | |||
3 | pub enum CliError { | ||
4 | Reqwest(reqwest::Error), | ||
5 | Config(config::ConfigError), | ||
6 | Serde(serde_json::Error), | ||
7 | // Cli(Box<dyn std::error::Error>), | ||
8 | } | ||
9 | |||
10 | impl Debug for CliError { | ||
11 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
12 | match self { | ||
13 | Self::Reqwest(err) => { err.fmt(f) }, | ||
14 | Self::Config(err) => { err.fmt(f) }, | ||
15 | Self::Serde(err) => { err.fmt(f) }, | ||
16 | // Self::Cli(err) => {err.fmt(f)}, | ||
17 | } | ||
18 | } | ||
19 | } | ||
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 @@ | |||
1 | use clap::{Parser, Subcommand}; | ||
2 | use config::SETTINGS; | ||
3 | use error::CliError; | ||
4 | use requests::{start::start, get::get}; | ||
5 | use reqwest::header::{HeaderMap, HeaderValue}; | ||
6 | |||
7 | mod config; | ||
8 | mod error; | ||
9 | mod requests; | ||
10 | |||
11 | /// webol http client | ||
12 | #[derive(Parser)] | ||
13 | #[command(author, version, about, long_about = None)] | ||
14 | struct Args { | ||
15 | #[command(subcommand)] | ||
16 | commands: Commands, | ||
17 | } | ||
18 | |||
19 | #[derive(Subcommand)] | ||
20 | enum Commands { | ||
21 | Start { | ||
22 | /// id of the device | ||
23 | id: String | ||
24 | }, | ||
25 | Get { | ||
26 | id: String | ||
27 | } | ||
28 | } | ||
29 | |||
30 | fn main() -> Result<(), CliError> { | ||
31 | let cli = Args::parse(); | ||
32 | |||
33 | match cli.commands { | ||
34 | Commands::Start { id } => { | ||
35 | start(id)?; | ||
36 | }, | ||
37 | Commands::Get { id } => { | ||
38 | get(id)?; | ||
39 | } | ||
40 | } | ||
41 | |||
42 | Ok(()) | ||
43 | } | ||
44 | |||
45 | fn default_headers() -> Result<HeaderMap, CliError> { | ||
46 | let mut map = HeaderMap::new(); | ||
47 | map.append("Accept-Content", HeaderValue::from_str("application/json").unwrap()); | ||
48 | map.append("Content-Type", HeaderValue::from_str("application/json").unwrap()); | ||
49 | map.append( | ||
50 | "Authorization", | ||
51 | HeaderValue::from_str( | ||
52 | SETTINGS.get_string("key") | ||
53 | .map_err(CliError::Config)? | ||
54 | .as_str() | ||
55 | ).unwrap() | ||
56 | ); | ||
57 | |||
58 | Ok(map) | ||
59 | |||
60 | } | ||
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 @@ | |||
1 | use crate::{error::CliError, config::SETTINGS, default_headers}; | ||
2 | |||
3 | pub fn get(id: String) -> Result<(), CliError> { | ||
4 | let res = reqwest::blocking::Client::new() | ||
5 | .get( | ||
6 | format!( | ||
7 | "{}/start", | ||
8 | SETTINGS.get_string("server").map_err(CliError::Config)? | ||
9 | ) | ||
10 | ) | ||
11 | .headers(default_headers()?) | ||
12 | .body( | ||
13 | format!(r#"{{"id": "{}"}}"#, id) | ||
14 | ) | ||
15 | .send() | ||
16 | .map_err(CliError::Reqwest)? | ||
17 | .text(); | ||
18 | |||
19 | println!("{:?}", res); | ||
20 | Ok(()) | ||
21 | } | ||
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 @@ | |||
1 | pub mod start; | ||
2 | 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 @@ | |||
1 | use serde::Deserialize; | ||
2 | |||
3 | use crate::{config::SETTINGS, error::CliError, default_headers}; | ||
4 | |||
5 | pub fn start(id: String) -> Result<(), CliError> { | ||
6 | let res = reqwest::blocking::Client::new() | ||
7 | .post( | ||
8 | format!( | ||
9 | "{}/start", | ||
10 | SETTINGS.get_string("server").map_err(CliError::Config)? | ||
11 | ) | ||
12 | ) | ||
13 | .headers(default_headers()?) | ||
14 | .body( | ||
15 | format!(r#"{{"id": "{}"}}"#, id) | ||
16 | ) | ||
17 | .send() | ||
18 | .map_err(CliError::Reqwest)? | ||
19 | .text(); | ||
20 | |||
21 | let res = serde_json::from_str::<StartResponse>(&res.map_err(CliError::Reqwest)?).map_err(CliError::Serde)?; | ||
22 | |||
23 | if res.boot { | ||
24 | println!("successfully started {}", res.id); | ||
25 | } | ||
26 | |||
27 | Ok(()) | ||
28 | } | ||
29 | |||
30 | #[derive(Debug, Deserialize)] | ||
31 | struct StartResponse { | ||
32 | boot: bool, | ||
33 | id: String, | ||
34 | } | ||