summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorfx <[email protected]>2023-10-18 15:11:44 +0200
committerfx <[email protected]>2023-10-18 15:11:44 +0200
commitb4f59c226c6916a3e45f1a52dc6a9b15c800297a (patch)
tree07016e2ac7278da6b63aa839065c5ad572215e50 /src
downloadwebol-cli-b4f59c226c6916a3e45f1a52dc6a9b15c800297a.tar
webol-cli-b4f59c226c6916a3e45f1a52dc6a9b15c800297a.tar.gz
webol-cli-b4f59c226c6916a3e45f1a52dc6a9b15c800297a.zip
basic cli, only start and get device
Diffstat (limited to 'src')
-rw-r--r--src/config.rs19
-rw-r--r--src/error.rs19
-rw-r--r--src/main.rs60
-rw-r--r--src/requests/get.rs21
-rw-r--r--src/requests/mod.rs2
-rw-r--r--src/requests/start.rs34
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 @@
1use config::Config;
2use once_cell::sync::Lazy;
3
4pub static SETTINGS: Lazy<Config> = Lazy::new(setup);
5
6fn 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 @@
1use std::fmt::Debug;
2
3pub enum CliError {
4 Reqwest(reqwest::Error),
5 Config(config::ConfigError),
6 Serde(serde_json::Error),
7 // Cli(Box<dyn std::error::Error>),
8}
9
10impl 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 @@
1use clap::{Parser, Subcommand};
2use config::SETTINGS;
3use error::CliError;
4use requests::{start::start, get::get};
5use reqwest::header::{HeaderMap, HeaderValue};
6
7mod config;
8mod error;
9mod requests;
10
11/// webol http client
12#[derive(Parser)]
13#[command(author, version, about, long_about = None)]
14struct Args {
15 #[command(subcommand)]
16 commands: Commands,
17}
18
19#[derive(Subcommand)]
20enum Commands {
21 Start {
22 /// id of the device
23 id: String
24 },
25 Get {
26 id: String
27 }
28}
29
30fn 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
45fn 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 @@
1use crate::{error::CliError, config::SETTINGS, default_headers};
2
3pub 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 @@
1pub mod start;
2pub 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 @@
1use serde::Deserialize;
2
3use crate::{config::SETTINGS, error::CliError, default_headers};
4
5pub 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)]
31struct StartResponse {
32 boot: bool,
33 id: String,
34}