Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(path): make path reading api more rusty #9103

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions crates/turborepo-lib/src/commands/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,12 @@ pub async fn link(

let local_config_path = base.local_config_path();
let before = local_config_path
.read_existing_to_string_or(Ok("{}"))
.read_existing_to_string()
.map_err(|e| config::Error::FailedToReadConfig {
config_path: local_config_path.clone(),
error: e,
})?;
})?
.unwrap_or_else(|| String::from("{}"));

let no_preexisting_id = unset_path(&before, &["teamid"], false)?.unwrap_or(before);
let no_preexisting_slug =
Expand Down Expand Up @@ -314,11 +315,12 @@ pub async fn link(

let local_config_path = base.local_config_path();
let before = local_config_path
.read_existing_to_string_or(Ok("{}"))
.read_existing_to_string()
.map_err(|error| config::Error::FailedToReadConfig {
config_path: local_config_path.clone(),
error,
})?;
})?
.unwrap_or_else(|| String::from("{}"));

let no_preexisting_id = unset_path(&before, &["teamid"], false)?.unwrap_or(before);
let no_preexisting_slug =
Expand Down Expand Up @@ -541,11 +543,12 @@ fn add_turbo_to_gitignore(base: &CommandBase) -> Result<(), io::Error> {
fn add_space_id_to_turbo_json(base: &CommandBase, space_id: &str) -> Result<(), Error> {
let turbo_json_path = base.repo_root.join_component("turbo.json");
let turbo_json = turbo_json_path
.read_existing_to_string_or(Ok("{}"))
.read_existing_to_string()
.map_err(|error| config::Error::FailedToReadConfig {
config_path: turbo_json_path.clone(),
error,
})?;
})?
.unwrap_or_else(|| String::from("{}"));

let space_id_json_value = format!("\"{}\"", space_id);

Expand Down
10 changes: 6 additions & 4 deletions crates/turborepo-lib/src/commands/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ pub async fn sso_login(

let global_config_path = base.global_config_path()?;
let before = global_config_path
.read_existing_to_string_or(Ok("{}"))
.read_existing_to_string()
.map_err(|e| config::Error::FailedToReadConfig {
config_path: global_config_path.clone(),
error: e,
})?;
})?
.unwrap_or_else(|| String::from("{}"));

let after = set_path(&before, &["token"], &format!("\"{}\"", token.into_inner()))?;

Expand Down Expand Up @@ -92,11 +93,12 @@ pub async fn login(

let global_config_path = base.global_config_path()?;
let before = global_config_path
.read_existing_to_string_or(Ok("{}"))
.read_existing_to_string()
.map_err(|e| config::Error::FailedToReadConfig {
config_path: global_config_path.clone(),
error: e,
})?;
})?
.unwrap_or_else(|| String::from("{}"));
let after = set_path(&before, &["token"], &format!("\"{}\"", token.into_inner()))?;

global_config_path
Expand Down
10 changes: 6 additions & 4 deletions crates/turborepo-lib/src/commands/unlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ fn unlink_remote_caching(base: &mut CommandBase) -> Result<(), cli::Error> {
let local_config_path = base.local_config_path();

let before = local_config_path
.read_existing_to_string_or(Ok("{}"))
.read_existing_to_string()
.map_err(|error| config::Error::FailedToReadConfig {
config_path: local_config_path.clone(),
error,
})?;
})?
.unwrap_or_else(|| String::from("{}"));
let no_id = unset_path(&before, &["teamid"], false)?.unwrap_or(before);
let no_slug = unset_path(&no_id, &["teamslug"], false)?.unwrap_or(no_id);

Expand Down Expand Up @@ -62,11 +63,12 @@ fn unlink_spaces(base: &mut CommandBase) -> Result<(), cli::Error> {
if needs_disabling {
let local_config_path = base.local_config_path();
let before = local_config_path
.read_existing_to_string_or(Ok("{}"))
.read_existing_to_string()
.map_err(|e| config::Error::FailedToReadConfig {
config_path: local_config_path.clone(),
error: e,
})?;
})?
.unwrap_or_else(|| String::from("{}"));
let no_id = unset_path(&before, &["teamid"], false)?.unwrap_or(before);
let no_slug = unset_path(&no_id, &["teamslug"], false)?.unwrap_or(no_id);

Expand Down
15 changes: 8 additions & 7 deletions crates/turborepo-lib/src/config/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ impl ResolvedConfigurationOptions for ConfigFile {
&self,
_existing_config: &ConfigurationOptions,
) -> Result<ConfigurationOptions, Error> {
let mut contents = self
let contents = self
.path
.read_existing_to_string_or(Ok("{}"))
.read_existing_to_string()
.map_err(|error| Error::FailedToReadConfig {
config_path: self.path.clone(),
error,
})?;
if contents.is_empty() {
contents = String::from("{}");
}
let global_config: ConfigurationOptions = serde_json::from_str(&contents)?;
})?
.filter(|s| !s.is_empty());

let global_config = contents
.as_deref()
.map_or_else(|| Ok(ConfigurationOptions::default()), serde_json::from_str)?;
Ok(global_config)
}
}
Expand Down
16 changes: 0 additions & 16 deletions crates/turborepo-paths/src/absolute_system_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,22 +441,6 @@ impl AbsoluteSystemPath {
fs::read_to_string(&self.0)
}

/// Attempts to read a file, and:
/// If the file does not exist it returns the default value.
/// For all other scenarios passes through the `read_to_string` results.
pub fn read_existing_to_string_or<I>(
&self,
default_value: Result<I, io::Error>,
) -> Result<String, io::Error>
where
I: Into<String>,
{
match self.read_existing_to_string()? {
Some(contents) => Ok(contents),
None => default_value.map(|value| value.into()),
}
}

/// Attempts to read a file returning None if the file does not exist
/// For all other scenarios passes through the `read_to_string` results.
pub fn read_existing_to_string(&self) -> Result<Option<String>, io::Error> {
Expand Down
Loading