Skip to content

Commit 9135040

Browse files
committed
basic saving functionality
1 parent c5da55f commit 9135040

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ shadow-rs = { version = "1.2.0", features = ["metadata"] }
3838

3939
hickory-resolver = "0.25.2"
4040
toml = "0.8"
41+
toml_edit = { version = "0.22", features = ["serde"] }
4142
serde = { version = "1.0", features = ["derive"] }
4243
log = "0.4.20"
4344
env_logger = "0.11.3"

lan-mouse-cli/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ enum CliSubcommand {
7171
},
7272
/// deauthorize a public key
7373
RemoveAuthorizedKey { sha256_fingerprint: String },
74+
/// save configuration to file
75+
SaveConfig,
7476
}
7577

7678
pub async fn run(args: CliArgs) -> Result<(), CliError> {
@@ -162,6 +164,7 @@ async fn execute(cmd: CliSubcommand) -> Result<(), CliError> {
162164
tx.request(FrontendRequest::RemoveAuthorizedKey(sha256_fingerprint))
163165
.await?
164166
}
167+
CliSubcommand::SaveConfig => tx.request(FrontendRequest::SaveConfiguration).await?,
165168
}
166169
Ok(())
167170
}

src/config.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::path::{Path, PathBuf};
1111
use std::{collections::HashSet, io};
1212
use thiserror::Error;
1313
use toml;
14+
use toml_edit::{self, DocumentMut};
1415

1516
use lan_mouse_cli::CliArgs;
1617
use lan_mouse_ipc::{Position, DEFAULT_PORT};
@@ -44,7 +45,7 @@ fn default_path() -> Result<PathBuf, VarError> {
4445
Ok(PathBuf::from(default_path))
4546
}
4647

47-
#[derive(Serialize, Deserialize, Debug, Default)]
48+
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
4849
struct ConfigToml {
4950
capture_backend: Option<CaptureBackend>,
5051
emulation_backend: Option<EmulationBackend>,
@@ -414,15 +415,21 @@ impl Config {
414415

415416
/// set configured clients
416417
pub fn set_clients(&mut self, clients: Vec<ConfigClient>) {
418+
if clients.is_empty() {
419+
return;
420+
}
417421
if self.config_toml.is_none() {
418422
self.config_toml = Default::default();
419423
}
420424
self.config_toml.as_mut().expect("config").clients =
421-
clients.into_iter().map(|c| c.into()).collect::<Vec<_>>();
425+
Some(clients.into_iter().map(|c| c.into()).collect::<Vec<_>>());
422426
}
423427

424428
/// set authorized keys
425429
pub fn set_authorized_keys(&mut self, fingerprints: HashMap<String, String>) {
430+
if fingerprints.is_empty() {
431+
return;
432+
}
426433
if self.config_toml.is_none() {
427434
self.config_toml = Default::default();
428435
}
@@ -432,7 +439,24 @@ impl Config {
432439
.authorized_fingerprints = Some(fingerprints);
433440
}
434441

435-
pub fn write_back(&self) {
436-
todo!()
442+
pub fn write_back(&self) -> Result<(), io::Error> {
443+
log::info!("writing config to {:?}", &self.config_path);
444+
/* load the current configuration file */
445+
let current_config = fs::read_to_string(&self.config_path)?;
446+
let current_config = current_config.parse::<DocumentMut>().expect("fix me");
447+
let _current_config =
448+
toml_edit::de::from_document::<ConfigToml>(current_config).expect("fixme");
449+
450+
/* the new config */
451+
let new_config = self.config_toml.clone().unwrap_or_default();
452+
// let new_config = toml_edit::ser::to_document::<ConfigToml>(&new_config).expect("fixme");
453+
let new_config = toml_edit::ser::to_string_pretty(&new_config).expect("todo");
454+
455+
/* TODO merge documents */
456+
457+
/* write new config to file */
458+
fs::write(&self.config_path, new_config)?;
459+
460+
Ok(())
437461
}
438462
}

src/service.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@ impl Service {
223223
self.config.set_clients(clients);
224224
let authorized_keys = self.authorized_keys.read().expect("lock").clone();
225225
self.config.set_authorized_keys(authorized_keys);
226-
self.config.write_back();
226+
if let Err(e) = self.config.write_back() {
227+
log::warn!("failed to write config: {e}");
228+
}
227229
}
228230

229231
async fn handle_frontend_pending(&mut self) {

0 commit comments

Comments
 (0)