diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cli.rs | 56 | ||||
-rw-r--r-- | src/config.rs | 12 | ||||
-rw-r--r-- | src/main.rs | 61 |
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 @@ | |||
1 | use clap::{arg, command, Parser, Subcommand}; | ||
2 | use clap_complete::{generate, Generator, Shell}; | ||
3 | |||
4 | /// webol client | ||
5 | #[derive(Parser)] | ||
6 | #[command(author, version, about, long_about = None)] | ||
7 | pub 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)] | ||
19 | pub 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)] | ||
36 | pub 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 | |||
54 | pub 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 @@ | |||
1 | use serde::Deserialize; | 1 | use serde::Deserialize; |
2 | 2 | ||
3 | use crate::cli::Args; | ||
4 | |||
3 | #[derive(Deserialize)] | 5 | #[derive(Deserialize)] |
4 | pub struct Config { | 6 | pub 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 @@ | |||
1 | use std::{fmt::Display, time::Duration}; | 1 | use std::{fmt::Display, time::Duration}; |
2 | 2 | ||
3 | use crate::config::Config; | 3 | use crate::{cli::print_completions, config::Config}; |
4 | use clap::{Command, CommandFactory, Parser, Subcommand}; | 4 | use clap::{CommandFactory, Parser}; |
5 | use clap_complete::{generate, Generator, Shell}; | 5 | use cli::{Args, Commands, DeviceCmd}; |
6 | use config::Method; | 6 | use config::Method; |
7 | use error::Error; | 7 | use error::Error; |
8 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; | 8 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; |
@@ -13,6 +13,7 @@ use reqwest::{ | |||
13 | }; | 13 | }; |
14 | use serde::Deserialize; | 14 | use serde::Deserialize; |
15 | 15 | ||
16 | mod cli; | ||
16 | mod config; | 17 | mod config; |
17 | mod error; | 18 | mod error; |
18 | mod requests; | 19 | mod requests; |
@@ -25,56 +26,14 @@ static DONE_STYLE: &str = " ✓ {wide_msg}"; | |||
25 | static ERROR_STYLE: &str = " ✗ {wide_msg}"; | 26 | static ERROR_STYLE: &str = " ✗ {wide_msg}"; |
26 | static TICK_SPEED: u64 = 1000 / 16; | 27 | static TICK_SPEED: u64 = 1000 / 16; |
27 | 28 | ||
28 | /// webol client | ||
29 | #[derive(Parser)] | ||
30 | #[command(author, version, about, long_about = None)] | ||
31 | struct Args { | ||
32 | #[command(subcommand)] | ||
33 | commands: Commands, | ||
34 | } | ||
35 | |||
36 | #[derive(Subcommand)] | ||
37 | enum 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)] | ||
54 | enum 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] |
73 | async fn main() -> Result<(), anyhow::Error> { | 30 | async 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 | ||
113 | fn print_completions<G: Generator>(gen: G, cmd: &mut Command) { | ||
114 | generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout()); | ||
115 | } | ||
116 | |||
117 | fn default_headers(config: &Config) -> Result<HeaderMap, Error> { | 72 | fn 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> { | |||
129 | fn format_url(config: &Config, path: &str, protocol: &Protocols, id: Option<&str>) -> String { | 83 | fn 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 | } |