From 1ecef6fc16e941035ae2844c5b7d6d662e962b31 Mon Sep 17 00:00:00 2001 From: Christian Visintin Date: Tue, 28 Feb 2023 10:45:07 +0100 Subject: [PATCH] issue 150: some issues with configuration (#151) * issue 150: some issues with configuration --- CHANGELOG.md | 9 +++++++++ src/config/params.rs | 9 ++++++++- src/system/config_client.rs | 11 +++++++++-- src/system/environment.rs | 13 +++++++------ 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bc47c2..347fb33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog - [Changelog](#changelog) + - [0.11.1](#0111) - [0.11.0](#0110) - [0.10.0](#0100) - [0.9.0](#090) @@ -27,6 +28,14 @@ --- +## 0.11.1 + +Released on ?? + +- [Issue 150](https://github.com/veeso/termscp/issues/150) + - fixed config directory not being created + - before setting default ssh config path; check wheter it actually exists + ## 0.11.0 Released on 20/02/2023 diff --git a/src/config/params.rs b/src/config/params.rs index 579f095..9052598 100644 --- a/src/config/params.rs +++ b/src/config/params.rs @@ -52,8 +52,15 @@ impl Default for RemoteConfig { let mut ssh_config_path = "~/.ssh/config".to_string(); ssh_config_path = ssh_config_path.replacen('~', &home_dir.to_string_lossy(), 1); + // check if ssh config path exists first + let ssh_config_path = if PathBuf::from(&ssh_config_path).exists() { + Some(ssh_config_path) + } else { + None + }; + Self { - ssh_config: Some(ssh_config_path), + ssh_config: ssh_config_path, ssh_keys: HashMap::default(), } } diff --git a/src/system/config_client.rs b/src/system/config_client.rs index 1938e54..fdfa93d 100644 --- a/src/system/config_client.rs +++ b/src/system/config_client.rs @@ -645,7 +645,7 @@ mod tests { } #[test] - fn test_system_config_remote_ssh_config() { + fn should_get_and_set_ssh_config_dir() { let tmp_dir: TempDir = TempDir::new().ok().unwrap(); let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path()); let mut client: ConfigClient = ConfigClient::new(cfg_path.as_path(), key_path.as_path()) @@ -655,7 +655,14 @@ mod tests { let home_dir = dirs::home_dir().unwrap_or_else(|| PathBuf::from("/root")); let mut ssh_config_path = "~/.ssh/config".to_string(); ssh_config_path = ssh_config_path.replacen('~', &home_dir.to_string_lossy(), 1); - assert_eq!(client.get_ssh_config(), Some(ssh_config_path.as_str())); // Null ? + + let ssh_config_path = if PathBuf::from(&ssh_config_path).exists() { + Some(ssh_config_path) + } else { + None + }; + + assert_eq!(client.get_ssh_config(), ssh_config_path.as_deref()); // Null ? client.set_ssh_config(Some(String::from("/tmp/ssh_config"))); assert_eq!(client.get_ssh_config(), Some("/tmp/ssh_config")); client.set_ssh_config(None); diff --git a/src/system/environment.rs b/src/system/environment.rs index 7839bf1..b374dcb 100644 --- a/src/system/environment.rs +++ b/src/system/environment.rs @@ -25,12 +25,13 @@ pub fn init_config_dir() -> Result, String> { // Append termscp dir p.push("termscp/"); // If directory doesn't exist, create it - match p.exists() { - true => Ok(Some(p)), - false => match std::fs::create_dir(p.as_path()) { - Ok(_) => Ok(Some(p)), - Err(err) => Err(err.to_string()), - }, + if p.exists() { + return Ok(Some(p)); + } + // directory doesn't exist; create dir recursively + match std::fs::create_dir_all(p.as_path()) { + Ok(_) => Ok(Some(p)), + Err(err) => Err(err.to_string()), } } else { Ok(None)