Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added native operator permission management #348

Merged
merged 15 commits into from
Dec 27, 2024
Prev Previous commit
Next Next commit
create data dir when needed
Snowiiii committed Dec 27, 2024
commit 1f5e6fa8598481b30b5a38caa4b8c0fd8ad6ad03
32 changes: 23 additions & 9 deletions pumpkin/src/data/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{fs, path::Path};
use std::{env, fs, path::Path};

use serde::{Deserialize, Serialize};

const DATA_FOLDER: &str = "data/";

pub mod op_data;

pub trait LoadJSONConfiguration {
@@ -10,23 +12,29 @@ pub trait LoadJSONConfiguration {
where
Self: Sized + Default + Serialize + for<'de> Deserialize<'de>,
{
let path = Self::get_path();
let exe_dir = env::current_dir().unwrap();
let data_dir = exe_dir.join(DATA_FOLDER);
if !data_dir.exists() {
log::debug!("creating new data root folder");
fs::create_dir(&data_dir).expect("Failed to create data root folder");
}
let path = data_dir.join(Self::get_path());

let config = if path.exists() {
let file_content = fs::read_to_string(path)
let file_content = fs::read_to_string(&path)
.unwrap_or_else(|_| panic!("Couldn't read configuration file at {path:?}"));

serde_json::from_str(&file_content).unwrap_or_else(|err| {
panic!(
"Couldn't parse config at {path:?}. Reason: {err}. This is probably caused by a config update. Just delete the old config and restart.",
"Couldn't parse data config at {path:?}. Reason: {err}. This is probably caused by a config update. Just delete the old data config and restart.",
)
})
} else {
let content = Self::default();

if let Err(err) = fs::write(path, serde_json::to_string_pretty(&content).unwrap()) {
if let Err(err) = fs::write(&path, serde_json::to_string_pretty(&content).unwrap()) {
eprintln!(
"Couldn't write default config to {path:?}. Reason: {err}. This is probably caused by a config update. Just delete the old config and restart.",
"Couldn't write default data config to {path:?}. Reason: {err}. This is probably caused by a config update. Just delete the old data config and restart.",
);
}

@@ -49,21 +57,27 @@ pub trait SaveJSONConfiguration: LoadJSONConfiguration {
where
Self: Sized + Default + Serialize + for<'de> Deserialize<'de>,
{
let path = <Self as LoadJSONConfiguration>::get_path();
let exe_dir = env::current_dir().unwrap();
let data_dir = exe_dir.join(DATA_FOLDER);
if !data_dir.exists() {
log::debug!("creating new data root folder");
fs::create_dir(&data_dir).expect("Failed to create data root folder");
}
let path = data_dir.join(Self::get_path());

let content = match serde_json::to_string_pretty(self) {
Ok(content) => content,
Err(err) => {
log::warn!(
"Couldn't serialize operator config to {:?}. Reason: {}",
"Couldn't serialize operator data config to {:?}. Reason: {}",
path,
err
);
return;
}
};

if let Err(err) = std::fs::write(path, content) {
if let Err(err) = std::fs::write(&path, content) {
log::warn!(
"Couldn't write operator config to {:?}. Reason: {}",
path,