-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Removed closure from logout (#6251)
### Description Removed the closure from logout to simplify the type checking and in preparation for our errors refactor ### Testing Instructions <!-- Give a quick description of steps to test your changes. --> Closes TURBO-1520 Co-authored-by: nicholaslyang <Nicholas Yang>
- Loading branch information
1 parent
9c51873
commit 338cdb7
Showing
2 changed files
with
30 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,5 @@ | ||
use anyhow::Result; | ||
use tracing::error; | ||
use turborepo_ui::{GREY, UI}; | ||
use turborepo_ui::{cprintln, GREY, UI}; | ||
|
||
pub fn logout<F>(ui: &UI, mut set_token: F) -> Result<()> | ||
where | ||
F: FnMut() -> Result<()>, | ||
{ | ||
if let Err(err) = set_token() { | ||
error!("could not logout. Something went wrong: {}", err); | ||
return Err(err); | ||
} | ||
|
||
println!("{}", ui.apply(GREY.apply_to(">>> Logged out"))); | ||
Ok(()) | ||
pub fn logout(ui: &UI) { | ||
cprintln!(ui, GREY, ">>> Logged out"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,37 @@ | ||
use anyhow::{anyhow, Error, Result}; | ||
use tracing::error; | ||
use turborepo_auth::logout as auth_logout; | ||
|
||
use crate::{commands::CommandBase, rewrite_json::unset_path}; | ||
|
||
pub fn logout(base: &mut CommandBase) -> Result<()> { | ||
let ui = base.ui; | ||
if let Err(err) = remove_token(base) { | ||
error!("could not logout. Something went wrong: {}", err); | ||
return Err(err); | ||
} | ||
|
||
// Passing a closure here while we figure out how to make turborepo-auth | ||
// crate manage its own configuration for the path to the token. | ||
let set_token = || -> Result<(), Error> { | ||
let global_config_path = base.global_config_path()?; | ||
let before = global_config_path | ||
.read_existing_to_string_or(Ok("{}")) | ||
.map_err(|e| { | ||
anyhow!( | ||
"Encountered an IO error while attempting to read {}: {}", | ||
global_config_path, | ||
e | ||
) | ||
})?; | ||
auth_logout(&base.ui); | ||
|
||
if let Some(after) = unset_path(&before, &["token"], true)? { | ||
global_config_path | ||
.create_with_contents(after) | ||
.map_err(Error::from) | ||
} else { | ||
Ok(()) | ||
} | ||
}; | ||
Ok(()) | ||
} | ||
|
||
fn remove_token(base: &mut CommandBase) -> Result<()> { | ||
let global_config_path = base.global_config_path()?; | ||
let before = global_config_path | ||
.read_existing_to_string_or(Ok("{}")) | ||
.map_err(|e| { | ||
anyhow!( | ||
"Encountered an IO error while attempting to read {}: {}", | ||
global_config_path, | ||
e | ||
) | ||
})?; | ||
|
||
auth_logout(&ui, set_token) | ||
if let Some(after) = unset_path(&before, &["token"], true)? { | ||
global_config_path | ||
.create_with_contents(after) | ||
.map_err(Error::from) | ||
} else { | ||
Ok(()) | ||
} | ||
} |