summaryrefslogtreecommitdiff
path: root/src/main.rs
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 /src/main.rs
parentb375657e660b199127a76683980a5d210a572ab7 (diff)
downloadwebol-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.rs61
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 @@
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 }