use crate::config::{Config, ProviderSource};
use anyhow::{anyhow, Context};
use console::style;
use std::path::Path;
pub fn add_provider_to_config(
workspace: &Path,
provider_source: ProviderSource,
file: &Path,
) -> anyhow::Result<()> {
if !provider_source.correctly_configured() {
return Err(anyhow!("Provider is not correctly configured"));
}
let path_to_config = workspace.join(file);
let config = Config::new(vec![path_to_config]);
let mut sources = config.read().with_context(|| "Error reading config file")?;
if sources.iter().any(|s| s == &provider_source) {
println!("Entry already exists, skipping");
} else {
println!(
"Adding {} to {}",
provider_source,
style(&workspace.join(file).display()).green()
);
sources.push(provider_source);
config
.write(sources, &workspace.join(file))
.with_context(|| "Error writing config file")?;
}
Ok(())
}