Config is now setup mostly :3

This commit is contained in:
2026-07-01 18:18:37 +03:00
parent 23b05296ba
commit cb142bff0e
8 changed files with 139 additions and 2 deletions

17
Cargo.lock generated
View File

@@ -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",

View File

@@ -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"

13
config.default.toml Normal file
View File

@@ -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"

13
config.toml Normal file
View File

@@ -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"

10
src/config/cli.rs Normal file
View File

@@ -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,
}

View File

@@ -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<ConfigHost>,
#[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<String>,
pub password: Option<String>,
}
impl Config {
pub fn parse() -> anyhow::Result<Self> {
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,
}
}
}

View File

@@ -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(())
}