summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2024-06-18 14:38:48 +0200
committerfxqnlr <[email protected]>2024-06-18 14:38:48 +0200
commit3df6bc8f1a2ecec1313bd9b36ff7283f840b8308 (patch)
treef780cf73a1a41de6a9314c6e5a6d1fa07fb8c37a
parentb375657e660b199127a76683980a5d210a572ab7 (diff)
downloadwebol-cli-args.tar
webol-cli-args.tar.gz
webol-cli-args.zip
add server and secret as argumentsargs
-rw-r--r--Cargo.lock108
-rw-r--r--src/cli.rs56
-rw-r--r--src/config.rs12
-rw-r--r--src/main.rs61
4 files changed, 175 insertions, 62 deletions
diff --git a/Cargo.lock b/Cargo.lock
index efeb4e9..a073c3f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -626,9 +626,9 @@ dependencies = [
626 626
627[[package]] 627[[package]]
628name = "httparse" 628name = "httparse"
629version = "1.9.3" 629version = "1.9.4"
630source = "registry+https://github.com/rust-lang/crates.io-index" 630source = "registry+https://github.com/rust-lang/crates.io-index"
631checksum = "d0e7a4dd27b9476dc40cb050d3632d3bba3a70ddbff012285f7f8559a1e7e545" 631checksum = "0fcc0b4a115bf80b728eb8ea024ad5bd707b615bfed49e0665b6e0f86fd082d9"
632 632
633[[package]] 633[[package]]
634name = "hyper" 634name = "hyper"
@@ -651,6 +651,23 @@ dependencies = [
651] 651]
652 652
653[[package]] 653[[package]]
654name = "hyper-rustls"
655version = "0.27.2"
656source = "registry+https://github.com/rust-lang/crates.io-index"
657checksum = "5ee4be2c948921a1a5320b629c4193916ed787a7f7f293fd3f7f5a6c9de74155"
658dependencies = [
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]]
654name = "hyper-tls" 671name = "hyper-tls"
655version = "0.6.0" 672version = "0.6.0"
656source = "registry+https://github.com/rust-lang/crates.io-index" 673source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -952,9 +969,9 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
952 969
953[[package]] 970[[package]]
954name = "miniz_oxide" 971name = "miniz_oxide"
955version = "0.7.3" 972version = "0.7.4"
956source = "registry+https://github.com/rust-lang/crates.io-index" 973source = "registry+https://github.com/rust-lang/crates.io-index"
957checksum = "87dfd01fe195c66b572b37921ad8803d010623c0aca821bea2302239d155cdae" 974checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08"
958dependencies = [ 975dependencies = [
959 "adler", 976 "adler",
960] 977]
@@ -1256,9 +1273,9 @@ dependencies = [
1256 1273
1257[[package]] 1274[[package]]
1258name = "reqwest" 1275name = "reqwest"
1259version = "0.12.4" 1276version = "0.12.5"
1260source = "registry+https://github.com/rust-lang/crates.io-index" 1277source = "registry+https://github.com/rust-lang/crates.io-index"
1261checksum = "566cafdd92868e0939d3fb961bd0dc25fcfaaed179291093b3d43e6b3150ea10" 1278checksum = "c7d6d2a27d57148378eb5e111173f4276ad26340ecc5c49a4a2152167a2d6a37"
1262dependencies = [ 1279dependencies = [
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]]
1319name = "ring"
1320version = "0.17.8"
1321source = "registry+https://github.com/rust-lang/crates.io-index"
1322checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d"
1323dependencies = [
1324 "cc",
1325 "cfg-if",
1326 "getrandom",
1327 "libc",
1328 "spin",
1329 "untrusted",
1330 "windows-sys 0.52.0",
1331]
1332
1333[[package]]
1301name = "ron" 1334name = "ron"
1302version = "0.8.1" 1335version = "0.8.1"
1303source = "registry+https://github.com/rust-lang/crates.io-index" 1336source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1339,6 +1372,19 @@ dependencies = [
1339] 1372]
1340 1373
1341[[package]] 1374[[package]]
1375name = "rustls"
1376version = "0.23.10"
1377source = "registry+https://github.com/rust-lang/crates.io-index"
1378checksum = "05cff451f60db80f490f3c182b77c35260baace73209e9cdbbe526bfe3a4d402"
1379dependencies = [
1380 "once_cell",
1381 "rustls-pki-types",
1382 "rustls-webpki",
1383 "subtle",
1384 "zeroize",
1385]
1386
1387[[package]]
1342name = "rustls-pemfile" 1388name = "rustls-pemfile"
1343version = "2.1.2" 1389version = "2.1.2"
1344source = "registry+https://github.com/rust-lang/crates.io-index" 1390source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1355,6 +1401,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1355checksum = "976295e77ce332211c0d24d92c0e83e50f5c5f046d11082cea19f3df13a3562d" 1401checksum = "976295e77ce332211c0d24d92c0e83e50f5c5f046d11082cea19f3df13a3562d"
1356 1402
1357[[package]] 1403[[package]]
1404name = "rustls-webpki"
1405version = "0.102.4"
1406source = "registry+https://github.com/rust-lang/crates.io-index"
1407checksum = "ff448f7e92e913c4b7d4c6d8e4540a1724b319b4152b8aef6d4cf8339712b33e"
1408dependencies = [
1409 "ring",
1410 "rustls-pki-types",
1411 "untrusted",
1412]
1413
1414[[package]]
1358name = "ryu" 1415name = "ryu"
1359version = "1.0.18" 1416version = "1.0.18"
1360source = "registry+https://github.com/rust-lang/crates.io-index" 1417source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1492,6 +1549,12 @@ dependencies = [
1492] 1549]
1493 1550
1494[[package]] 1551[[package]]
1552name = "spin"
1553version = "0.9.8"
1554source = "registry+https://github.com/rust-lang/crates.io-index"
1555checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
1556
1557[[package]]
1495name = "stable_deref_trait" 1558name = "stable_deref_trait"
1496version = "1.2.0" 1559version = "1.2.0"
1497source = "registry+https://github.com/rust-lang/crates.io-index" 1560source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1504,6 +1567,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1504checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" 1567checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
1505 1568
1506[[package]] 1569[[package]]
1570name = "subtle"
1571version = "2.5.0"
1572source = "registry+https://github.com/rust-lang/crates.io-index"
1573checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc"
1574
1575[[package]]
1507name = "syn" 1576name = "syn"
1508version = "2.0.66" 1577version = "2.0.66"
1509source = "registry+https://github.com/rust-lang/crates.io-index" 1578source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1516,9 +1585,9 @@ dependencies = [
1516 1585
1517[[package]] 1586[[package]]
1518name = "sync_wrapper" 1587name = "sync_wrapper"
1519version = "0.1.2" 1588version = "1.0.1"
1520source = "registry+https://github.com/rust-lang/crates.io-index" 1589source = "registry+https://github.com/rust-lang/crates.io-index"
1521checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" 1590checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394"
1522 1591
1523[[package]] 1592[[package]]
1524name = "synstructure" 1593name = "synstructure"
@@ -1642,6 +1711,17 @@ dependencies = [
1642] 1711]
1643 1712
1644[[package]] 1713[[package]]
1714name = "tokio-rustls"
1715version = "0.26.0"
1716source = "registry+https://github.com/rust-lang/crates.io-index"
1717checksum = "0c7bc40d0e5a97695bb96e27995cd3a08538541b0a846f65bba7a359f36700d4"
1718dependencies = [
1719 "rustls",
1720 "rustls-pki-types",
1721 "tokio",
1722]
1723
1724[[package]]
1645name = "tokio-tungstenite" 1725name = "tokio-tungstenite"
1646version = "0.23.1" 1726version = "0.23.1"
1647source = "registry+https://github.com/rust-lang/crates.io-index" 1727source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1801,6 +1881,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1801checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d" 1881checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d"
1802 1882
1803[[package]] 1883[[package]]
1884name = "untrusted"
1885version = "0.9.0"
1886source = "registry+https://github.com/rust-lang/crates.io-index"
1887checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
1888
1889[[package]]
1804name = "url" 1890name = "url"
1805version = "2.5.1" 1891version = "2.5.1"
1806source = "registry+https://github.com/rust-lang/crates.io-index" 1892source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2182,6 +2268,12 @@ dependencies = [
2182] 2268]
2183 2269
2184[[package]] 2270[[package]]
2271name = "zeroize"
2272version = "1.8.1"
2273source = "registry+https://github.com/rust-lang/crates.io-index"
2274checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde"
2275
2276[[package]]
2185name = "zerovec" 2277name = "zerovec"
2186version = "0.10.2" 2278version = "0.10.2"
2187source = "registry+https://github.com/rust-lang/crates.io-index" 2279source = "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 @@
1use clap::{arg, command, Parser, Subcommand};
2use clap_complete::{generate, Generator, Shell};
3
4/// webol client
5#[derive(Parser)]
6#[command(author, version, about, long_about = None)]
7pub 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)]
19pub 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)]
36pub 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
54pub 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 @@
1use serde::Deserialize; 1use serde::Deserialize;
2 2
3use crate::cli::Args;
4
3#[derive(Deserialize)] 5#[derive(Deserialize)]
4pub struct Config { 6pub 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 @@
1use std::{fmt::Display, time::Duration}; 1use std::{fmt::Display, time::Duration};
2 2
3use crate::config::Config; 3use crate::{cli::print_completions, config::Config};
4use clap::{Command, CommandFactory, Parser, Subcommand}; 4use clap::{CommandFactory, Parser};
5use clap_complete::{generate, Generator, Shell}; 5use cli::{Args, Commands, DeviceCmd};
6use config::Method; 6use config::Method;
7use error::Error; 7use error::Error;
8use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; 8use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
@@ -13,6 +13,7 @@ use reqwest::{
13}; 13};
14use serde::Deserialize; 14use serde::Deserialize;
15 15
16mod cli;
16mod config; 17mod config;
17mod error; 18mod error;
18mod requests; 19mod requests;
@@ -25,56 +26,14 @@ static DONE_STYLE: &str = " ✓ {wide_msg}";
25static ERROR_STYLE: &str = " ✗ {wide_msg}"; 26static ERROR_STYLE: &str = " ✗ {wide_msg}";
26static TICK_SPEED: u64 = 1000 / 16; 27static TICK_SPEED: u64 = 1000 / 16;
27 28
28/// webol client
29#[derive(Parser)]
30#[command(author, version, about, long_about = None)]
31struct Args {
32 #[command(subcommand)]
33 commands: Commands,
34}
35
36#[derive(Subcommand)]
37enum 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)]
54enum 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]
73async fn main() -> Result<(), anyhow::Error> { 30async 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
113fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
114 generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout());
115}
116
117fn default_headers(config: &Config) -> Result<HeaderMap, Error> { 72fn 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> {
129fn format_url(config: &Config, path: &str, protocol: &Protocols, id: Option<&str>) -> String { 83fn 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 }