use terraphim_config::{Config, ConfigState};
use terraphim_persistence::Persistable;
use terraphim_types::RoleName;
pub async fn get_config(config_state: &ConfigState) -> Config {
let config = config_state.config.lock().await;
config.clone()
}
pub async fn get_selected_role(config_state: &ConfigState) -> RoleName {
let config = config_state.config.lock().await;
config.selected_role.clone()
}
pub async fn list_roles_with_info(config_state: &ConfigState) -> Vec<(String, Option<String>)> {
let config = config_state.config.lock().await;
config
.roles
.iter()
.map(|(name, role)| (name.to_string(), role.shortname.clone()))
.collect()
}
pub async fn find_role_by_name_or_shortname(
config_state: &ConfigState,
query: &str,
) -> Option<RoleName> {
let config = config_state.config.lock().await;
let query_lower = query.to_lowercase();
for (name, _role) in config.roles.iter() {
if name.to_string().to_lowercase() == query_lower {
return Some(name.clone());
}
}
for (name, role) in config.roles.iter() {
if let Some(ref shortname) = role.shortname
&& shortname.to_lowercase() == query_lower
{
return Some(name.clone());
}
}
None
}
pub async fn save_config(config_state: &ConfigState) -> anyhow::Result<()> {
let config = config_state.config.lock().await;
config.save().await?;
Ok(())
}