summaryrefslogtreecommitdiff
path: root/src/util.rs
diff options
context:
space:
mode:
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);
+ }
+}