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/cli.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/config.rs | 12 ++++++++++++ src/main.rs | 61 +++++++---------------------------------------------------- 3 files changed, 75 insertions(+), 54 deletions(-) create mode 100644 src/cli.rs (limited to 'src') 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 @@ +use clap::{arg, command, Parser, Subcommand}; +use clap_complete::{generate, Generator, Shell}; + +/// webol client +#[derive(Parser)] +#[command(author, version, about, long_about = None)] +pub struct Args { + #[command(subcommand)] + pub commands: Commands, + + #[arg(long)] + pub server: Option, + + #[arg(short, long)] + pub secret: Option, +} + +#[derive(Subcommand)] +pub enum Commands { + Start { + /// id of the device + id: String, + #[arg(short, long)] + ping: Option, + }, + Device { + #[command(subcommand)] + devicecmd: DeviceCmd, + }, + CliGen { + id: Shell, + }, +} + +#[derive(Subcommand)] +pub 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, + }, +} + +pub fn print_completions(gen: G, cmd: &mut clap::Command) { + generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout()); +} 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 @@ use serde::Deserialize; +use crate::cli::Args; + #[derive(Deserialize)] pub struct Config { pub server: String, @@ -45,4 +47,14 @@ impl Config { build.try_deserialize() } + + pub fn cli_override(&mut self, cli: &Args) -> &Self { + if let Some(server) = cli.server.to_owned() { + self.server = server + } + if let Some(secret) = cli.secret.to_owned() { + self.auth.secret = secret + } + self + } } 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