147 lines
4.2 KiB
Rust
147 lines
4.2 KiB
Rust
//! Modul für die Konfigurationsverwaltung der Anwendung.
|
|
//!
|
|
//! Dieses Modul stellt die Funktionalität zum Laden, Speichern und Verwalten
|
|
//! der Anwendungskonfiguration bereit. Die Konfiguration wird in einer Datei gespeichert
|
|
//! und beim Programmstart automatisch geladen.
|
|
|
|
use crate::program;
|
|
use crate::sudo::is_run_as_root;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::sync::OnceLock;
|
|
|
|
/// Hauptkonfigurationsstruktur der Anwendung.
|
|
///
|
|
/// Enthält alle Konfigurationseinstellungen, aufgeteilt in verschiedene Bereiche.
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct AppConfig {
|
|
/// Allgemeine Einstellungen
|
|
pub general: General,
|
|
/// Lokale Einbindungseinstellungen
|
|
pub local: Local,
|
|
/// Remote-Einbindungseinstellungen
|
|
pub remote: Remote,
|
|
}
|
|
|
|
impl Default for AppConfig {
|
|
fn default() -> Self {
|
|
Self {
|
|
general: General::default(),
|
|
local: Local::default(),
|
|
remote: Remote::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Allgemeine Konfigurationseinstellungen.
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct General {
|
|
/// Log-Level für die Anwendung
|
|
pub log_level: String,
|
|
/// Pfad zum Einhängepunkt
|
|
pub mount_point: String,
|
|
}
|
|
|
|
impl Default for General {
|
|
fn default() -> Self {
|
|
Self {
|
|
log_level: "info".to_string(),
|
|
mount_point: get_default_mount_path(),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Konfiguration für lokale Einbindungen.
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct Local {
|
|
/// Typ der lokalen Einbindung (z.B. "nfs")
|
|
pub mount_type: String,
|
|
/// Pfad zur lokalen Einbindung
|
|
pub mount_path: String,
|
|
/// MAC-Adresse des lokalen Geräts
|
|
pub device_mac: String,
|
|
}
|
|
|
|
impl Default for Local {
|
|
fn default() -> Self {
|
|
Self {
|
|
mount_type: "nfs".to_string(),
|
|
mount_path: "".to_string(),
|
|
device_mac: "".to_string(),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Konfiguration für Remote-Einbindungen.
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct Remote {
|
|
/// Typ der Remote-Einbindung (z.B. "webdav")
|
|
pub mount_type: String,
|
|
/// Pfad zur Remote-Einbindung
|
|
pub mount_path: String,
|
|
/// Benutzername für Remote-Zugriff
|
|
pub username: String,
|
|
/// Passwort für Remote-Zugriff
|
|
pub password: String,
|
|
}
|
|
|
|
impl Default for Remote {
|
|
fn default() -> Self {
|
|
Self {
|
|
mount_type: "webdav".to_string(),
|
|
mount_path: "".to_string(),
|
|
username: "".to_string(),
|
|
password: "".to_string(),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Ermittelt den Standard-Einhängepunkt basierend auf dem Betriebssystem.
|
|
fn get_default_mount_path() -> String {
|
|
#[cfg(not(any(target_os = "windows")))]
|
|
{
|
|
"/media/smart_mount".to_string()
|
|
}
|
|
#[cfg(any(target_os = "windows"))]
|
|
{
|
|
let drives = ('C'..='Z').collect::<Vec<char>>();
|
|
for drive in drives {
|
|
let drive_string = format!("{}:", drive);
|
|
if !std::path::Path::new(&drive_string).exists() {
|
|
return format!("{}:", drive);
|
|
}
|
|
}
|
|
"H".to_string()
|
|
}
|
|
}
|
|
|
|
static CONFIG_NAME: &str = "config";
|
|
static CONFIG: OnceLock<AppConfig> = OnceLock::new();
|
|
|
|
/// Gibt die aktuelle Konfiguration zurück.
|
|
///
|
|
/// Lädt die Konfiguration beim ersten Aufruf und speichert sie zwischen.
|
|
/// Nachfolgende Aufrufe geben die gespeicherte Konfiguration zurück.
|
|
pub fn get_config() -> &'static AppConfig {
|
|
CONFIG.get_or_init(|| {
|
|
load_config()
|
|
})
|
|
}
|
|
|
|
/// Lädt die Konfiguration aus der Konfigurationsdatei.
|
|
fn load_config() -> AppConfig {
|
|
#[cfg(target_os = "linux")]
|
|
if is_run_as_root() {
|
|
return confy::load_path(format!("/etc/{}/{}.toml", program::program_name(), CONFIG_NAME)).unwrap_or_default();
|
|
}
|
|
confy::load(&*program::program_name(), CONFIG_NAME).unwrap_or_default()
|
|
}
|
|
|
|
// /// Speichert die übergebene Konfiguration in der Konfigurationsdatei.
|
|
// fn save_config(config: AppConfig) {
|
|
// #[cfg(target_os = "linux")]
|
|
// if is_run_as_root() {
|
|
// confy::store_path(format!("/etc/{}/{}.toml", program::program_name(), CONFIG_NAME), config).unwrap();
|
|
// return;
|
|
// }
|
|
// confy::store(&*program::program_name(), CONFIG_NAME, config).unwrap();
|
|
// }
|