-
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.
chore: move logout functionality into auth crate
- Loading branch information
Showing
7 changed files
with
67 additions
and
47 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,5 +1,34 @@ | ||
use turborepo_ui::{cprintln, GREY, UI}; | ||
use std::fs; | ||
|
||
pub fn logout(ui: &UI) { | ||
cprintln!(ui, GREY, ">>> Logged out"); | ||
use tracing::error; | ||
use turborepo_api_client::Client; | ||
use turborepo_ui::{cprintln, GREY}; | ||
|
||
use crate::{Error, LogoutOptions}; | ||
|
||
pub fn logout<T: Client>(options: &LogoutOptions<T>) -> Result<(), Error> { | ||
if let Err(err) = remove_token(options) { | ||
error!("could not logout. Something went wrong: {}", err); | ||
return Err(err); | ||
} | ||
|
||
cprintln!(options.ui, GREY, ">>> Logged out"); | ||
Ok(()) | ||
} | ||
|
||
fn remove_token<T: Client>(options: &LogoutOptions<T>) -> Result<(), Error> { | ||
let global_config_path = options.path; | ||
let content = | ||
fs::read_to_string(global_config_path).map_err(|e| Error::FailedToReadAuthFilePath { | ||
path: global_config_path.clone(), | ||
error: e, | ||
})?; | ||
|
||
let mut data: serde_json::Value = serde_json::from_str(&content)?; | ||
data["token"] = serde_json::Value::Null; | ||
|
||
let new_content = serde_json::to_string_pretty(&data)?; | ||
fs::write(global_config_path, new_content)?; | ||
|
||
Ok(()) | ||
} |
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
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
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
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,39 +1,13 @@ | ||
use tracing::error; | ||
use turborepo_auth::logout as auth_logout; | ||
use turborepo_auth::{logout as auth_logout, LogoutOptions}; | ||
use turborepo_telemetry::events::command::CommandEventBuilder; | ||
|
||
use crate::{cli::Error, commands::CommandBase, config, rewrite_json::unset_path}; | ||
use crate::{cli::Error, commands::CommandBase}; | ||
|
||
pub fn logout(base: &mut CommandBase, _telemetry: CommandEventBuilder) -> Result<(), Error> { | ||
if let Err(err) = remove_token(base) { | ||
error!("could not logout. Something went wrong: {}", err); | ||
return Err(err); | ||
} | ||
|
||
auth_logout(&base.ui); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn remove_token(base: &mut CommandBase) -> Result<(), Error> { | ||
let global_config_path = base.global_config_path()?; | ||
let before = global_config_path | ||
.read_existing_to_string_or(Ok("{}")) | ||
.map_err(|e| { | ||
Error::Config(config::Error::FailedToReadConfig { | ||
config_path: global_config_path.clone(), | ||
error: e, | ||
}) | ||
})?; | ||
|
||
if let Some(after) = unset_path(&before, &["token"], true)? { | ||
global_config_path.create_with_contents(after).map_err(|e| { | ||
Error::Config(config::Error::FailedToSetConfig { | ||
config_path: global_config_path.clone(), | ||
error: e, | ||
}) | ||
}) | ||
} else { | ||
Ok(()) | ||
} | ||
auth_logout(&LogoutOptions { | ||
ui: &base.ui, | ||
api_client: &base.api_client()?, | ||
path: &base.global_config_path()?, | ||
}) | ||
.map_err(Error::from) | ||
} |