mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-03-14 00:30:54 +03:00
basic saving functionality
This commit is contained in:
committed by
Ferdinand Schober
parent
18bba183ee
commit
a29489cd40
@@ -11,6 +11,7 @@ use std::path::{Path, PathBuf};
|
||||
use std::{collections::HashSet, io};
|
||||
use thiserror::Error;
|
||||
use toml;
|
||||
use toml_edit::{self, DocumentMut};
|
||||
|
||||
use lan_mouse_cli::CliArgs;
|
||||
use lan_mouse_ipc::{DEFAULT_PORT, Position};
|
||||
@@ -44,7 +45,7 @@ fn default_path() -> Result<PathBuf, VarError> {
|
||||
Ok(PathBuf::from(default_path))
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Default)]
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
|
||||
struct ConfigToml {
|
||||
capture_backend: Option<CaptureBackend>,
|
||||
emulation_backend: Option<EmulationBackend>,
|
||||
@@ -414,15 +415,21 @@ impl Config {
|
||||
|
||||
/// set configured clients
|
||||
pub fn set_clients(&mut self, clients: Vec<ConfigClient>) {
|
||||
if clients.is_empty() {
|
||||
return;
|
||||
}
|
||||
if self.config_toml.is_none() {
|
||||
self.config_toml = Default::default();
|
||||
}
|
||||
self.config_toml.as_mut().expect("config").clients =
|
||||
clients.into_iter().map(|c| c.into()).collect::<Vec<_>>();
|
||||
Some(clients.into_iter().map(|c| c.into()).collect::<Vec<_>>());
|
||||
}
|
||||
|
||||
/// set authorized keys
|
||||
pub fn set_authorized_keys(&mut self, fingerprints: HashMap<String, String>) {
|
||||
if fingerprints.is_empty() {
|
||||
return;
|
||||
}
|
||||
if self.config_toml.is_none() {
|
||||
self.config_toml = Default::default();
|
||||
}
|
||||
@@ -432,7 +439,24 @@ impl Config {
|
||||
.authorized_fingerprints = Some(fingerprints);
|
||||
}
|
||||
|
||||
pub fn write_back(&self) {
|
||||
todo!()
|
||||
pub fn write_back(&self) -> Result<(), io::Error> {
|
||||
log::info!("writing config to {:?}", &self.config_path);
|
||||
/* load the current configuration file */
|
||||
let current_config = fs::read_to_string(&self.config_path)?;
|
||||
let current_config = current_config.parse::<DocumentMut>().expect("fix me");
|
||||
let _current_config =
|
||||
toml_edit::de::from_document::<ConfigToml>(current_config).expect("fixme");
|
||||
|
||||
/* the new config */
|
||||
let new_config = self.config_toml.clone().unwrap_or_default();
|
||||
// let new_config = toml_edit::ser::to_document::<ConfigToml>(&new_config).expect("fixme");
|
||||
let new_config = toml_edit::ser::to_string_pretty(&new_config).expect("todo");
|
||||
|
||||
/* TODO merge documents */
|
||||
|
||||
/* write new config to file */
|
||||
fs::write(&self.config_path, new_config)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,7 +223,9 @@ impl Service {
|
||||
self.config.set_clients(clients);
|
||||
let authorized_keys = self.authorized_keys.read().expect("lock").clone();
|
||||
self.config.set_authorized_keys(authorized_keys);
|
||||
self.config.write_back();
|
||||
if let Err(e) = self.config.write_back() {
|
||||
log::warn!("failed to write config: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_frontend_pending(&mut self) {
|
||||
|
||||
Reference in New Issue
Block a user