summaryrefslogtreecommitdiff
path: root/src/util.rs
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-03-25 11:18:33 +0000
committerLars Wirzenius <liw@liw.fi>2021-03-25 11:18:33 +0000
commit228758b795b3c27d5fd146e380774b29db73432f (patch)
tree20df497a743e8442f9725c2f5ab552747363a9d1 /src/util.rs
parenta45450a42e4cdcd7f2d5671984c9c7f3945131fd (diff)
parent00dc4a7a85b32c4337dd3672adec2cbfe3597bfe (diff)
downloadvmadm-228758b795b3c27d5fd146e380774b29db73432f.tar.gz
Merge branch 'tilde' into 'main'
test: don't hide clippy output if it's successful See merge request larswirzenius/vmadm!30
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);
+ }
+}