git_workspace/commands/
add_provider.rs1use crate::config::{Config, ProviderSource};
2use anyhow::{anyhow, Context};
3use console::style;
4use std::path::Path;
5
6pub fn add_provider_to_config(
8 workspace: &Path,
9 provider_source: ProviderSource,
10 file: &Path,
11) -> anyhow::Result<()> {
12 if !provider_source.correctly_configured() {
13 return Err(anyhow!("Provider is not correctly configured"));
14 }
15 let path_to_config = workspace.join(file);
16 let config = Config::new(vec![path_to_config]);
18 let mut sources = config.read().with_context(|| "Error reading config file")?;
19 if sources.iter().any(|s| s == &provider_source) {
21 println!("Entry already exists, skipping");
22 } else {
23 println!(
24 "Adding {} to {}",
25 provider_source,
26 style(&workspace.join(file).display()).green()
27 );
28 sources.push(provider_source);
30 config
31 .write(sources, &workspace.join(file))
32 .with_context(|| "Error writing config file")?;
33 }
34 Ok(())
35}