Skip to content

Commit

Permalink
fix(turborepo): Better unlink Output (#5764)
Browse files Browse the repository at this point in the history
`turbo unlink` previously would delete the entire configuration file
which is unnecessarily punishing, this switches to do the absolute
minimum change to the file.

Closes TURBO-1225
  • Loading branch information
nathanhammond authored Aug 25, 2023
1 parent 0cac948 commit 561bab2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
12 changes: 0 additions & 12 deletions crates/turborepo-lib/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,6 @@ impl CommandBase {
Ok(self.repo_config.get_mut().unwrap())
}

// NOTE: This deletes the repo config file. It does *not* remove the
// `RepoConfig` struct from `CommandBase`. This is fine because we
// currently do not have any commands that delete the repo config file
// and then attempt to read from it.
pub fn delete_repo_config_file(&mut self) -> Result<()> {
let repo_config_path = get_repo_config_path(self.repo_root.borrow());
if repo_config_path.exists() {
std::fs::remove_file(repo_config_path)?;
}
Ok(())
}

fn user_config_init(&self) -> Result<UserConfig, ConfigError> {
UserConfigLoader::new(default_user_config_path()?)
.with_token(self.args.token.clone())
Expand Down
43 changes: 26 additions & 17 deletions crates/turborepo-lib/src/commands/unlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,41 @@ enum UnlinkSpacesResult {
}

fn unlink_remote_caching(base: &mut CommandBase) -> Result<()> {
base.delete_repo_config_file()
.context("could not unlink. Something went wrong")?;
let repo_config: &mut crate::config::RepoConfig = base.repo_config_mut()?;
let needs_disabling = repo_config.team_id().is_some() || repo_config.team_slug().is_some();

println!(
"{}",
base.ui.apply(GREY.apply_to("> Disabled Remote Caching"))
);
let output = if needs_disabling {
repo_config.set_team_id(None)?;
"> Disabled Remote Caching"
} else {
"> No Remote Caching config found"
};

println!("{}", base.ui.apply(GREY.apply_to(output)));

Ok(())
}

fn unlink_spaces(base: &mut CommandBase) -> Result<()> {
let repo_config: &mut crate::config::RepoConfig = base.repo_config_mut()?;
let needs_disabling =
repo_config.space_team_id().is_some() || repo_config.space_team_slug().is_some();

if needs_disabling {
repo_config.set_space_team_id(None)?;
}

// Space config is _also_ in turbo.json.
let result =
remove_spaces_from_turbo_json(base).context("could not unlink. Something went wrong")?;

match result {
UnlinkSpacesResult::Unlinked => {
println!("{}", base.ui.apply(GREY.apply_to("> Unlinked Spaces")));
}
UnlinkSpacesResult::NoSpacesFound => {
println!(
"{}",
base.ui.apply(GREY.apply_to("> No Spaces config found"))
);
}
}
let output = match (needs_disabling, result) {
(_, UnlinkSpacesResult::Unlinked) => "> Unlinked Spaces",
(true, _) => "> Unlinked Spaces",
(false, UnlinkSpacesResult::NoSpacesFound) => "> No Spaces config found",
};

println!("{}", base.ui.apply(GREY.apply_to(output)));

Ok(())
}
Expand Down

0 comments on commit 561bab2

Please sign in to comment.