use std::path::{PathBuf};
use log::{error, info, trace};
use crate::file::format_manager::FormatManager;
pub async fn convert(file: &PathBuf, fmt: &str) -> Result<(), Box<dyn std::error::Error>> {
trace!("Starting the conversion process.");
let manager = FormatManager::read().await;
let keys = match manager.load_keys(file, None) {
Ok(keys) => keys,
Err(e) => {
let error_message = format!("Failed to load keys from '{}': {:?}", file.to_str().unwrap(), e);
error!("{}", error_message);
return Err(Box::new(e));
}
};
let file_name = file.file_name().unwrap().to_str().unwrap();
let new_file_name = manager.get_new_file_name(fmt, Some(file_name));
let output_path = file.with_file_name(new_file_name);
info!("Output path: {}", output_path.to_str().unwrap());
if let Err(e) = manager.save_keys(&output_path, &keys, Some(fmt)) {
let error_message = format!("Failed to save keys to '{}': {:?}", output_path.to_str().unwrap(), e);
error!("{}", error_message);
return Err(Box::new(e));
}
info!("Successfully converted and synced secrets.");
Ok(())
}