-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig_from_env.rs
111 lines (89 loc) · 3.03 KB
/
config_from_env.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use std::env;
use anyhow::Result;
use thiserror::Error;
use crate::config_file::get_env_var;
const ZOO_HOST: &str = "ZOO_HOST";
const ZOO_TOKEN: &str = "ZOO_TOKEN";
pub struct EnvConfig<'a> {
pub config: &'a mut (dyn crate::config::Config + 'a),
}
impl EnvConfig<'_> {
pub fn inherit_env(config: &mut dyn crate::config::Config) -> EnvConfig {
EnvConfig { config }
}
}
#[derive(Error, Debug)]
pub enum ReadOnlyEnvVarError {
#[error("read-only value in: {0}")]
Variable(String),
}
impl crate::config::Config for EnvConfig<'_> {
fn get(&self, hostname: &str, key: &str) -> Result<String> {
let (val, _) = self.get_with_source(hostname, key)?;
Ok(val)
}
fn get_with_source(&self, hostname: &str, key: &str) -> Result<(String, String)> {
// If they are asking specifically for the token, return the value.
if key == "token" {
let token = get_env_var(ZOO_TOKEN);
if !token.is_empty() {
return Ok((token, ZOO_TOKEN.to_string()));
}
} else {
let var = format!("ZOO_{}", heck::AsShoutySnakeCase(key));
let val = get_env_var(&var);
if !val.is_empty() {
return Ok((val, var));
}
}
self.config.get_with_source(hostname, key)
}
fn set(&mut self, hostname: &str, key: &str, value: Option<&str>) -> Result<()> {
self.config.set(hostname, key, value)
}
fn unset_host(&mut self, key: &str) -> Result<()> {
self.config.unset_host(key)
}
fn hosts(&self) -> Result<Vec<String>> {
self.config.hosts()
}
fn default_host(&self) -> Result<String> {
let (host, _) = self.default_host_with_source()?;
Ok(host)
}
fn default_host_with_source(&self) -> Result<(String, String)> {
if let Ok(host) = env::var(ZOO_HOST) {
Ok((host, ZOO_HOST.to_string()))
} else {
self.config.default_host_with_source()
}
}
fn aliases(&mut self) -> Result<crate::config_alias::AliasConfig> {
self.config.aliases()
}
fn save_aliases(&mut self, aliases: &crate::config_map::ConfigMap) -> Result<()> {
self.config.save_aliases(aliases)
}
fn expand_alias(&mut self, args: Vec<String>) -> Result<(Vec<String>, bool)> {
self.config.expand_alias(args)
}
fn check_writable(&self, hostname: &str, key: &str) -> Result<()> {
// If they are asking specifically for the token, return the value.
if key == "token" {
let token = get_env_var(ZOO_TOKEN);
if !token.is_empty() {
return Err(ReadOnlyEnvVarError::Variable(ZOO_TOKEN.to_string()).into());
}
}
self.config.check_writable(hostname, key)
}
fn write(&self) -> Result<()> {
self.config.write()
}
fn config_to_string(&self) -> Result<String> {
self.config.config_to_string()
}
fn hosts_to_string(&self) -> Result<String> {
self.config.hosts_to_string()
}
}