From e3d61f45c211d5397a2f12b7fbcb3829a8e0fab7 Mon Sep 17 00:00:00 2001 From: steebchen Date: Tue, 17 Sep 2024 14:47:26 -0400 Subject: [PATCH] Add environment variable support to credential loading Load credentials from SLOT_AUTH env variable if set, else from file. This allows for easier configuration management and flexibility in different deployment environments. --- slot/src/credential.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/slot/src/credential.rs b/slot/src/credential.rs index 8594e2f..7c47ab2 100644 --- a/slot/src/credential.rs +++ b/slot/src/credential.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use std::fs; use std::path::{Path, PathBuf}; +use std::{env, fs}; use crate::account::Account; use crate::error::Error; @@ -65,13 +65,19 @@ impl Credentials { } pub(crate) fn load_at>(config_dir: P) -> Result { - let path = get_file_path(config_dir); + let content: String; + if env::var("SLOT_AUTH").is_ok() { + content = env::var("SLOT_AUTH").unwrap(); + } else { + let path = get_file_path(config_dir); + + if !path.exists() { + return Err(Error::Unauthorized); + } - if !path.exists() { - return Err(Error::Unauthorized); + content = fs::read_to_string(path)?; } - let content = fs::read_to_string(path)?; let credentials = serde_json::from_str::(&content); match credentials {