use crate::git::GitRepo;
use config::{Config, ConfigError, File};
use std::collections::HashMap;
#[derive(Debug, Deserialize, Serialize)]
pub struct InternalSettings {
pub workflow: HashMap<String, String>, }
impl ToString for InternalSettings {
fn to_string(&self) -> String {
toml::to_string(&self).unwrap()
}
}
impl Default for InternalSettings {
fn default() -> Self {
InternalSettings {
workflow: Default::default(),
}
}
}
impl InternalSettings {
pub fn get() -> Result<Self, ConfigError> {
let repo = GitRepo::open().unwrap();
let workdir = repo.get_repo_dir().unwrap();
let mut config_path = workdir.to_path_buf();
config_path.push(".jet/config.internal.toml");
if config_path.exists() {
let mut s = Config::new();
s.merge(File::from(config_path))?;
s.try_into()
} else {
Err(ConfigError::NotFound(
"Unable to find .jet/config.internal.toml".into(),
))
}
}
pub fn add_workflow(
name: &str,
id: &str,
) -> Result<Self, ConfigError> {
let repo = GitRepo::open().unwrap();
let workdir = repo.get_repo_dir().unwrap();
let mut path = workdir.to_path_buf();
path.push(".jet/config.internal.toml");
let mut s = Config::new();
s.merge(File::from(path))?;
let mut workflow: HashMap<String, String> = if let Ok(workflow) = s.get("workflow") {
workflow
} else {
HashMap::new()
};
let _ = workflow.insert(name.into(), id.into());
s.set("workflow", workflow)?;
s.try_into()
}
}