summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cli.rs56
-rw-r--r--src/config.rs12
-rw-r--r--src/main.rs61
3 files changed, 75 insertions, 54 deletions
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 }