summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2023-11-17 11:18:31 +0100
committerGitHub <[email protected]>2023-11-17 11:18:31 +0100
commit48190366ac94888811dd4b0b8e6532b35f1a9d10 (patch)
tree8689660d75d0e45af460ed3d144b5a010289c0be /src/main.rs
parent8fab2e7c3a38a91c8f5549b639e7f2ac4ae1a420 (diff)
parent93de8742961287cb9cfd08e68c8afa2347585a73 (diff)
downloadwebol-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.rs91
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 @@
1use clap::{Parser, Subcommand}; 1use std::{fmt::Display, time::Duration};
2
3use clap::{Parser, Command, CommandFactory, Subcommand};
4use clap_complete::{generate, Shell, Generator};
2use config::SETTINGS; 5use config::SETTINGS;
3use error::CliError; 6use error::CliError;
7use indicatif::{ProgressBar, ProgressStyle, MultiProgress};
4use requests::{start::start, device}; 8use requests::{start::start, device};
5use reqwest::header::{HeaderMap, HeaderValue}; 9use reqwest::header::{HeaderMap, HeaderValue};
6use serde::Deserialize; 10use serde::Deserialize;
@@ -9,7 +13,15 @@ mod config;
9mod error; 13mod error;
10mod requests; 14mod requests;
11 15
12/// webol http client 16static OVERVIEW_STYLE: &str = "{spinner:.green} ({elapsed}{wide_msg}";
17static OVERVIEW_ERROR: &str = "✗ ({elapsed}) {wide_msg}";
18static OVERVIEW_DONE: &str = "✓ ({elapsed}) {wide_msg}";
19static DEFAULT_STYLE: &str = " {spinner:.green} {wide_msg}";
20static DONE_STYLE: &str = " ✓ {wide_msg}";
21static ERROR_STYLE: &str = " ✗ {wide_msg}";
22static 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)]
15struct Args { 27struct Args {
@@ -21,12 +33,17 @@ struct Args {
21enum Commands { 33enum 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
49fn main() -> Result<(), CliError> { 68#[tokio::main]
69async 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
99fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
100 generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout());
101}
102
74fn default_headers() -> Result<HeaderMap, CliError> { 103fn 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
90fn format_url(path: &str) -> Result<String, CliError> { 119fn 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
128fn 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
137fn 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
143enum Protocols {
144 Http,
145 Websocket,
146}
147
148impl 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)]
99struct ErrorResponse { 158struct ErrorResponse {
100 error: String 159 error: String