summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs48
1 files changed, 35 insertions, 13 deletions
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 @@
1use std::fmt::Display;
2
1use clap::{Parser, Subcommand}; 3use clap::{Parser, Subcommand};
2use config::SETTINGS; 4use config::SETTINGS;
3use error::CliError; 5use error::CliError;
@@ -21,7 +23,9 @@ struct Args {
21enum Commands { 23enum Commands {
22 Start { 24 Start {
23 /// id of the device 25 /// id of the device
24 id: String 26 id: String,
27 #[arg(short, long)]
28 ping: Option<bool>
25 }, 29 },
26 Device { 30 Device {
27 #[command(subcommand)] 31 #[command(subcommand)]
@@ -34,7 +38,8 @@ enum DeviceCmd {
34 Add { 38 Add {
35 id: String, 39 id: String,
36 mac: String, 40 mac: String,
37 broadcast_addr: String 41 broadcast_addr: String,
42 ip: String
38 }, 43 },
39 Get { 44 Get {
40 id: String, 45 id: String,
@@ -42,27 +47,29 @@ enum DeviceCmd {
42 Edit { 47 Edit {
43 id: String, 48 id: String,
44 mac: String, 49 mac: String,
45 broadcast_addr: String 50 broadcast_addr: String,
51 ip: String
46 }, 52 },
47} 53}
48 54
49fn main() -> Result<(), CliError> { 55#[tokio::main]
56async fn main() -> Result<(), CliError> {
50 let cli = Args::parse(); 57 let cli = Args::parse();
51 58
52 match cli.commands { 59 match cli.commands {
53 Commands::Start { id } => { 60 Commands::Start { id, ping } => {
54 start(id)?; 61 start(id, ping.unwrap_or(true)).await?;
55 }, 62 },
56 Commands::Device { devicecmd } => { 63 Commands::Device { devicecmd } => {
57 match devicecmd { 64 match devicecmd {
58 DeviceCmd::Add { id, mac, broadcast_addr } => { 65 DeviceCmd::Add { id, mac, broadcast_addr, ip } => {
59 device::put(id, mac, broadcast_addr)?; 66 device::put(id, mac, broadcast_addr, ip).await?;
60 }, 67 },
61 DeviceCmd::Get { id } => { 68 DeviceCmd::Get { id } => {
62 device::get(id)?; 69 device::get(id).await?;
63 }, 70 },
64 DeviceCmd::Edit { id, mac, broadcast_addr } => { 71 DeviceCmd::Edit { id, mac, broadcast_addr, ip } => {
65 device::post(id, mac, broadcast_addr)?; 72 device::post(id, mac, broadcast_addr, ip).await?;
66 }, 73 },
67 } 74 }
68 } 75 }
@@ -87,14 +94,29 @@ fn default_headers() -> Result<HeaderMap, CliError> {
87 Ok(map) 94 Ok(map)
88} 95}
89 96
90fn format_url(path: &str) -> Result<String, CliError> { 97fn format_url(path: &str, protocol: Protocols) -> Result<String, CliError> {
91 Ok(format!( 98 Ok(format!(
92 "{}/{}", 99 "{}://{}/{}",
100 protocol,
93 SETTINGS.get_string("server").map_err(CliError::Config)?, 101 SETTINGS.get_string("server").map_err(CliError::Config)?,
94 path 102 path
95 )) 103 ))
96} 104}
97 105
106enum Protocols {
107 Http,
108 Websocket,
109}
110
111impl Display for Protocols {
112 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113 match self {
114 Self::Http => f.write_str("http"),
115 Self::Websocket => f.write_str("ws")
116 }
117 }
118}
119
98#[derive(Debug, Deserialize)] 120#[derive(Debug, Deserialize)]
99struct ErrorResponse { 121struct ErrorResponse {
100 error: String 122 error: String