lib_defs/kloud/src/datacontext.rs

35 lines
879 B
Rust
Raw Normal View History

2021-09-01 16:16:19 +00:00
use serde::{Serialize,Deserialize};
use async_graphql::{SimpleObject};
use crate::{Profile,Auth,Permissions};
#[derive(Clone, Debug, Serialize, Deserialize, Default, SimpleObject)]
pub struct DataContext {
pub profile: Profile,
pub auth: Auth,
pub perms: Permissions,
}
pub fn set_data_ctx() -> DataContext {
let data_ctx_path = &envmnt::get_or("DATA_CONTEXT", "data/ctx.yaml");
let fallback = || {
DataContext {
profile: Profile::Pro,
auth: Auth::NotSet,
perms: Permissions::Write
}
};
let mut data_ctx: DataContext = fallback();
if std::path::Path::new(&data_ctx_path).exists() {
data_ctx = match std::fs::read_to_string(&data_ctx_path) {
Ok(content) =>
match serde_yaml::from_str(&content) {
Ok(res) => res,
Err(_) => fallback(),
},
Err(_) => fallback(),
}
}
data_ctx
}