use super::{Provider, ProviderUrl};
use crate::{Result, SecretSpecError};
use secrecy::SecretString;
use serde::{Deserialize, Serialize};
use std::env;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct EnvConfig {}
impl TryFrom<&ProviderUrl> for EnvConfig {
type Error = SecretSpecError;
fn try_from(url: &ProviderUrl) -> std::result::Result<Self, Self::Error> {
if url.scheme() != "env" {
return Err(SecretSpecError::ProviderOperationFailed(format!(
"Invalid scheme '{}' for env provider",
url.scheme()
)));
}
Ok(Self::default())
}
}
impl EnvConfig {}
pub struct EnvProvider {
#[allow(dead_code)]
config: EnvConfig,
}
crate::register_provider! {
struct: EnvProvider,
config: EnvConfig,
name: "env",
description: "Read-only environment variables",
schemes: ["env"],
examples: ["env://"],
}
impl EnvProvider {
pub fn new(config: EnvConfig) -> Self {
Self { config }
}
}
impl Provider for EnvProvider {
fn name(&self) -> &'static str {
Self::PROVIDER_NAME
}
fn uri(&self) -> String {
"env".to_string()
}
fn get(&self, _project: &str, key: &str, _profile: &str) -> Result<Option<SecretString>> {
Ok(env::var(key).ok().map(|v| SecretString::new(v.into())))
}
fn set(&self, _project: &str, _key: &str, _value: &SecretString, _profile: &str) -> Result<()> {
Err(crate::SecretSpecError::ProviderOperationFailed(
"Environment variable provider is read-only. Set variables in your shell or process environment.".to_string()
))
}
fn allows_set(&self) -> bool {
false
}
}