summaryrefslogtreecommitdiff
path: root/src/util.rs
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-03-25 12:29:44 +0200
committerLars Wirzenius <liw@liw.fi>2021-03-25 13:16:13 +0200
commit00dc4a7a85b32c4337dd3672adec2cbfe3597bfe (patch)
tree20df497a743e8442f9725c2f5ab552747363a9d1 /src/util.rs
parent7d906d7f5436705ec3f2f3f5c4d8629c79f98fde (diff)
downloadvmadm-00dc4a7a85b32c4337dd3672adec2cbfe3597bfe.tar.gz
feat: allow ~/ in config, specification files
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs
index f3e104b..c641ea6 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -2,6 +2,7 @@
use log::debug;
use std::net::TcpStream;
+use std::path::{Path, PathBuf};
const SSH_PORT: i32 = 22;
@@ -15,3 +16,36 @@ pub fn wait_for_ssh(name: &str) {
}
}
}
+
+/// Expand a ~/ at the beginning of a Path to refer to the home directory.
+pub fn expand_tilde(path: &Path) -> PathBuf {
+ if path.starts_with("~/") {
+ if let Some(home) = std::env::var_os("HOME") {
+ let mut expanded = PathBuf::from(home);
+ for comp in path.components().skip(1) {
+ expanded.push(comp);
+ }
+ expanded
+ } else {
+ path.to_path_buf()
+ }
+ } else {
+ path.to_path_buf()
+ }
+}
+
+pub fn expand_optional_pathbuf(maybe_path: &mut Option<PathBuf>) {
+ if let Some(path) = maybe_path {
+ *maybe_path = Some(expand_tilde(path));
+ }
+}
+
+pub fn expand_optional_pathbufs(maybe_paths: &mut Option<Vec<PathBuf>>) {
+ if let Some(paths) = maybe_paths {
+ let mut expanded = vec![];
+ for path in paths {
+ expanded.push(expand_tilde(&path));
+ }
+ *maybe_paths = Some(expanded);
+ }
+}