From e7d3384e2bf66d2fc369e705ebb9e762e79b8b8c Mon Sep 17 00:00:00 2001 From: Adam Leventhal Date: Tue, 23 Jul 2024 18:50:43 -0700 Subject: [PATCH] create the directory specified by the config_dir before saving config files (#762) --- cli/src/cmd_auth.rs | 18 ++++++++++++++++-- cli/tests/test_auth.rs | 9 ++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/cli/src/cmd_auth.rs b/cli/src/cmd_auth.rs index fb04df48..e48fe0da 100644 --- a/cli/src/cmd_auth.rs +++ b/cli/src/cmd_auth.rs @@ -283,7 +283,8 @@ impl CmdAuthLogin { let uid = user.id; // Read / modify / write the credentials file. - let credentials_path = ctx.client_config().config_dir().join("credentials.toml"); + let config_dir = ctx.client_config().config_dir(); + let credentials_path = config_dir.join("credentials.toml"); let mut credentials = if let Ok(contents) = std::fs::read_to_string(credentials_path.clone()) { contents.parse::().unwrap() @@ -323,6 +324,12 @@ impl CmdAuthLogin { profile.insert("token", toml_edit::value(token)); profile.insert("user", toml_edit::value(uid.to_string())); + std::fs::create_dir_all(config_dir).unwrap_or_else(|_| { + panic!( + "unable to create config directory '{}'", + config_dir.to_string_lossy() + ) + }); std::fs::write(credentials_path, credentials.to_string()) .expect("unable to write credentials.toml"); @@ -381,7 +388,8 @@ impl CmdAuthLogout { return Ok(()); } - let credentials_path = ctx.client_config().config_dir().join("credentials.toml"); + let config_dir = ctx.client_config().config_dir(); + let credentials_path = config_dir.join("credentials.toml"); if self.all { // Clear the entire file for users who want to reset their known hosts. @@ -406,6 +414,12 @@ impl CmdAuthLogout { let profiles = profiles.as_table_mut().unwrap(); profiles.remove(profile_name); } + std::fs::create_dir_all(config_dir).unwrap_or_else(|_| { + panic!( + "unable to create config directory '{}'", + config_dir.to_string_lossy() + ) + }); std::fs::write(credentials_path, credentials.to_string()) .expect("unable to write credentials.toml"); println!( diff --git a/cli/tests/test_auth.rs b/cli/tests/test_auth.rs index c36313b2..44dac705 100644 --- a/cli/tests/test_auth.rs +++ b/cli/tests/test_auth.rs @@ -91,11 +91,14 @@ fn test_auth_login_first() { let temp_dir = tempfile::tempdir().unwrap().into_path(); + // Make sure we know how to make non-existent directories. + let config_dir = temp_dir.join(".config").join("oxide"); + let cmd = Command::cargo_bin("oxide") .unwrap() .env("RUST_BACKTRACE", "1") .arg("--config-dir") - .arg(temp_dir.as_os_str()) + .arg(config_dir.as_os_str()) .arg("auth") .arg("login") .arg("--no-browser") @@ -115,14 +118,14 @@ fn test_auth_login_first() { assert_contents( "tests/data/test_auth_login_first_credentials.toml", &scrub_server( - read_to_string(temp_dir.join("credentials.toml")).unwrap(), + read_to_string(config_dir.join("credentials.toml")).unwrap(), server.url(""), ), ); assert_contents( "tests/data/test_auth_login_first_config.toml", - &read_to_string(temp_dir.join("config.toml")).unwrap(), + &read_to_string(config_dir.join("config.toml")).unwrap(), ); }