use anyhow::{Context, Result};
use std::fs;
use crate::config::config::get_config_storage_path;
use crate::config::types::{ConfigStorage, Configuration};
impl ConfigStorage {
pub fn load() -> Result<Self> {
let new_path = get_config_storage_path()?;
if new_path.exists() {
let content = fs::read_to_string(&new_path).with_context(|| {
format!(
"Failed to read configuration storage from {}",
new_path.display()
)
})?;
let storage: ConfigStorage = serde_json::from_str(&content)
.with_context(|| "Failed to parse configuration storage JSON")?;
return Ok(storage);
}
Ok(ConfigStorage::default())
}
pub fn save(&self) -> Result<()> {
let path = get_config_storage_path()?;
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)
.with_context(|| format!("Failed to create directory {}", parent.display()))?;
}
let json = serde_json::to_string_pretty(self)
.with_context(|| "Failed to serialize configuration storage")?;
fs::write(&path, json).with_context(|| format!("Failed to write to {}", path.display()))?;
Ok(())
}
pub fn migrate_from_old_path() -> Result<()> {
let new_path = get_config_storage_path()?;
let old_path = dirs::home_dir()
.map(|home| home.join(".cc_auto_switch").join("configurations.json"))
.ok_or_else(|| anyhow::anyhow!("Could not find home directory"))?;
if !old_path.exists() {
println!("âšī¸ No old configuration found at {}", old_path.display());
return Ok(());
}
println!("đ Migrating configuration from old location...");
let content = fs::read_to_string(&old_path).with_context(|| {
format!(
"Failed to read old configuration from {}",
old_path.display()
)
})?;
let storage: ConfigStorage = serde_json::from_str(&content)
.with_context(|| "Failed to parse old configuration storage JSON")?;
storage
.save()
.with_context(|| "Failed to save migrated configuration to new location")?;
if let Some(parent) = old_path.parent() {
fs::remove_dir_all(parent).with_context(|| {
format!(
"Failed to remove old configuration directory {}",
parent.display()
)
})?;
}
println!(
"â
Configuration migrated successfully to {}",
new_path.display()
);
Ok(())
}
pub fn add_configuration(&mut self, config: Configuration) {
self.configurations
.insert(config.alias_name.clone(), config);
}
pub fn remove_configuration(&mut self, alias_name: &str) -> bool {
self.configurations.remove(alias_name).is_some()
}
pub fn get_configuration(&self, alias_name: &str) -> Option<&Configuration> {
self.configurations.get(alias_name)
}
#[allow(dead_code)]
pub fn set_claude_settings_dir(&mut self, directory: String) {
self.claude_settings_dir = Some(directory);
}
#[allow(dead_code)]
pub fn get_claude_settings_dir(&self) -> Option<&String> {
self.claude_settings_dir.as_ref()
}
pub fn update_configuration(
&mut self,
old_alias: &str,
new_config: Configuration,
) -> Result<()> {
if !self.configurations.contains_key(old_alias) {
return Err(anyhow::anyhow!("Configuration '{}' not found", old_alias));
}
if old_alias != new_config.alias_name {
self.configurations.remove(old_alias);
}
self.configurations
.insert(new_config.alias_name.clone(), new_config);
Ok(())
}
}