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 | |
parent | b375657e660b199127a76683980a5d210a572ab7 (diff) | |
download | webol-cli-args.tar webol-cli-args.tar.gz webol-cli-args.zip |
add server and secret as argumentsargs
-rw-r--r-- | Cargo.lock | 108 | ||||
-rw-r--r-- | src/cli.rs | 56 | ||||
-rw-r--r-- | src/config.rs | 12 | ||||
-rw-r--r-- | src/main.rs | 61 |
4 files changed, 175 insertions, 62 deletions
@@ -626,9 +626,9 @@ dependencies = [ | |||
626 | 626 | ||
627 | [[package]] | 627 | [[package]] |
628 | name = "httparse" | 628 | name = "httparse" |
629 | version = "1.9.3" | 629 | version = "1.9.4" |
630 | source = "registry+https://github.com/rust-lang/crates.io-index" | 630 | source = "registry+https://github.com/rust-lang/crates.io-index" |
631 | checksum = "d0e7a4dd27b9476dc40cb050d3632d3bba3a70ddbff012285f7f8559a1e7e545" | 631 | checksum = "0fcc0b4a115bf80b728eb8ea024ad5bd707b615bfed49e0665b6e0f86fd082d9" |
632 | 632 | ||
633 | [[package]] | 633 | [[package]] |
634 | name = "hyper" | 634 | name = "hyper" |
@@ -651,6 +651,23 @@ dependencies = [ | |||
651 | ] | 651 | ] |
652 | 652 | ||
653 | [[package]] | 653 | [[package]] |
654 | name = "hyper-rustls" | ||
655 | version = "0.27.2" | ||
656 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
657 | checksum = "5ee4be2c948921a1a5320b629c4193916ed787a7f7f293fd3f7f5a6c9de74155" | ||
658 | dependencies = [ | ||
659 | "futures-util", | ||
660 | "http", | ||
661 | "hyper", | ||
662 | "hyper-util", | ||
663 | "rustls", | ||
664 | "rustls-pki-types", | ||
665 | "tokio", | ||
666 | "tokio-rustls", | ||
667 | "tower-service", | ||
668 | ] | ||
669 | |||
670 | [[package]] | ||
654 | name = "hyper-tls" | 671 | name = "hyper-tls" |
655 | version = "0.6.0" | 672 | version = "0.6.0" |
656 | source = "registry+https://github.com/rust-lang/crates.io-index" | 673 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -952,9 +969,9 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" | |||
952 | 969 | ||
953 | [[package]] | 970 | [[package]] |
954 | name = "miniz_oxide" | 971 | name = "miniz_oxide" |
955 | version = "0.7.3" | 972 | version = "0.7.4" |
956 | source = "registry+https://github.com/rust-lang/crates.io-index" | 973 | source = "registry+https://github.com/rust-lang/crates.io-index" |
957 | checksum = "87dfd01fe195c66b572b37921ad8803d010623c0aca821bea2302239d155cdae" | 974 | checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" |
958 | dependencies = [ | 975 | dependencies = [ |
959 | "adler", | 976 | "adler", |
960 | ] | 977 | ] |
@@ -1256,9 +1273,9 @@ dependencies = [ | |||
1256 | 1273 | ||
1257 | [[package]] | 1274 | [[package]] |
1258 | name = "reqwest" | 1275 | name = "reqwest" |
1259 | version = "0.12.4" | 1276 | version = "0.12.5" |
1260 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1277 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1261 | checksum = "566cafdd92868e0939d3fb961bd0dc25fcfaaed179291093b3d43e6b3150ea10" | 1278 | checksum = "c7d6d2a27d57148378eb5e111173f4276ad26340ecc5c49a4a2152167a2d6a37" |
1262 | dependencies = [ | 1279 | dependencies = [ |
1263 | "base64 0.22.1", | 1280 | "base64 0.22.1", |
1264 | "bytes", | 1281 | "bytes", |
@@ -1271,6 +1288,7 @@ dependencies = [ | |||
1271 | "http-body", | 1288 | "http-body", |
1272 | "http-body-util", | 1289 | "http-body-util", |
1273 | "hyper", | 1290 | "hyper", |
1291 | "hyper-rustls", | ||
1274 | "hyper-tls", | 1292 | "hyper-tls", |
1275 | "hyper-util", | 1293 | "hyper-util", |
1276 | "ipnet", | 1294 | "ipnet", |
@@ -1298,6 +1316,21 @@ dependencies = [ | |||
1298 | ] | 1316 | ] |
1299 | 1317 | ||
1300 | [[package]] | 1318 | [[package]] |
1319 | name = "ring" | ||
1320 | version = "0.17.8" | ||
1321 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1322 | checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d" | ||
1323 | dependencies = [ | ||
1324 | "cc", | ||
1325 | "cfg-if", | ||
1326 | "getrandom", | ||
1327 | "libc", | ||
1328 | "spin", | ||
1329 | "untrusted", | ||
1330 | "windows-sys 0.52.0", | ||
1331 | ] | ||
1332 | |||
1333 | [[package]] | ||
1301 | name = "ron" | 1334 | name = "ron" |
1302 | version = "0.8.1" | 1335 | version = "0.8.1" |
1303 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1336 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -1339,6 +1372,19 @@ dependencies = [ | |||
1339 | ] | 1372 | ] |
1340 | 1373 | ||
1341 | [[package]] | 1374 | [[package]] |
1375 | name = "rustls" | ||
1376 | version = "0.23.10" | ||
1377 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1378 | checksum = "05cff451f60db80f490f3c182b77c35260baace73209e9cdbbe526bfe3a4d402" | ||
1379 | dependencies = [ | ||
1380 | "once_cell", | ||
1381 | "rustls-pki-types", | ||
1382 | "rustls-webpki", | ||
1383 | "subtle", | ||
1384 | "zeroize", | ||
1385 | ] | ||
1386 | |||
1387 | [[package]] | ||
1342 | name = "rustls-pemfile" | 1388 | name = "rustls-pemfile" |
1343 | version = "2.1.2" | 1389 | version = "2.1.2" |
1344 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1390 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -1355,6 +1401,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
1355 | checksum = "976295e77ce332211c0d24d92c0e83e50f5c5f046d11082cea19f3df13a3562d" | 1401 | checksum = "976295e77ce332211c0d24d92c0e83e50f5c5f046d11082cea19f3df13a3562d" |
1356 | 1402 | ||
1357 | [[package]] | 1403 | [[package]] |
1404 | name = "rustls-webpki" | ||
1405 | version = "0.102.4" | ||
1406 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1407 | checksum = "ff448f7e92e913c4b7d4c6d8e4540a1724b319b4152b8aef6d4cf8339712b33e" | ||
1408 | dependencies = [ | ||
1409 | "ring", | ||
1410 | "rustls-pki-types", | ||
1411 | "untrusted", | ||
1412 | ] | ||
1413 | |||
1414 | [[package]] | ||
1358 | name = "ryu" | 1415 | name = "ryu" |
1359 | version = "1.0.18" | 1416 | version = "1.0.18" |
1360 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1417 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -1492,6 +1549,12 @@ dependencies = [ | |||
1492 | ] | 1549 | ] |
1493 | 1550 | ||
1494 | [[package]] | 1551 | [[package]] |
1552 | name = "spin" | ||
1553 | version = "0.9.8" | ||
1554 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1555 | checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" | ||
1556 | |||
1557 | [[package]] | ||
1495 | name = "stable_deref_trait" | 1558 | name = "stable_deref_trait" |
1496 | version = "1.2.0" | 1559 | version = "1.2.0" |
1497 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1560 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -1504,6 +1567,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
1504 | checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" | 1567 | checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" |
1505 | 1568 | ||
1506 | [[package]] | 1569 | [[package]] |
1570 | name = "subtle" | ||
1571 | version = "2.5.0" | ||
1572 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1573 | checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" | ||
1574 | |||
1575 | [[package]] | ||
1507 | name = "syn" | 1576 | name = "syn" |
1508 | version = "2.0.66" | 1577 | version = "2.0.66" |
1509 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1578 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -1516,9 +1585,9 @@ dependencies = [ | |||
1516 | 1585 | ||
1517 | [[package]] | 1586 | [[package]] |
1518 | name = "sync_wrapper" | 1587 | name = "sync_wrapper" |
1519 | version = "0.1.2" | 1588 | version = "1.0.1" |
1520 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1589 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1521 | checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" | 1590 | checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394" |
1522 | 1591 | ||
1523 | [[package]] | 1592 | [[package]] |
1524 | name = "synstructure" | 1593 | name = "synstructure" |
@@ -1642,6 +1711,17 @@ dependencies = [ | |||
1642 | ] | 1711 | ] |
1643 | 1712 | ||
1644 | [[package]] | 1713 | [[package]] |
1714 | name = "tokio-rustls" | ||
1715 | version = "0.26.0" | ||
1716 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1717 | checksum = "0c7bc40d0e5a97695bb96e27995cd3a08538541b0a846f65bba7a359f36700d4" | ||
1718 | dependencies = [ | ||
1719 | "rustls", | ||
1720 | "rustls-pki-types", | ||
1721 | "tokio", | ||
1722 | ] | ||
1723 | |||
1724 | [[package]] | ||
1645 | name = "tokio-tungstenite" | 1725 | name = "tokio-tungstenite" |
1646 | version = "0.23.1" | 1726 | version = "0.23.1" |
1647 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1727 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -1801,6 +1881,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
1801 | checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d" | 1881 | checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d" |
1802 | 1882 | ||
1803 | [[package]] | 1883 | [[package]] |
1884 | name = "untrusted" | ||
1885 | version = "0.9.0" | ||
1886 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1887 | checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" | ||
1888 | |||
1889 | [[package]] | ||
1804 | name = "url" | 1890 | name = "url" |
1805 | version = "2.5.1" | 1891 | version = "2.5.1" |
1806 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1892 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -2182,6 +2268,12 @@ dependencies = [ | |||
2182 | ] | 2268 | ] |
2183 | 2269 | ||
2184 | [[package]] | 2270 | [[package]] |
2271 | name = "zeroize" | ||
2272 | version = "1.8.1" | ||
2273 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
2274 | checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" | ||
2275 | |||
2276 | [[package]] | ||
2185 | name = "zerovec" | 2277 | name = "zerovec" |
2186 | version = "0.10.2" | 2278 | version = "0.10.2" |
2187 | source = "registry+https://github.com/rust-lang/crates.io-index" | 2279 | source = "registry+https://github.com/rust-lang/crates.io-index" |
diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..9119338 --- /dev/null +++ b/src/cli.rs | |||
@@ -0,0 +1,56 @@ | |||
1 | use clap::{arg, command, Parser, Subcommand}; | ||
2 | use clap_complete::{generate, Generator, Shell}; | ||
3 | |||
4 | /// webol client | ||
5 | #[derive(Parser)] | ||
6 | #[command(author, version, about, long_about = None)] | ||
7 | pub struct Args { | ||
8 | #[command(subcommand)] | ||
9 | pub commands: Commands, | ||
10 | |||
11 | #[arg(long)] | ||
12 | pub server: Option<String>, | ||
13 | |||
14 | #[arg(short, long)] | ||
15 | pub secret: Option<String>, | ||
16 | } | ||
17 | |||
18 | #[derive(Subcommand)] | ||
19 | pub enum Commands { | ||
20 | Start { | ||
21 | /// id of the device | ||
22 | id: String, | ||
23 | #[arg(short, long)] | ||
24 | ping: Option<bool>, | ||
25 | }, | ||
26 | Device { | ||
27 | #[command(subcommand)] | ||
28 | devicecmd: DeviceCmd, | ||
29 | }, | ||
30 | CliGen { | ||
31 | id: Shell, | ||
32 | }, | ||
33 | } | ||
34 | |||
35 | #[derive(Subcommand)] | ||
36 | pub enum DeviceCmd { | ||
37 | Add { | ||
38 | id: String, | ||
39 | mac: String, | ||
40 | broadcast_addr: String, | ||
41 | ip: String, | ||
42 | }, | ||
43 | Get { | ||
44 | id: String, | ||
45 | }, | ||
46 | Edit { | ||
47 | id: String, | ||
48 | mac: String, | ||
49 | broadcast_addr: String, | ||
50 | ip: String, | ||
51 | }, | ||
52 | } | ||
53 | |||
54 | pub fn print_completions<G: Generator>(gen: G, cmd: &mut clap::Command) { | ||
55 | generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout()); | ||
56 | } | ||
diff --git a/src/config.rs b/src/config.rs index 01ab097..cd0b1c3 100644 --- a/src/config.rs +++ b/src/config.rs | |||
@@ -1,5 +1,7 @@ | |||
1 | use serde::Deserialize; | 1 | use serde::Deserialize; |
2 | 2 | ||
3 | use crate::cli::Args; | ||
4 | |||
3 | #[derive(Deserialize)] | 5 | #[derive(Deserialize)] |
4 | pub struct Config { | 6 | pub struct Config { |
5 | pub server: String, | 7 | pub server: String, |
@@ -45,4 +47,14 @@ impl Config { | |||
45 | 47 | ||
46 | build.try_deserialize() | 48 | build.try_deserialize() |
47 | } | 49 | } |
50 | |||
51 | pub fn cli_override(&mut self, cli: &Args) -> &Self { | ||
52 | if let Some(server) = cli.server.to_owned() { | ||
53 | self.server = server | ||
54 | } | ||
55 | if let Some(secret) = cli.secret.to_owned() { | ||
56 | self.auth.secret = secret | ||
57 | } | ||
58 | self | ||
59 | } | ||
48 | } | 60 | } |
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 | } |