summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-10-16 11:18:48 +0300
committerLars Wirzenius <liw@liw.fi>2021-10-16 11:18:48 +0300
commitf7d5a9e11eed3f7f0769fbb154acb10bdc7fb58d (patch)
treee2e6e500848617920b53dd126c1392e4db49a2e2 /src
parent21521cfe1b0870e8552d19b022ac0c3902eb356e (diff)
downloadvmadm-f7d5a9e11eed3f7f0769fbb154acb10bdc7fb58d.tar.gz
feat: allow ~user/ in path names, not just ~/
Add the home-dir crate as a dependency for tilde expansion. Sponsored-by: author
Diffstat (limited to 'src')
-rw-r--r--src/config.rs19
-rw-r--r--src/spec.rs12
-rw-r--r--src/util.rs28
3 files changed, 30 insertions, 29 deletions
diff --git a/src/config.rs b/src/config.rs
index 6829151..9e91eb4 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -53,6 +53,10 @@ pub enum ConfigurationError {
/// YAML parsing error.
#[error(transparent)]
YamlError(#[from] serde_yaml::Error),
+
+ /// Error expanding a ~user in a path name.
+ #[error(transparent)]
+ HomeDirError(#[from] home_dir::Error)
}
impl Configuration {
@@ -63,7 +67,7 @@ impl Configuration {
let config = fs::read(filename)
.map_err(|err| ConfigurationError::ReadError(filename.to_path_buf(), err))?;
let mut config: Configuration = serde_yaml::from_slice(&config)?;
- config.expand_tildes();
+ config.expand_tildes()?;
config.fill_in_missing_networks();
debug!("config: {:#?}", config);
Ok(config)
@@ -78,11 +82,12 @@ impl Configuration {
}
}
- fn expand_tildes(&mut self) {
- expand_optional_pathbuf(&mut self.default_base_image);
- expand_optional_pathbuf(&mut self.image_directory);
- expand_optional_pathbuf(&mut self.image_directory);
- expand_optional_pathbuf(&mut self.ca_key);
- expand_optional_pathbufs(&mut self.authorized_keys)
+ fn expand_tildes(&mut self) -> Result<(), home_dir::Error> {
+ expand_optional_pathbuf(&mut self.default_base_image)?;
+ expand_optional_pathbuf(&mut self.image_directory)?;
+ expand_optional_pathbuf(&mut self.image_directory)?;
+ expand_optional_pathbuf(&mut self.ca_key)?;
+ expand_optional_pathbufs(&mut self.authorized_keys)?;
+ Ok(())
}
}
diff --git a/src/spec.rs b/src/spec.rs
index 5dc7714..87e59a2 100644
--- a/src/spec.rs
+++ b/src/spec.rs
@@ -245,6 +245,10 @@ pub enum SpecificationError {
/// Error parsing YAML.
#[error(transparent)]
YamlError(#[from] serde_yaml::Error),
+
+ /// Error expanding a ~user in a path name.
+ #[error(transparent)]
+ HomeDirError(#[from] home_dir::Error)
}
impl Specification {
@@ -284,7 +288,7 @@ impl Specification {
let key_filenames = input.ssh_key_files(config, name)?;
let ssh_keys = ssh_keys(&key_filenames)?;
let ca_key = if let Some(filename) = &input.ca_key {
- Some(expand_tilde(filename))
+ Some(expand_tilde(filename)?)
} else {
config.ca_key.clone()
};
@@ -307,8 +311,8 @@ impl Specification {
ecdsa_host_cert: input.ecdsa_host_cert.clone(),
ed25519_host_key: input.ed25519_host_key.clone(),
ed25519_host_cert: input.ed25519_host_cert.clone(),
- base: expand_tilde(&input.base_image(config, name)?),
- image: expand_tilde(&input.image(config, name)?),
+ base: expand_tilde(&input.base_image(config, name)?)?,
+ image: expand_tilde(&input.image(config, name)?)?,
image_size_gib: input.image_size_gib(config, name)?,
memory_mib: input.memory_mib(config, name)?,
cpus: input.cpus(config, name)?,
@@ -326,7 +330,7 @@ impl Specification {
fn ssh_keys(filenames: &[PathBuf]) -> Result<Vec<String>, SpecificationError> {
let mut keys = vec![];
for filename in filenames {
- let filename = expand_tilde(filename);
+ let filename = expand_tilde(filename)?;
let key =
std::fs::read(&filename).map_err(|e| SpecificationError::SshKeyRead(filename, e))?;
let key = String::from_utf8(key)?;
diff --git a/src/util.rs b/src/util.rs
index 9a86ea0..2db4390 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -1,5 +1,7 @@
//! Utilities.
+use home_dir::HomeDirExt;
+
use log::debug;
use std::net::TcpStream;
use std::path::{Path, PathBuf};
@@ -18,34 +20,24 @@ 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_tilde(path: &Path) -> Result<PathBuf, home_dir::Error> {
+ path.expand_home()
}
-pub fn expand_optional_pathbuf(maybe_path: &mut Option<PathBuf>) {
+pub fn expand_optional_pathbuf(maybe_path: &mut Option<PathBuf>) -> Result<(), home_dir::Error> {
if let Some(path) = maybe_path {
- *maybe_path = Some(expand_tilde(path));
+ *maybe_path = Some(expand_tilde(path)?);
}
+ Ok(())
}
-pub fn expand_optional_pathbufs(maybe_paths: &mut Option<Vec<PathBuf>>) {
+pub fn expand_optional_pathbufs(maybe_paths: &mut Option<Vec<PathBuf>>) -> Result<(), home_dir::Error> {
if let Some(paths) = maybe_paths {
let mut expanded = vec![];
for path in paths {
- expanded.push(expand_tilde(path));
+ expanded.push(expand_tilde(path)?);
}
*maybe_paths = Some(expanded);
}
+ Ok(())
}