diff options
author | FxQnLr <[email protected]> | 2023-11-17 11:18:31 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2023-11-17 11:18:31 +0100 |
commit | 48190366ac94888811dd4b0b8e6532b35f1a9d10 (patch) | |
tree | 8689660d75d0e45af460ed3d144b5a010289c0be /src/main.rs | |
parent | 8fab2e7c3a38a91c8f5549b639e7f2ac4ae1a420 (diff) | |
parent | 93de8742961287cb9cfd08e68c8afa2347585a73 (diff) | |
download | webol-cli-48190366ac94888811dd4b0b8e6532b35f1a9d10.tar webol-cli-48190366ac94888811dd4b0b8e6532b35f1a9d10.tar.gz webol-cli-48190366ac94888811dd4b0b8e6532b35f1a9d10.zip |
Merge pull request #1 from FxQnLr/eta
Eta
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 91 |
1 files changed, 75 insertions, 16 deletions
diff --git a/src/main.rs b/src/main.rs index ab7e476..afe6fac 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -1,6 +1,10 @@ | |||
1 | use clap::{Parser, Subcommand}; | 1 | use std::{fmt::Display, time::Duration}; |
2 | |||
3 | use clap::{Parser, Command, CommandFactory, Subcommand}; | ||
4 | use clap_complete::{generate, Shell, Generator}; | ||
2 | use config::SETTINGS; | 5 | use config::SETTINGS; |
3 | use error::CliError; | 6 | use error::CliError; |
7 | use indicatif::{ProgressBar, ProgressStyle, MultiProgress}; | ||
4 | use requests::{start::start, device}; | 8 | use requests::{start::start, device}; |
5 | use reqwest::header::{HeaderMap, HeaderValue}; | 9 | use reqwest::header::{HeaderMap, HeaderValue}; |
6 | use serde::Deserialize; | 10 | use serde::Deserialize; |
@@ -9,7 +13,15 @@ mod config; | |||
9 | mod error; | 13 | mod error; |
10 | mod requests; | 14 | mod requests; |
11 | 15 | ||
12 | /// webol http client | 16 | static OVERVIEW_STYLE: &str = "{spinner:.green} ({elapsed}{wide_msg}"; |
17 | static OVERVIEW_ERROR: &str = "✗ ({elapsed}) {wide_msg}"; | ||
18 | static OVERVIEW_DONE: &str = "✓ ({elapsed}) {wide_msg}"; | ||
19 | static DEFAULT_STYLE: &str = " {spinner:.green} {wide_msg}"; | ||
20 | static DONE_STYLE: &str = " ✓ {wide_msg}"; | ||
21 | static ERROR_STYLE: &str = " ✗ {wide_msg}"; | ||
22 | static TICK_SPEED: u64 = 1000 / 16; | ||
23 | |||
24 | /// webol client | ||
13 | #[derive(Parser)] | 25 | #[derive(Parser)] |
14 | #[command(author, version, about, long_about = None)] | 26 | #[command(author, version, about, long_about = None)] |
15 | struct Args { | 27 | struct Args { |
@@ -21,12 +33,17 @@ struct Args { | |||
21 | enum Commands { | 33 | enum Commands { |
22 | Start { | 34 | Start { |
23 | /// id of the device | 35 | /// id of the device |
24 | id: String | 36 | id: String, |
37 | #[arg(short, long)] | ||
38 | ping: Option<bool> | ||
25 | }, | 39 | }, |
26 | Device { | 40 | Device { |
27 | #[command(subcommand)] | 41 | #[command(subcommand)] |
28 | devicecmd: DeviceCmd, | 42 | devicecmd: DeviceCmd, |
29 | } | 43 | }, |
44 | CliGen { | ||
45 | id: Shell, | ||
46 | }, | ||
30 | } | 47 | } |
31 | 48 | ||
32 | #[derive(Subcommand)] | 49 | #[derive(Subcommand)] |
@@ -34,7 +51,8 @@ enum DeviceCmd { | |||
34 | Add { | 51 | Add { |
35 | id: String, | 52 | id: String, |
36 | mac: String, | 53 | mac: String, |
37 | broadcast_addr: String | 54 | broadcast_addr: String, |
55 | ip: String | ||
38 | }, | 56 | }, |
39 | Get { | 57 | Get { |
40 | id: String, | 58 | id: String, |
@@ -42,35 +60,46 @@ enum DeviceCmd { | |||
42 | Edit { | 60 | Edit { |
43 | id: String, | 61 | id: String, |
44 | mac: String, | 62 | mac: String, |
45 | broadcast_addr: String | 63 | broadcast_addr: String, |
64 | ip: String | ||
46 | }, | 65 | }, |
47 | } | 66 | } |
48 | 67 | ||
49 | fn main() -> Result<(), CliError> { | 68 | #[tokio::main] |
69 | async fn main() -> Result<(), CliError> { | ||
50 | let cli = Args::parse(); | 70 | let cli = Args::parse(); |
51 | 71 | ||
52 | match cli.commands { | 72 | match cli.commands { |
53 | Commands::Start { id } => { | 73 | Commands::Start { id, ping } => { |
54 | start(id)?; | 74 | start(id, ping.unwrap_or(true)).await?; |
55 | }, | 75 | }, |
56 | Commands::Device { devicecmd } => { | 76 | Commands::Device { devicecmd } => { |
57 | match devicecmd { | 77 | match devicecmd { |
58 | DeviceCmd::Add { id, mac, broadcast_addr } => { | 78 | DeviceCmd::Add { id, mac, broadcast_addr, ip } => { |
59 | device::put(id, mac, broadcast_addr)?; | 79 | device::put(id, mac, broadcast_addr, ip).await?; |
60 | }, | 80 | }, |
61 | DeviceCmd::Get { id } => { | 81 | DeviceCmd::Get { id } => { |
62 | device::get(id)?; | 82 | device::get(id).await?; |
63 | }, | 83 | }, |
64 | DeviceCmd::Edit { id, mac, broadcast_addr } => { | 84 | DeviceCmd::Edit { id, mac, broadcast_addr, ip } => { |
65 | device::post(id, mac, broadcast_addr)?; | 85 | device::post(id, mac, broadcast_addr, ip).await?; |
66 | }, | 86 | }, |
67 | } | 87 | } |
88 | }, | ||
89 | Commands::CliGen { id } => { | ||
90 | eprintln!("Generating completion file for {id:?}..."); | ||
91 | let mut cmd = Args::command(); | ||
92 | print_completions(id, &mut cmd) | ||
68 | } | 93 | } |
69 | } | 94 | } |
70 | 95 | ||
71 | Ok(()) | 96 | Ok(()) |
72 | } | 97 | } |
73 | 98 | ||
99 | fn print_completions<G: Generator>(gen: G, cmd: &mut Command) { | ||
100 | generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout()); | ||
101 | } | ||
102 | |||
74 | fn default_headers() -> Result<HeaderMap, CliError> { | 103 | fn default_headers() -> Result<HeaderMap, CliError> { |
75 | let mut map = HeaderMap::new(); | 104 | let mut map = HeaderMap::new(); |
76 | map.append("Accept-Content", HeaderValue::from_str("application/json").unwrap()); | 105 | map.append("Accept-Content", HeaderValue::from_str("application/json").unwrap()); |
@@ -87,14 +116,44 @@ fn default_headers() -> Result<HeaderMap, CliError> { | |||
87 | Ok(map) | 116 | Ok(map) |
88 | } | 117 | } |
89 | 118 | ||
90 | fn format_url(path: &str) -> Result<String, CliError> { | 119 | fn format_url(path: &str, protocol: Protocols) -> Result<String, CliError> { |
91 | Ok(format!( | 120 | Ok(format!( |
92 | "{}/{}", | 121 | "{}://{}/{}", |
122 | protocol, | ||
93 | SETTINGS.get_string("server").map_err(CliError::Config)?, | 123 | SETTINGS.get_string("server").map_err(CliError::Config)?, |
94 | path | 124 | path |
95 | )) | 125 | )) |
96 | } | 126 | } |
97 | 127 | ||
128 | fn add_pb(mp: &MultiProgress, template: &str, message: String) -> ProgressBar { | ||
129 | let pb = mp.add(ProgressBar::new(1)); | ||
130 | pb.set_style(ProgressStyle::with_template(template).unwrap()); | ||
131 | pb.enable_steady_tick(Duration::from_millis(TICK_SPEED)); | ||
132 | pb.set_message(message); | ||
133 | |||
134 | pb | ||
135 | } | ||
136 | |||
137 | fn finish_pb(pb: ProgressBar, message: String, template: &str) { | ||
138 | pb.set_style(ProgressStyle::with_template(template).unwrap()); | ||
139 | pb.finish_with_message(message); | ||
140 | |||
141 | } | ||
142 | |||
143 | enum Protocols { | ||
144 | Http, | ||
145 | Websocket, | ||
146 | } | ||
147 | |||
148 | impl Display for Protocols { | ||
149 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
150 | match self { | ||
151 | Self::Http => f.write_str("http"), | ||
152 | Self::Websocket => f.write_str("ws") | ||
153 | } | ||
154 | } | ||
155 | } | ||
156 | |||
98 | #[derive(Debug, Deserialize)] | 157 | #[derive(Debug, Deserialize)] |
99 | struct ErrorResponse { | 158 | struct ErrorResponse { |
100 | error: String | 159 | error: String |