Skip to content

Commit

Permalink
create the directory specified by the config_dir before saving config…
Browse files Browse the repository at this point in the history
… files (#762)
  • Loading branch information
ahl committed Jul 24, 2024
1 parent 040f1b0 commit e7d3384
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
18 changes: 16 additions & 2 deletions cli/src/cmd_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<toml_edit::DocumentMut>().unwrap()
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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.
Expand All @@ -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!(
Expand Down
9 changes: 6 additions & 3 deletions cli/tests/test_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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(),
);
}

Expand Down

0 comments on commit e7d3384

Please sign in to comment.