diff --git a/Cargo.lock b/Cargo.lock index c710d3b..8cc6f3e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -67,6 +67,12 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "anyhow" +version = "1.0.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a4385e2e34eb35d6b3efe798b9eb88096925d87726c0798709bf56d9ed84af3" + [[package]] name = "bitflags" version = "1.3.2" @@ -79,6 +85,15 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ae3f5d315924270530207e2a68396c3cc547f6dca3fbdca317cfb1a51edb593" +[[package]] +name = "camino" +version = "1.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f2d30e4173c4026932d51d31d6b0613b1fd3014bf3f9f8943d4ba139c437ba0" +dependencies = [ + "serde_core", +] + [[package]] name = "cc" version = "1.2.60" @@ -716,6 +731,8 @@ dependencies = [ name = "wara" version = "0.1.0" dependencies = [ + "anyhow", + "camino", "clap", "env_logger", "log", diff --git a/Cargo.toml b/Cargo.toml index 3751290..34d110c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,8 @@ version = "0.1.0" edition = "2024" [dependencies] +anyhow = "1.0.103" +camino = { version = "1.2.4", features = ["serde1"] } clap = { version = "4.6.1", features = ["derive"] } env_logger = "0.11.11" log = "0.4.33" diff --git a/config.default.toml b/config.default.toml new file mode 100644 index 0000000..2e9276b --- /dev/null +++ b/config.default.toml @@ -0,0 +1,13 @@ +[default_logins] +username="monitor" +password="HewoWorld" + +[[host]] +name="Example1" +ip="127.0.0.1" + +[[host]] +name="Example2" +ip="127.0.0.2" +username="admin" +password="password123" diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..2e9276b --- /dev/null +++ b/config.toml @@ -0,0 +1,13 @@ +[default_logins] +username="monitor" +password="HewoWorld" + +[[host]] +name="Example1" +ip="127.0.0.1" + +[[host]] +name="Example2" +ip="127.0.0.2" +username="admin" +password="password123" diff --git a/src/config/cli.rs b/src/config/cli.rs new file mode 100644 index 0000000..d24d336 --- /dev/null +++ b/src/config/cli.rs @@ -0,0 +1,10 @@ +#[derive(Debug, Clone, Default, clap::Parser)] +pub struct CliArgs { + /// Configuration file path + #[arg(long = "config", short = 'C', default_value = "./config.toml")] + pub config_file: camino::Utf8PathBuf, + + /// Output more information in logs + #[arg(long = "verbose", short = 'v')] + pub verbose: bool, +} diff --git a/src/config/mod.rs b/src/config/mod.rs index e69de29..d156062 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -0,0 +1,77 @@ +use std::net::{IpAddr, Ipv4Addr}; + +use clap::Parser; +use serde::{Deserialize, Serialize}; + +use crate::config::cli::CliArgs; + +const DEFAULT_CONFIG: &'static str = include_str!("../../config.default.toml"); + +mod cli; + +#[derive(Debug, Clone, Serialize, Deserialize, Default)] +pub struct Config { + pub default_logins: ConfigDefaultLogins, + #[serde(rename = "host")] + pub hosts: Vec, + + #[serde(skip)] + pub cli: CliArgs, +} + +#[derive(Debug, Clone, Serialize, Deserialize, Default)] +pub struct ConfigDefaultLogins { + pub username: String, + pub password: String, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct ConfigHost { + pub name: String, + pub ip: IpAddr, + pub username: Option, + pub password: Option, +} + +impl Config { + pub fn parse() -> anyhow::Result { + let cli = cli::CliArgs::parse(); + + if !cli.config_file.exists() { + std::fs::write(&cli.config_file, DEFAULT_CONFIG)?; + } + + let text = std::fs::read_to_string(&cli.config_file)?; + let mut slf: Config = toml::from_str(&text)?; + slf.cli = cli; + Ok(slf) + } +} + +impl ConfigHost { + pub fn username<'a>(&'a self, cfg: &'a Config) -> &'a String { + if let Some(username) = &self.username { + username + } else { + &cfg.default_logins.username + } + } + pub fn password<'a>(&'a self, cfg: &'a Config) -> &'a String { + if let Some(password) = &self.password { + password + } else { + &cfg.default_logins.password + } + } +} + +impl Default for ConfigHost { + fn default() -> Self { + Self { + name: String::default(), + ip: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), + username: None, + password: None, + } + } +} diff --git a/src/main.rs b/src/main.rs index e7a11a9..a9e4062 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,8 @@ -fn main() { - println!("Hello, world!"); +mod config; +mod mt_commander; + +fn main() -> anyhow::Result<()> { + let cfg = config::Config::parse()?; + dbg!(&cfg); + Ok(()) } diff --git a/src/mt_commander/mods.rs b/src/mt_commander/mod.rs similarity index 100% rename from src/mt_commander/mods.rs rename to src/mt_commander/mod.rs