Skip to content

Commit

Permalink
chore: Port logout to be pure Rust (vercel#3302)
Browse files Browse the repository at this point in the history
With this PR `turbo logout` can run without jumping to Go.

Notes:
- Used `config-rs` for our config reading. It doesn't support writing
configs to disk out of the box, but this can be solved with a wrapper
struct. This can probably be abstracted to be generic over different
configs, but we should hold off until that seems beneficial.
- `termcolor` is used for coloring output, this is the same crate that
`env_logger` uses for coloring outputs. The `UI` struct is expected to
be passed into any subcommands that need pretty output.
- A slight behavior change is that we no longer delete the entire config
file and instead make the token null (`"token": null`). If this is
unwanted, I can change it back to the old behavior, but I wanted to test
out `UserConfig::set_token` before someone else tried using it.

Co-authored-by: Leah <[email protected]>
  • Loading branch information
chris-olszewski and ForsakenHarmony authored Jan 18, 2023
1 parent 99ad481 commit cda0322
Show file tree
Hide file tree
Showing 10 changed files with 338 additions and 29 deletions.
92 changes: 92 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 0 additions & 25 deletions cli/internal/cmd/auth/logout.go

This file was deleted.

2 changes: 0 additions & 2 deletions cli/internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ func RunWithArgs(args turbostate.ParsedArgsFromRust, turboVersion string) int {
execErr = login.ExecuteLink(helper, &args)
} else if command.Login != nil {
execErr = login.ExecuteLogin(ctx, helper, &args)
} else if command.Logout != nil {
execErr = auth.ExecuteLogout(helper, &args)
} else if command.Unlink != nil {
execErr = auth.ExecuteUnlink(helper, &args)
} else if command.Daemon != nil {
Expand Down
6 changes: 6 additions & 0 deletions crates/turborepo-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ license = "MPL-2.0"
assert_cmd = "2.0.7"
itertools = "0.10.5"
pretty_assertions = "1.3.0"
tempfile = "3.3.0"

[dependencies]
anyhow = { version = "1.0.65", features = ["backtrace"] }
atty = "0.2"
chrono = "0.4.23"
clap = { version = "4.0.22", features = ["derive"] }
clap_complete = "4.0.6"
config = "0.13"
console = "0.15.5"
dirs-next = "2.0.0"
dunce = "1.0"
env_logger = "0.10.0"
lazy_static = "1.4.0"
log = "0.4.17"
predicates = "2.1.1"
semver = "1.0"
Expand Down
21 changes: 19 additions & 2 deletions crates/turborepo-lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use log::error;
use serde::Serialize;

use crate::{
commands::bin,
commands::{bin, logout},
get_version,
shim::{RepoMode, RepoState},
ui::UI,
Payload,
};

Expand Down Expand Up @@ -387,9 +388,13 @@ pub fn run(repo_state: Option<RepoState>) -> Result<Payload> {

Ok(Payload::Rust(Ok(0)))
}
Command::Logout { .. } => {
logout::logout(clap_args.ui())?;

Ok(Payload::Rust(Ok(0)))
}
Command::Login { .. }
| Command::Link { .. }
| Command::Logout { .. }
| Command::Unlink { .. }
| Command::Daemon { .. }
| Command::Prune { .. }
Expand All @@ -402,6 +407,18 @@ pub fn run(repo_state: Option<RepoState>) -> Result<Payload> {
}
}

impl Args {
fn ui(&self) -> UI {
if self.no_color {
UI::new(true)
} else if self.color {
UI::new(false)
} else {
UI::infer()
}
}
}

#[cfg(test)]
mod test {
use std::path::PathBuf;
Expand Down
19 changes: 19 additions & 0 deletions crates/turborepo-lib/src/commands/logout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use anyhow::Result;
use log::error;

use crate::{
config::{default_user_config_path, UserConfig},
ui::{GREY, UI},
};

pub fn logout(ui: UI) -> Result<()> {
let mut config = UserConfig::load(&default_user_config_path()?, None)?;

if let Err(err) = config.set_token(None) {
error!("could not logout. Something went wrong: {}", err);
return Err(err);
}

println!("{}", ui.apply(GREY.apply_to(">>> Logged out")));
Ok(())
}
1 change: 1 addition & 0 deletions crates/turborepo-lib/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub(crate) mod bin;
pub(crate) mod logout;
Loading

0 comments on commit cda0322

Please sign in to comment.