Skip to content

Commit

Permalink
switch from shellexapnd to custom env var expantion
Browse files Browse the repository at this point in the history
  • Loading branch information
meain committed Jan 21, 2020
1 parent c77db0e commit ac396ab
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 42 deletions.
37 changes: 3 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tiny/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ libtiny_wire = { path = "../libtiny_wire" }
log = "0.4"
serde = { version = "1.0.8", features = ["derive"] }
serde_yaml = "0.7.1"
shellexpand = "1.1.0"
regex = "1"
time = "0.1"
tokio = { version = "0.2.9", features = ["full"] }
31 changes: 24 additions & 7 deletions tiny/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use serde::{de::Error, Deserialize, Deserializer};
use regex::Regex;
use serde::{Deserialize, Deserializer};
use serde_yaml;
use std::env;
use std::fs;
use std::fs::File;
use std::io::{Read, Write};
Expand All @@ -11,7 +13,7 @@ pub(crate) struct SASLAuth {
pub(crate) password: String,
}

#[derive(Clone, Deserialize)]
#[derive(Clone, Deserialize, Debug)]
pub(crate) struct Server {
/// Address of the server
pub(crate) addr: String,
Expand Down Expand Up @@ -47,7 +49,7 @@ pub(crate) struct Server {
}

/// Similar to `Server`, but used when connecting via the `/connect` command.
#[derive(Clone, Deserialize)]
#[derive(Clone, Deserialize, Debug)]
pub(crate) struct Defaults {
pub(crate) nicks: Vec<String>,
pub(crate) realname: String,
Expand All @@ -57,22 +59,37 @@ pub(crate) struct Defaults {
pub(crate) tls: bool,
}

#[derive(Deserialize)]
#[derive(Deserialize, Debug)]
pub(crate) struct Config {
pub(crate) servers: Vec<Server>,
pub(crate) defaults: Defaults,
pub(crate) log_dir: Option<PathBuf>,
}

fn is_shell_var(val: &str) -> bool {
let re = Regex::new(r"^\$[A-z][A-z0-9_]+$").unwrap();
return re.is_match(val);
}

/// Expand env variables in the config (used primarily for pass)
fn with_expand_envs<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
match shellexpand::env(&s) {
Ok(value) => Ok(Some(value.to_string())),
Err(err) => Err(Error::custom(err)),
if is_shell_var(&s) {
return match env::var(&s[1..]) {
Ok(value) => Ok(Some(value.to_string())),
Err(_) => {
eprintln!("Tried expanding env variable {} but unable to find it. If this is your password and not and env variable, add the password as ${}", &s, &s);
std::process::exit(1);
}
};
} else {
if &s[0..2] == "$$" {
return Ok(Some(s[1..].to_string()));
}
Ok(Some(s))
}
}

Expand Down

0 comments on commit ac396ab

Please sign in to comment.