diff options
author | fxqnlr <[email protected]> | 2024-06-18 14:38:48 +0200 |
---|---|---|
committer | fxqnlr <[email protected]> | 2024-06-18 14:38:48 +0200 |
commit | 3df6bc8f1a2ecec1313bd9b36ff7283f840b8308 (patch) | |
tree | f780cf73a1a41de6a9314c6e5a6d1fa07fb8c37a /src/main.rs | |
parent | b375657e660b199127a76683980a5d210a572ab7 (diff) | |
download | webol-cli-args.tar webol-cli-args.tar.gz webol-cli-args.zip |
add server and secret as argumentsargs
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 61 |
1 files changed, 7 insertions, 54 deletions
diff --git a/src/main.rs b/src/main.rs index 2726a5e..ccc0550 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -1,8 +1,8 @@ | |||
1 | use std::{fmt::Display, time::Duration}; | 1 | use std::{fmt::Display, time::Duration}; |
2 | 2 | ||
3 | use crate::config::Config; | 3 | use crate::{cli::print_completions, config::Config}; |
4 | use clap::{Command, CommandFactory, Parser, Subcommand}; | 4 | use clap::{CommandFactory, Parser}; |
5 | use clap_complete::{generate, Generator, Shell}; | 5 | use cli::{Args, Commands, DeviceCmd}; |
6 | use config::Method; | 6 | use config::Method; |
7 | use error::Error; | 7 | use error::Error; |
8 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; | 8 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; |
@@ -13,6 +13,7 @@ use reqwest::{ | |||
13 | }; | 13 | }; |
14 | use serde::Deserialize; | 14 | use serde::Deserialize; |
15 | 15 | ||
16 | mod cli; | ||
16 | mod config; | 17 | mod config; |
17 | mod error; | 18 | mod error; |
18 | mod requests; | 19 | mod requests; |
@@ -25,56 +26,14 @@ static DONE_STYLE: &str = " ✓ {wide_msg}"; | |||
25 | static ERROR_STYLE: &str = " ✗ {wide_msg}"; | 26 | static ERROR_STYLE: &str = " ✗ {wide_msg}"; |
26 | static TICK_SPEED: u64 = 1000 / 16; | 27 | static TICK_SPEED: u64 = 1000 / 16; |
27 | 28 | ||
28 | /// webol client | ||
29 | #[derive(Parser)] | ||
30 | #[command(author, version, about, long_about = None)] | ||
31 | struct Args { | ||
32 | #[command(subcommand)] | ||
33 | commands: Commands, | ||
34 | } | ||
35 | |||
36 | #[derive(Subcommand)] | ||
37 | enum Commands { | ||
38 | Start { | ||
39 | /// id of the device | ||
40 | id: String, | ||
41 | #[arg(short, long)] | ||
42 | ping: Option<bool>, | ||
43 | }, | ||
44 | Device { | ||
45 | #[command(subcommand)] | ||
46 | devicecmd: DeviceCmd, | ||
47 | }, | ||
48 | CliGen { | ||
49 | id: Shell, | ||
50 | }, | ||
51 | } | ||
52 | |||
53 | #[derive(Subcommand)] | ||
54 | enum DeviceCmd { | ||
55 | Add { | ||
56 | id: String, | ||
57 | mac: String, | ||
58 | broadcast_addr: String, | ||
59 | ip: String, | ||
60 | }, | ||
61 | Get { | ||
62 | id: String, | ||
63 | }, | ||
64 | Edit { | ||
65 | id: String, | ||
66 | mac: String, | ||
67 | broadcast_addr: String, | ||
68 | ip: String, | ||
69 | }, | ||
70 | } | ||
71 | |||
72 | #[tokio::main] | 29 | #[tokio::main] |
73 | async fn main() -> Result<(), anyhow::Error> { | 30 | async fn main() -> Result<(), anyhow::Error> { |
74 | let config = Config::load()?; | 31 | let mut config = Config::load()?; |
75 | 32 | ||
76 | let cli = Args::parse(); | 33 | let cli = Args::parse(); |
77 | 34 | ||
35 | config.cli_override(&cli); | ||
36 | |||
78 | match cli.commands { | 37 | match cli.commands { |
79 | Commands::Start { id, ping } => { | 38 | Commands::Start { id, ping } => { |
80 | start(&config, id, ping.unwrap_or(true)).await?; | 39 | start(&config, id, ping.unwrap_or(true)).await?; |
@@ -110,17 +69,12 @@ async fn main() -> Result<(), anyhow::Error> { | |||
110 | Ok(()) | 69 | Ok(()) |
111 | } | 70 | } |
112 | 71 | ||
113 | fn print_completions<G: Generator>(gen: G, cmd: &mut Command) { | ||
114 | generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout()); | ||
115 | } | ||
116 | |||
117 | fn default_headers(config: &Config) -> Result<HeaderMap, Error> { | 72 | fn default_headers(config: &Config) -> Result<HeaderMap, Error> { |
118 | let mut map = HeaderMap::new(); | 73 | let mut map = HeaderMap::new(); |
119 | map.append("Accept-Content", HeaderValue::from_str("application/json")?); | 74 | map.append("Accept-Content", HeaderValue::from_str("application/json")?); |
120 | map.append("Content-Type", HeaderValue::from_str("application/json")?); | 75 | map.append("Content-Type", HeaderValue::from_str("application/json")?); |
121 | if config.auth.method != Method::None { | 76 | if config.auth.method != Method::None { |
122 | map.append("Authorization", HeaderValue::from_str(&config.auth.secret)?); | 77 | map.append("Authorization", HeaderValue::from_str(&config.auth.secret)?); |
123 | |||
124 | } | 78 | } |
125 | 79 | ||
126 | Ok(map) | 80 | Ok(map) |
@@ -129,7 +83,6 @@ fn default_headers(config: &Config) -> Result<HeaderMap, Error> { | |||
129 | fn format_url(config: &Config, path: &str, protocol: &Protocols, id: Option<&str>) -> String { | 83 | fn format_url(config: &Config, path: &str, protocol: &Protocols, id: Option<&str>) -> String { |
130 | if let Some(id) = id { | 84 | if let Some(id) = id { |
131 | format!("{}://{}/{}/{}", protocol, config.server, path, id) | 85 | format!("{}://{}/{}/{}", protocol, config.server, path, id) |
132 | |||
133 | } else { | 86 | } else { |
134 | format!("{}://{}/{}", protocol, config.server, path) | 87 | format!("{}://{}/{}", protocol, config.server, path) |
135 | } | 88 | } |