Skip to content

Commit

Permalink
refactor: Removed closure from logout (#6251)
Browse files Browse the repository at this point in the history
### 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
NicholasLYang committed Oct 23, 2023
1 parent 9c51873 commit 338cdb7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 37 deletions.
17 changes: 3 additions & 14 deletions crates/turborepo-auth/src/auth/logout.rs
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");
}
50 changes: 27 additions & 23 deletions crates/turborepo-lib/src/commands/logout.rs
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(())
}
}

0 comments on commit 338cdb7

Please sign in to comment.