diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 84 |
1 files changed, 45 insertions, 39 deletions
diff --git a/src/main.rs b/src/main.rs index afe6fac..0393183 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -1,11 +1,11 @@ | |||
1 | use std::{fmt::Display, time::Duration}; | 1 | use std::{fmt::Display, time::Duration}; |
2 | 2 | ||
3 | use clap::{Parser, Command, CommandFactory, Subcommand}; | 3 | use crate::config::Config; |
4 | use clap_complete::{generate, Shell, Generator}; | 4 | use clap::{Command, CommandFactory, Parser, Subcommand}; |
5 | use config::SETTINGS; | 5 | use clap_complete::{generate, Generator, Shell}; |
6 | use error::CliError; | 6 | use error::CliError; |
7 | use indicatif::{ProgressBar, ProgressStyle, MultiProgress}; | 7 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; |
8 | use requests::{start::start, device}; | 8 | use requests::{device, start::start}; |
9 | use reqwest::header::{HeaderMap, HeaderValue}; | 9 | use reqwest::header::{HeaderMap, HeaderValue}; |
10 | use serde::Deserialize; | 10 | use serde::Deserialize; |
11 | 11 | ||
@@ -35,7 +35,7 @@ enum Commands { | |||
35 | /// id of the device | 35 | /// id of the device |
36 | id: String, | 36 | id: String, |
37 | #[arg(short, long)] | 37 | #[arg(short, long)] |
38 | ping: Option<bool> | 38 | ping: Option<bool>, |
39 | }, | 39 | }, |
40 | Device { | 40 | Device { |
41 | #[command(subcommand)] | 41 | #[command(subcommand)] |
@@ -52,7 +52,7 @@ enum DeviceCmd { | |||
52 | id: String, | 52 | id: String, |
53 | mac: String, | 53 | mac: String, |
54 | broadcast_addr: String, | 54 | broadcast_addr: String, |
55 | ip: String | 55 | ip: String, |
56 | }, | 56 | }, |
57 | Get { | 57 | Get { |
58 | id: String, | 58 | id: String, |
@@ -61,29 +61,39 @@ enum DeviceCmd { | |||
61 | id: String, | 61 | id: String, |
62 | mac: String, | 62 | mac: String, |
63 | broadcast_addr: String, | 63 | broadcast_addr: String, |
64 | ip: String | 64 | ip: String, |
65 | }, | 65 | }, |
66 | } | 66 | } |
67 | 67 | ||
68 | #[tokio::main] | 68 | #[tokio::main] |
69 | async fn main() -> Result<(), CliError> { | 69 | async fn main() -> Result<(), CliError> { |
70 | let config = Config::load().map_err(CliError::Config)?; | ||
71 | |||
70 | let cli = Args::parse(); | 72 | let cli = Args::parse(); |
71 | 73 | ||
72 | match cli.commands { | 74 | match cli.commands { |
73 | Commands::Start { id, ping } => { | 75 | Commands::Start { id, ping } => { |
74 | start(id, ping.unwrap_or(true)).await?; | 76 | start(&config, id, ping.unwrap_or(true)).await?; |
75 | }, | 77 | } |
76 | Commands::Device { devicecmd } => { | 78 | Commands::Device { devicecmd } => match devicecmd { |
77 | match devicecmd { | 79 | DeviceCmd::Add { |
78 | DeviceCmd::Add { id, mac, broadcast_addr, ip } => { | 80 | id, |
79 | device::put(id, mac, broadcast_addr, ip).await?; | 81 | mac, |
80 | }, | 82 | broadcast_addr, |
81 | DeviceCmd::Get { id } => { | 83 | ip, |
82 | device::get(id).await?; | 84 | } => { |
83 | }, | 85 | device::put(&config, id, mac, broadcast_addr, ip).await?; |
84 | DeviceCmd::Edit { id, mac, broadcast_addr, ip } => { | 86 | } |
85 | device::post(id, mac, broadcast_addr, ip).await?; | 87 | DeviceCmd::Get { id } => { |
86 | }, | 88 | device::get(&config, id).await?; |
89 | } | ||
90 | DeviceCmd::Edit { | ||
91 | id, | ||
92 | mac, | ||
93 | broadcast_addr, | ||
94 | ip, | ||
95 | } => { | ||
96 | device::post(&config, id, mac, broadcast_addr, ip).await?; | ||
87 | } | 97 | } |
88 | }, | 98 | }, |
89 | Commands::CliGen { id } => { | 99 | Commands::CliGen { id } => { |
@@ -100,29 +110,26 @@ fn print_completions<G: Generator>(gen: G, cmd: &mut Command) { | |||
100 | generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout()); | 110 | generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout()); |
101 | } | 111 | } |
102 | 112 | ||
103 | fn default_headers() -> Result<HeaderMap, CliError> { | 113 | fn default_headers(config: &Config) -> Result<HeaderMap, CliError> { |
104 | let mut map = HeaderMap::new(); | 114 | let mut map = HeaderMap::new(); |
105 | map.append("Accept-Content", HeaderValue::from_str("application/json").unwrap()); | 115 | map.append( |
106 | map.append("Content-Type", HeaderValue::from_str("application/json").unwrap()); | 116 | "Accept-Content", |
117 | HeaderValue::from_str("application/json").unwrap(), | ||
118 | ); | ||
119 | map.append( | ||
120 | "Content-Type", | ||
121 | HeaderValue::from_str("application/json").unwrap(), | ||
122 | ); | ||
107 | map.append( | 123 | map.append( |
108 | "Authorization", | 124 | "Authorization", |
109 | HeaderValue::from_str( | 125 | HeaderValue::from_str(&config.apikey).unwrap(), |
110 | SETTINGS.get_string("key") | ||
111 | .map_err(CliError::Config)? | ||
112 | .as_str() | ||
113 | ).unwrap() | ||
114 | ); | 126 | ); |
115 | 127 | ||
116 | Ok(map) | 128 | Ok(map) |
117 | } | 129 | } |
118 | 130 | ||
119 | fn format_url(path: &str, protocol: Protocols) -> Result<String, CliError> { | 131 | fn format_url(config: &Config, path: &str, protocol: Protocols) -> Result<String, CliError> { |
120 | Ok(format!( | 132 | Ok(format!("{}://{}/{}", protocol, config.server, path)) |
121 | "{}://{}/{}", | ||
122 | protocol, | ||
123 | SETTINGS.get_string("server").map_err(CliError::Config)?, | ||
124 | path | ||
125 | )) | ||
126 | } | 133 | } |
127 | 134 | ||
128 | fn add_pb(mp: &MultiProgress, template: &str, message: String) -> ProgressBar { | 135 | fn add_pb(mp: &MultiProgress, template: &str, message: String) -> ProgressBar { |
@@ -137,7 +144,6 @@ fn add_pb(mp: &MultiProgress, template: &str, message: String) -> ProgressBar { | |||
137 | fn finish_pb(pb: ProgressBar, message: String, template: &str) { | 144 | fn finish_pb(pb: ProgressBar, message: String, template: &str) { |
138 | pb.set_style(ProgressStyle::with_template(template).unwrap()); | 145 | pb.set_style(ProgressStyle::with_template(template).unwrap()); |
139 | pb.finish_with_message(message); | 146 | pb.finish_with_message(message); |
140 | |||
141 | } | 147 | } |
142 | 148 | ||
143 | enum Protocols { | 149 | enum Protocols { |
@@ -149,12 +155,12 @@ impl Display for Protocols { | |||
149 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | 155 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
150 | match self { | 156 | match self { |
151 | Self::Http => f.write_str("http"), | 157 | Self::Http => f.write_str("http"), |
152 | Self::Websocket => f.write_str("ws") | 158 | Self::Websocket => f.write_str("ws"), |
153 | } | 159 | } |
154 | } | 160 | } |
155 | } | 161 | } |
156 | 162 | ||
157 | #[derive(Debug, Deserialize)] | 163 | #[derive(Debug, Deserialize)] |
158 | struct ErrorResponse { | 164 | struct ErrorResponse { |
159 | error: String | 165 | error: String, |
160 | } | 166 | } |