use std::path::Path;
use artifactize_provider::auth::providers as provider;
pub use provider::{
ApiKey, Custom, CustomView, EndpointUrl, KeyChange, KeySource, KeyStatus, Providers,
};
use super::{credential_location, storage::Tokens, writable_credential_location};
use crate::config::{Backend, CustomBackendName, HttpProvider, Provider};
pub fn key_variable(builtin: Provider) -> Option<&'static str> {
HttpProvider::try_from(builtin)
.ok()
.and_then(provider::key_variable)
}
pub fn list(state: Option<&Path>, repo: Option<&Path>) -> Result<Providers, String> {
provider::list(&credential_location(state, repo, Tokens::Providers)?)
}
pub async fn save_key(
state: Option<&Path>,
repo: Option<&Path>,
builtin: Provider,
key: ApiKey,
) -> Result<(), String> {
let http = HttpProvider::try_from(builtin).map_err(|_| {
format!("{builtin} signs in instead of using a key: artifactize login {builtin}.")
})?;
provider::save_key(
&writable_credential_location(state, repo, Tokens::Providers)?,
http,
key,
)
.await
}
pub async fn remove_key(
state: Option<&Path>,
repo: Option<&Path>,
builtin: Provider,
) -> Result<(), String> {
provider::remove_key(
&writable_credential_location(state, repo, Tokens::Providers)?,
builtin.try_into()?,
)
.await
}
pub async fn save_custom(
state: Option<&Path>,
repo: Option<&Path>,
name: Backend,
custom: Custom,
key: KeyChange,
) -> Result<(), String> {
let name: CustomBackendName = name.as_str().parse().map_err(|error| {
if name.is_reserved_custom() {
format!("{name} is reserved; give the custom provider another name.")
} else {
error
}
})?;
provider::save_custom(
&writable_credential_location(state, repo, Tokens::Providers)?,
name,
custom,
key,
)
.await
}
pub async fn remove_custom(
state: Option<&Path>,
repo: Option<&Path>,
name: &Backend,
) -> Result<(), String> {
let name = name
.as_str()
.parse()
.map_err(|_| format!("No custom provider is named {name}."))?;
provider::remove_custom(
&writable_credential_location(state, repo, Tokens::Providers)?,
&name,
)
.await
}