All checks were successful
Run Check Script / check (pull_request) Successful in 59s
Co-authored-by: Jean-Gabriel Gill-Couture <jeangabriel.gc@gmail.com> Co-authored-by: Ian Letourneau <ian@noma.to> Reviewed-on: #130 Reviewed-by: Ian Letourneau <ian@noma.to> Co-authored-by: Jean-Gabriel Gill-Couture <jg@nationtech.io> Co-committed-by: Jean-Gabriel Gill-Couture <jg@nationtech.io>
38 lines
1.0 KiB
Rust
38 lines
1.0 KiB
Rust
use crate::config::check_hash;
|
|
use crate::config::manager::ConfigManager;
|
|
use crate::error::Error;
|
|
use async_trait::async_trait;
|
|
use std::fs;
|
|
|
|
#[derive(Debug)]
|
|
pub struct LocalFileConfigManager {
|
|
file_path: String,
|
|
}
|
|
|
|
impl LocalFileConfigManager {
|
|
pub fn new(file_path: String) -> Self {
|
|
Self { file_path }
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl ConfigManager for LocalFileConfigManager {
|
|
async fn load_as_str(&self) -> Result<String, Error> {
|
|
Ok(fs::read_to_string(&self.file_path)?)
|
|
}
|
|
|
|
async fn save_config(&self, content: &str, hash: &str) -> Result<(), Error> {
|
|
let current_content = self.load_as_str().await?;
|
|
if !check_hash(¤t_content, hash) {
|
|
return Err(Error::Config(format!(
|
|
"OPNSense config file changed since loading it! Hash when loading : {hash}"
|
|
)));
|
|
}
|
|
Ok(fs::write(&self.file_path, content)?)
|
|
}
|
|
|
|
async fn apply_new_config(&self, content: &str, hash: &str) -> Result<(), Error> {
|
|
self.save_config(content, hash).await
|
|
}
|
|
}
|