diff options
author | Ophir LOJKINE <ophir.lojkine@auto-grid.com> | 2019-03-12 10:19:52 +0100 |
---|---|---|
committer | Ophir LOJKINE <ophir.lojkine@auto-grid.com> | 2019-03-12 10:19:52 +0100 |
commit | 505fbbb03fce34153a6e52f7aa046d17927d14fb (patch) | |
tree | 0b77d32c6ffabdb77ba6696d7201c4ecd487648b /src/external.rs | |
parent | b470aa5f6624ccb1f30fff6d78f096fecd7d45d6 (diff) |
Allow environment variables in verbs
See #20
Diffstat (limited to 'src/external.rs')
-rw-r--r-- | src/external.rs | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/src/external.rs b/src/external.rs index c5b20db..3d9e029 100644 --- a/src/external.rs +++ b/src/external.rs @@ -1,3 +1,4 @@ +use std::env; use std::fs::OpenOptions; use std::io::{self, Write}; use std::path::{Path, PathBuf}; @@ -17,6 +18,15 @@ pub struct Launchable { pub just_print: bool, // this part of the API will change } +/// If s starts by a '$', replace it by the environment variable of the same name +fn resolve_env_variable(s: String) -> String { + if s.starts_with('$') { + env::var(&s[1..]).unwrap_or(s) + } else { + s + } +} + impl Launchable { pub fn opener(path: &PathBuf) -> io::Result<Launchable> { Launchable::from(vec![ @@ -25,7 +35,7 @@ impl Launchable { ]) } pub fn from(mut parts: Vec<String>) -> io::Result<Launchable> { - let mut parts = parts.drain(0..); + let mut parts = parts.drain(0..).map(resolve_env_variable); match parts.next() { Some(exe) => Ok(Launchable { exe, |