From 3df6bc8f1a2ecec1313bd9b36ff7283f840b8308 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Tue, 18 Jun 2024 14:38:48 +0200 Subject: add server and secret as arguments --- src/main.rs | 61 +++++++------------------------------------------------------ 1 file changed, 7 insertions(+), 54 deletions(-) (limited to 'src/main.rs') 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 @@ use std::{fmt::Display, time::Duration}; -use crate::config::Config; -use clap::{Command, CommandFactory, Parser, Subcommand}; -use clap_complete::{generate, Generator, Shell}; +use crate::{cli::print_completions, config::Config}; +use clap::{CommandFactory, Parser}; +use cli::{Args, Commands, DeviceCmd}; use config::Method; use error::Error; use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; @@ -13,6 +13,7 @@ use reqwest::{ }; use serde::Deserialize; +mod cli; mod config; mod error; mod requests; @@ -25,56 +26,14 @@ static DONE_STYLE: &str = " ✓ {wide_msg}"; static ERROR_STYLE: &str = " ✗ {wide_msg}"; static TICK_SPEED: u64 = 1000 / 16; -/// webol client -#[derive(Parser)] -#[command(author, version, about, long_about = None)] -struct Args { - #[command(subcommand)] - commands: Commands, -} - -#[derive(Subcommand)] -enum Commands { - Start { - /// id of the device - id: String, - #[arg(short, long)] - ping: Option, - }, - Device { - #[command(subcommand)] - devicecmd: DeviceCmd, - }, - CliGen { - id: Shell, - }, -} - -#[derive(Subcommand)] -enum DeviceCmd { - Add { - id: String, - mac: String, - broadcast_addr: String, - ip: String, - }, - Get { - id: String, - }, - Edit { - id: String, - mac: String, - broadcast_addr: String, - ip: String, - }, -} - #[tokio::main] async fn main() -> Result<(), anyhow::Error> { - let config = Config::load()?; + let mut config = Config::load()?; let cli = Args::parse(); + config.cli_override(&cli); + match cli.commands { Commands::Start { id, ping } => { start(&config, id, ping.unwrap_or(true)).await?; @@ -110,17 +69,12 @@ async fn main() -> Result<(), anyhow::Error> { Ok(()) } -fn print_completions(gen: G, cmd: &mut Command) { - generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout()); -} - fn default_headers(config: &Config) -> Result { let mut map = HeaderMap::new(); map.append("Accept-Content", HeaderValue::from_str("application/json")?); map.append("Content-Type", HeaderValue::from_str("application/json")?); if config.auth.method != Method::None { map.append("Authorization", HeaderValue::from_str(&config.auth.secret)?); - } Ok(map) @@ -129,7 +83,6 @@ fn default_headers(config: &Config) -> Result { fn format_url(config: &Config, path: &str, protocol: &Protocols, id: Option<&str>) -> String { if let Some(id) = id { format!("{}://{}/{}/{}", protocol, config.server, path, id) - } else { format!("{}://{}/{}", protocol, config.server, path) } -- cgit v1.2.3