Skip to content

Commit 4a896d1

Browse files
authored
create the directory specified by the config_dir before saving config files (#762)
1 parent 42df0ac commit 4a896d1

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

cli/src/cmd_auth.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@ impl CmdAuthLogin {
285285
let uid = user.id;
286286

287287
// Read / modify / write the credentials file.
288-
let credentials_path = ctx.client_config().config_dir().join("credentials.toml");
288+
let config_dir = ctx.client_config().config_dir();
289+
let credentials_path = config_dir.join("credentials.toml");
289290
let mut credentials =
290291
if let Ok(contents) = std::fs::read_to_string(credentials_path.clone()) {
291292
contents.parse::<toml_edit::DocumentMut>().unwrap()
@@ -325,6 +326,12 @@ impl CmdAuthLogin {
325326
profile.insert("token", toml_edit::value(token));
326327
profile.insert("user", toml_edit::value(uid.to_string()));
327328

329+
std::fs::create_dir_all(config_dir).unwrap_or_else(|_| {
330+
panic!(
331+
"unable to create config directory '{}'",
332+
config_dir.to_string_lossy()
333+
)
334+
});
328335
std::fs::write(credentials_path, credentials.to_string())
329336
.expect("unable to write credentials.toml");
330337

@@ -383,7 +390,8 @@ impl CmdAuthLogout {
383390
return Ok(());
384391
}
385392

386-
let credentials_path = ctx.client_config().config_dir().join("credentials.toml");
393+
let config_dir = ctx.client_config().config_dir();
394+
let credentials_path = config_dir.join("credentials.toml");
387395

388396
if self.all {
389397
// Clear the entire file for users who want to reset their known hosts.
@@ -408,6 +416,12 @@ impl CmdAuthLogout {
408416
let profiles = profiles.as_table_mut().unwrap();
409417
profiles.remove(profile_name);
410418
}
419+
std::fs::create_dir_all(config_dir).unwrap_or_else(|_| {
420+
panic!(
421+
"unable to create config directory '{}'",
422+
config_dir.to_string_lossy()
423+
)
424+
});
411425
std::fs::write(credentials_path, credentials.to_string())
412426
.expect("unable to write credentials.toml");
413427
println!(

cli/tests/test_auth.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,14 @@ fn test_auth_login_first() {
9191

9292
let temp_dir = tempfile::tempdir().unwrap().into_path();
9393

94+
// Make sure we know how to make non-existent directories.
95+
let config_dir = temp_dir.join(".config").join("oxide");
96+
9497
let cmd = Command::cargo_bin("oxide")
9598
.unwrap()
9699
.env("RUST_BACKTRACE", "1")
97100
.arg("--config-dir")
98-
.arg(temp_dir.as_os_str())
101+
.arg(config_dir.as_os_str())
99102
.arg("auth")
100103
.arg("login")
101104
.arg("--no-browser")
@@ -115,14 +118,14 @@ fn test_auth_login_first() {
115118
assert_contents(
116119
"tests/data/test_auth_login_first_credentials.toml",
117120
&scrub_server(
118-
read_to_string(temp_dir.join("credentials.toml")).unwrap(),
121+
read_to_string(config_dir.join("credentials.toml")).unwrap(),
119122
server.url(""),
120123
),
121124
);
122125

123126
assert_contents(
124127
"tests/data/test_auth_login_first_config.toml",
125-
&read_to_string(temp_dir.join("config.toml")).unwrap(),
128+
&read_to_string(config_dir.join("config.toml")).unwrap(),
126129
);
127130
}
128131

0 commit comments

Comments
 (0)