Start using anyhow

This commit is contained in:
Samuel Lorch 2023-10-29 22:20:32 +01:00
parent 8c82d26fcf
commit f951718241
3 changed files with 7 additions and 3 deletions

1
Cargo.lock generated
View file

@ -688,6 +688,7 @@ dependencies = [
name = "nfsense" name = "nfsense"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"async-trait", "async-trait",
"axum", "axum",
"ipnet", "ipnet",

View file

@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
anyhow = "1.0.75"
async-trait = "0.1.74" async-trait = "0.1.74"
axum = "0.6.20" axum = "0.6.20"
ipnet = { version = "2.8.0", features = ["serde"] } ipnet = { version = "2.8.0", features = ["serde"] }

View file

@ -3,9 +3,10 @@ use validator::Validate;
use crate::api::ApiError; use crate::api::ApiError;
use super::definitions::config::Config; use super::definitions::config::Config;
use anyhow::{Context, Result};
use std::fs; use std::fs;
use std::io;
use std::sync::{Arc, Mutex, MutexGuard}; use std::sync::{Arc, Mutex, MutexGuard};
use std::{io, result::Result};
use pwhash::sha512_crypt; use pwhash::sha512_crypt;
@ -74,11 +75,12 @@ impl ConfigManager {
self.shared_data.lock().unwrap().pending_config.clone() self.shared_data.lock().unwrap().pending_config.clone()
} }
pub fn apply_pending_changes(&mut self) -> Result<(), ConfigError> { pub fn apply_pending_changes(&mut self) -> Result<()> {
let mut data = self.shared_data.lock().unwrap(); let mut data = self.shared_data.lock().unwrap();
// TODO run Apply functions // TODO run Apply functions
// TODO Revert on Apply Failure and Return // TODO Revert on Apply Failure and Return
write_config_to_file(CURRENT_CONFIG_PATH, data.pending_config.clone())?; write_config_to_file(CURRENT_CONFIG_PATH, data.pending_config.clone())
.context("Writing Config to file".to_string())?;
// TODO revert if config save fails // TODO revert if config save fails
// TODO Remove Pending Config File // TODO Remove Pending Config File
data.current_config = data.pending_config.clone(); data.current_config = data.pending_config.clone();