1#![allow(deprecated)]
19
20use crate::constants::*;
21use ini::Ini;
22use reqsign_core::Context;
23use reqsign_core::Result;
24use reqsign_core::utils::Redact;
25use std::fmt::{Debug, Formatter};
26
27#[derive(Clone, Default)]
29#[deprecated(
30 since = "0.1.0",
31 note = "Config is no longer needed. Use specific credential providers instead"
32)]
33pub struct Config {
34 pub user: Option<String>,
36 pub tenancy: Option<String>,
38 pub region: Option<String>,
40 pub key_file: Option<String>,
42 pub fingerprint: Option<String>,
44 pub config_file: Option<String>,
46 pub profile: Option<String>,
48}
49
50impl Debug for Config {
51 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
52 f.debug_struct("Config")
53 .field("user", &self.user)
54 .field("tenancy", &self.tenancy)
55 .field("region", &self.region)
56 .field("key_file", &Redact::from(&self.key_file))
57 .field("fingerprint", &self.fingerprint)
58 .field("config_file", &self.config_file)
59 .field("profile", &self.profile)
60 .finish()
61 }
62}
63
64impl Config {
65 pub fn from_env(ctx: &Context) -> Self {
67 Self {
68 user: ctx.env_var(ORACLE_USER),
69 tenancy: ctx.env_var(ORACLE_TENANCY),
70 region: ctx.env_var(ORACLE_REGION),
71 key_file: ctx.env_var(ORACLE_KEY_FILE),
72 fingerprint: ctx.env_var(ORACLE_FINGERPRINT),
73 config_file: ctx.env_var(ORACLE_CONFIG_FILE),
74 profile: ctx.env_var(ORACLE_PROFILE),
75 }
76 }
77
78 pub async fn from_config_file(ctx: &Context, path: &str, profile: &str) -> Result<Self> {
80 let content = ctx.file_read_as_string(path).await?;
81 let ini = Ini::read_from(&mut content.as_bytes()).map_err(|e| {
82 reqsign_core::Error::config_invalid(format!("Failed to parse config file: {e}"))
83 })?;
84 let section = ini.section(Some(profile)).ok_or_else(|| {
85 reqsign_core::Error::config_invalid(format!(
86 "Profile {profile} not found in config file"
87 ))
88 })?;
89
90 Ok(Self {
91 user: section.get("user").map(|s| s.to_string()),
92 tenancy: section.get("tenancy").map(|s| s.to_string()),
93 region: section.get("region").map(|s| s.to_string()),
94 key_file: section.get("key_file").map(|s| s.to_string()),
95 fingerprint: section.get("fingerprint").map(|s| s.to_string()),
96 config_file: Some(path.to_string()),
97 profile: Some(profile.to_string()),
98 })
99 }
100}