summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfx <[email protected]>2023-10-21 17:43:31 +0200
committerfx <[email protected]>2023-10-21 17:43:31 +0200
commit8fab2e7c3a38a91c8f5549b639e7f2ac4ae1a420 (patch)
tree09a886de17d8dc5e36d89068828380e936b47650
parenteb0a092fc53964e02a09da2d92a1f8a3042a1360 (diff)
downloadwebol-cli-8fab2e7c3a38a91c8f5549b639e7f2ac4ae1a420.tar
webol-cli-8fab2e7c3a38a91c8f5549b639e7f2ac4ae1a420.tar.gz
webol-cli-8fab2e7c3a38a91c8f5549b639e7f2ac4ae1a420.zip
added device add and update
-rw-r--r--Cargo.lock32
-rw-r--r--src/main.rs45
-rw-r--r--src/requests/device.rs56
-rw-r--r--src/requests/get.rs21
-rw-r--r--src/requests/mod.rs2
5 files changed, 113 insertions, 43 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 57bd864..b2c6d30 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -251,9 +251,9 @@ checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa"
251 251
252[[package]] 252[[package]]
253name = "cpufeatures" 253name = "cpufeatures"
254version = "0.2.9" 254version = "0.2.10"
255source = "registry+https://github.com/rust-lang/crates.io-index" 255source = "registry+https://github.com/rust-lang/crates.io-index"
256checksum = "a17b76ff3a4162b0b27f354a0c87015ddad39d35f9c0c36607a3bdd175dde1f1" 256checksum = "3fbc60abd742b35f2492f808e1abbb83d45f72db402e14c55057edc9c7b1e9e4"
257dependencies = [ 257dependencies = [
258 "libc", 258 "libc",
259] 259]
@@ -526,7 +526,7 @@ dependencies = [
526 "httpdate", 526 "httpdate",
527 "itoa", 527 "itoa",
528 "pin-project-lite", 528 "pin-project-lite",
529 "socket2 0.4.9", 529 "socket2 0.4.10",
530 "tokio", 530 "tokio",
531 "tower-service", 531 "tower-service",
532 "tracing", 532 "tracing",
@@ -968,9 +968,9 @@ checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
968 968
969[[package]] 969[[package]]
970name = "rustix" 970name = "rustix"
971version = "0.38.19" 971version = "0.38.20"
972source = "registry+https://github.com/rust-lang/crates.io-index" 972source = "registry+https://github.com/rust-lang/crates.io-index"
973checksum = "745ecfa778e66b2b63c88a61cb36e0eea109e803b0b86bf9879fbc77c70e86ed" 973checksum = "67ce50cb2e16c2903e30d1cbccfd8387a74b9d4c938b6a4c5ec6cc7556f7a8a0"
974dependencies = [ 974dependencies = [
975 "bitflags 2.4.1", 975 "bitflags 2.4.1",
976 "errno", 976 "errno",
@@ -1082,9 +1082,9 @@ dependencies = [
1082 1082
1083[[package]] 1083[[package]]
1084name = "socket2" 1084name = "socket2"
1085version = "0.4.9" 1085version = "0.4.10"
1086source = "registry+https://github.com/rust-lang/crates.io-index" 1086source = "registry+https://github.com/rust-lang/crates.io-index"
1087checksum = "64a4a911eed85daf18834cfaa86a79b7d266ff93ff5ba14005426219480ed662" 1087checksum = "9f7916fc008ca5542385b89a3d3ce689953c143e9304a9bf8beec1de48994c0d"
1088dependencies = [ 1088dependencies = [
1089 "libc", 1089 "libc",
1090 "winapi", 1090 "winapi",
@@ -1092,9 +1092,9 @@ dependencies = [
1092 1092
1093[[package]] 1093[[package]]
1094name = "socket2" 1094name = "socket2"
1095version = "0.5.4" 1095version = "0.5.5"
1096source = "registry+https://github.com/rust-lang/crates.io-index" 1096source = "registry+https://github.com/rust-lang/crates.io-index"
1097checksum = "4031e820eb552adee9295814c0ced9e5cf38ddf1e8b7d566d6de8e2538ea989e" 1097checksum = "7b5fac59a5cb5dd637972e5fca70daf0523c9067fcdc4842f053dae04a18f8e9"
1098dependencies = [ 1098dependencies = [
1099 "libc", 1099 "libc",
1100 "windows-sys", 1100 "windows-sys",
@@ -1153,18 +1153,18 @@ dependencies = [
1153 1153
1154[[package]] 1154[[package]]
1155name = "thiserror" 1155name = "thiserror"
1156version = "1.0.49" 1156version = "1.0.50"
1157source = "registry+https://github.com/rust-lang/crates.io-index" 1157source = "registry+https://github.com/rust-lang/crates.io-index"
1158checksum = "1177e8c6d7ede7afde3585fd2513e611227efd6481bd78d2e82ba1ce16557ed4" 1158checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2"
1159dependencies = [ 1159dependencies = [
1160 "thiserror-impl", 1160 "thiserror-impl",
1161] 1161]
1162 1162
1163[[package]] 1163[[package]]
1164name = "thiserror-impl" 1164name = "thiserror-impl"
1165version = "1.0.49" 1165version = "1.0.50"
1166source = "registry+https://github.com/rust-lang/crates.io-index" 1166source = "registry+https://github.com/rust-lang/crates.io-index"
1167checksum = "10712f02019e9288794769fba95cd6847df9874d49d871d062172f9dd41bc4cc" 1167checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8"
1168dependencies = [ 1168dependencies = [
1169 "proc-macro2", 1169 "proc-macro2",
1170 "quote", 1170 "quote",
@@ -1198,7 +1198,7 @@ dependencies = [
1198 "mio", 1198 "mio",
1199 "num_cpus", 1199 "num_cpus",
1200 "pin-project-lite", 1200 "pin-project-lite",
1201 "socket2 0.5.4", 1201 "socket2 0.5.5",
1202 "windows-sys", 1202 "windows-sys",
1203] 1203]
1204 1204
@@ -1243,9 +1243,9 @@ checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52"
1243 1243
1244[[package]] 1244[[package]]
1245name = "tracing" 1245name = "tracing"
1246version = "0.1.39" 1246version = "0.1.40"
1247source = "registry+https://github.com/rust-lang/crates.io-index" 1247source = "registry+https://github.com/rust-lang/crates.io-index"
1248checksum = "ee2ef2af84856a50c1d430afce2fdded0a4ec7eda868db86409b4543df0797f9" 1248checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef"
1249dependencies = [ 1249dependencies = [
1250 "pin-project-lite", 1250 "pin-project-lite",
1251 "tracing-core", 1251 "tracing-core",
diff --git a/src/main.rs b/src/main.rs
index e6ca3ef..ab7e476 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,7 @@
1use clap::{Parser, Subcommand}; 1use clap::{Parser, Subcommand};
2use config::SETTINGS; 2use config::SETTINGS;
3use error::CliError; 3use error::CliError;
4use requests::{start::start, get::get}; 4use requests::{start::start, device};
5use reqwest::header::{HeaderMap, HeaderValue}; 5use reqwest::header::{HeaderMap, HeaderValue};
6use serde::Deserialize; 6use serde::Deserialize;
7 7
@@ -23,11 +23,29 @@ enum Commands {
23 /// id of the device 23 /// id of the device
24 id: String 24 id: String
25 }, 25 },
26 Get { 26 Device {
27 id: String 27 #[command(subcommand)]
28 devicecmd: DeviceCmd,
28 } 29 }
29} 30}
30 31
32#[derive(Subcommand)]
33enum DeviceCmd {
34 Add {
35 id: String,
36 mac: String,
37 broadcast_addr: String
38 },
39 Get {
40 id: String,
41 },
42 Edit {
43 id: String,
44 mac: String,
45 broadcast_addr: String
46 },
47}
48
31fn main() -> Result<(), CliError> { 49fn main() -> Result<(), CliError> {
32 let cli = Args::parse(); 50 let cli = Args::parse();
33 51
@@ -35,8 +53,18 @@ fn main() -> Result<(), CliError> {
35 Commands::Start { id } => { 53 Commands::Start { id } => {
36 start(id)?; 54 start(id)?;
37 }, 55 },
38 Commands::Get { id } => { 56 Commands::Device { devicecmd } => {
39 get(id)?; 57 match devicecmd {
58 DeviceCmd::Add { id, mac, broadcast_addr } => {
59 device::put(id, mac, broadcast_addr)?;
60 },
61 DeviceCmd::Get { id } => {
62 device::get(id)?;
63 },
64 DeviceCmd::Edit { id, mac, broadcast_addr } => {
65 device::post(id, mac, broadcast_addr)?;
66 },
67 }
40 } 68 }
41 } 69 }
42 70
@@ -57,7 +85,14 @@ fn default_headers() -> Result<HeaderMap, CliError> {
57 ); 85 );
58 86
59 Ok(map) 87 Ok(map)
88}
60 89
90fn format_url(path: &str) -> Result<String, CliError> {
91 Ok(format!(
92 "{}/{}",
93 SETTINGS.get_string("server").map_err(CliError::Config)?,
94 path
95 ))
61} 96}
62 97
63#[derive(Debug, Deserialize)] 98#[derive(Debug, Deserialize)]
diff --git a/src/requests/device.rs b/src/requests/device.rs
new file mode 100644
index 0000000..525745a
--- /dev/null
+++ b/src/requests/device.rs
@@ -0,0 +1,56 @@
1use crate::{error::CliError, default_headers, format_url};
2
3pub fn put(id: String, mac: String, broadcast_addr: String) -> Result<(), CliError> {
4 let res = reqwest::blocking::Client::new()
5 .put(format_url("device")?)
6 .headers(default_headers()?)
7 .body(
8 format!(
9 r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}"}}"#,
10 id,
11 mac,
12 broadcast_addr
13 )
14 )
15 .send()
16 .map_err(CliError::Reqwest)?
17 .text();
18
19 println!("{:?}", res);
20 Ok(())
21}
22
23pub fn get(id: String) -> Result<(), CliError> {
24 let res = reqwest::blocking::Client::new()
25 .get(format_url("device")?)
26 .headers(default_headers()?)
27 .body(
28 format!(r#"{{"id": "{}"}}"#, id)
29 )
30 .send()
31 .map_err(CliError::Reqwest)?
32 .text();
33
34 println!("{:?}", res);
35 Ok(())
36}
37
38pub fn post(id: String, mac: String, broadcast_addr: String) -> Result<(), CliError> {
39 let res = reqwest::blocking::Client::new()
40 .post(format_url("device")?)
41 .headers(default_headers()?)
42 .body(
43 format!(
44 r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}"}}"#,
45 id,
46 mac,
47 broadcast_addr
48 )
49 )
50 .send()
51 .map_err(CliError::Reqwest)?
52 .text();
53
54 println!("{:?}", res);
55 Ok(())
56}
diff --git a/src/requests/get.rs b/src/requests/get.rs
deleted file mode 100644
index 225562d..0000000
--- a/src/requests/get.rs
+++ /dev/null
@@ -1,21 +0,0 @@
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
index ddf0ea9..6855db1 100644
--- a/src/requests/mod.rs
+++ b/src/requests/mod.rs
@@ -1,2 +1,2 @@
1pub mod start; 1pub mod start;
2pub mod get; 2pub mod device;