diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-10-16 08:20:20 +0000 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-10-16 08:20:20 +0000 |
commit | fcca349375cf7b77e963d9c98a8aa5c263b9a7df (patch) | |
tree | e2e6e500848617920b53dd126c1392e4db49a2e2 | |
parent | 21521cfe1b0870e8552d19b022ac0c3902eb356e (diff) | |
parent | f7d5a9e11eed3f7f0769fbb154acb10bdc7fb58d (diff) | |
download | vmadm-fcca349375cf7b77e963d9c98a8aa5c263b9a7df.tar.gz |
Merge branch 'home-dir' into 'main'
feat: allow ~user/ in path names, not just ~/
See merge request larswirzenius/vmadm!47
-rw-r--r-- | Cargo.lock | 43 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/config.rs | 19 | ||||
-rw-r--r-- | src/spec.rs | 12 | ||||
-rw-r--r-- | src/util.rs | 28 |
5 files changed, 70 insertions, 33 deletions
@@ -50,6 +50,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "81a18687293a1546b67c246452202bbbf143d239cb43494cc163da14979082da" [[package]] +name = "cc" +version = "1.0.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79c2681d6594606957bbb8631c4b90a7fcaaa72cdb714743a437b156d6a7eedd" + +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + +[[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -76,7 +88,7 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "339ee130d97a610ea5a5872d2bbb130fdf68884ff09d3028b81bec8a1ac23bbc" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "dirs-sys-next", ] @@ -116,7 +128,7 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "wasi", ] @@ -140,6 +152,16 @@ dependencies = [ ] [[package]] +name = "home-dir" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "572c4f9690c3ac7058775be826d1b508ca20fd6633261435c27ac30ad0598212" +dependencies = [ + "nix", + "thiserror", +] + +[[package]] name = "humantime" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -178,7 +200,7 @@ version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -188,6 +210,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" [[package]] +name = "nix" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83450fe6a6142ddd95fb064b746083fc4ef1705fe81f64a64e1d4b39f54a1055" +dependencies = [ + "bitflags", + "cc", + "cfg-if 0.1.10", + "libc", +] + +[[package]] name = "once_cell" version = "1.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -445,7 +479,7 @@ version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "rand", "redox_syscall", @@ -546,6 +580,7 @@ dependencies = [ "anyhow", "bytesize", "directories-next", + "home-dir", "log", "pretty_env_logger", "serde", @@ -14,6 +14,7 @@ repository = "https://gitlab.com/larswirzenius/vmadm" anyhow = "1" bytesize = "1" directories-next = "2" +home-dir = "0.1.0" log = "0.4" pretty_env_logger = "0.4" serde = { version = "1", features = ["derive"] } 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(()) } |