Skip to content

Commit

Permalink
very minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
elkowar committed Dec 22, 2024
1 parent b1ab4fc commit 952917b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 52 deletions.
13 changes: 6 additions & 7 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ pub fn rename_safely(original: impl AsRef<Path>, new: impl AsRef<Path>) -> miett
let original = original.as_ref();
let new = new.as_ref();
tracing::trace!("Renaming {} -> {}", original.abbr(), new.abbr());
if new.exists() {
miette::bail!(
"Failed to move file {} to {}: File already exists.",
original.abbr(),
new.abbr()
);
}
miette::ensure!(
!new.exists(),
"Failed to move file {} to {}: File already exists.",
original.abbr(),
new.abbr()
);
fs_err::rename(original, new)
.into_diagnostic()
.wrap_err("Failed to rename file")?;
Expand Down
8 changes: 3 additions & 5 deletions src/yolk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Yolk {
fs_err::create_dir_all(parent).map_err(|e| {
miette!(
severity = Severity::Warning,
"Warning: Failed to create parent dir for deployment of {}: {e:?}",
"Failed to create parent dir for deployment of {}: {e:?}",
in_egg.abbr()
)
})?;
Expand Down Expand Up @@ -97,10 +97,7 @@ impl Yolk {
let mut errs = Vec::new();
for (in_egg, deployed) in mappings {
if let Err(e) = remove_symlink_recursive(in_egg, &deployed) {
errs.push(miette!(
"Failed to remove deployment of {}: {e:?}",
in_egg.abbr()
));
errs.push(e.wrap_err(format!("Failed to remove deployment of {}", in_egg.abbr())));
}
}
if !errs.is_empty() {
Expand Down Expand Up @@ -431,6 +428,7 @@ fn remove_symlink_recursive(
}
} else if link_path.exists() {
miette::bail!(
help = "Yolk will only try to remove files that are symlinks pointing into the corresponding egg.",
"Tried to remove deployment of {}, but {} doesn't link to it",
actual_path.abbr(),
link_path.abbr()
Expand Down
73 changes: 33 additions & 40 deletions src/yolk_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,21 @@ impl YolkPaths {

#[allow(unused)]
pub fn check(&self) -> Result<()> {
if !self.root_path().exists() {
miette::bail!(
"Yolk directory does not exist at {}",
self.root_path().abbr()
);
}
if !self.yolk_rhai_path().exists() {
miette::bail!(
"Yolk directory does not contain a yolk.rhai file at {}",
self.yolk_rhai_path().abbr()
);
}
if !self.eggs_dir_path().exists() {
miette::bail!(
"Yolk directory does not contain an eggs directory at {}",
self.eggs_dir_path().abbr()
);
}
miette::ensure!(
self.root_path().exists(),
"Yolk directory does not exist at {}",
self.root_path().abbr()
);
miette::ensure!(
self.yolk_rhai_path().exists(),
"Yolk directory does not contain a yolk.rhai file at {}",
self.yolk_rhai_path().abbr()
);
miette::ensure!(
self.eggs_dir_path().exists(),
"Yolk directory does not contain an eggs directory at {}",
self.eggs_dir_path().abbr()
);
Ok(())
}

Expand All @@ -116,17 +113,15 @@ impl YolkPaths {
/// Safeguard git directory by renaming it to `.yolk_git`
pub fn safeguard_git_dir(&self) -> Result<()> {
if self.root_path().join(".git").exists() {
if self.yolk_safeguarded_git_path().exists() {
miette::bail!(
help = "Safeguarded Yolk renames .git to .yolk_git to ensure you don't accidentally commit without yolks processing",
"Yolk directory contains both a .git directory and a .yolk_git directory"
);
} else {
util::rename_safely(
self.root_path().join(".git"),
self.yolk_safeguarded_git_path(),
)?;
}
miette::ensure!(
!self.yolk_safeguarded_git_path().exists(),
help = "Safeguarded Yolk renames .git to .yolk_git to ensure you don't accidentally commit without yolks processing",
"Yolk directory contains both a .git directory and a .yolk_git directory"
);
util::rename_safely(
self.root_path().join(".git"),
self.yolk_safeguarded_git_path(),
)?;
}
Ok(())
}
Expand Down Expand Up @@ -165,7 +160,10 @@ impl YolkPaths {
} else if default_git_dir.exists() {
Ok(default_git_dir)
} else {
miette::bail!("No git directory initialized")
miette::bail!(
help = "Run `git init`, then try again!",
"No git directory initialized"
)
}
}
///
Expand Down Expand Up @@ -197,16 +195,11 @@ pub struct Egg {

impl Egg {
pub fn open(home: PathBuf, egg_path: PathBuf, config: EggConfig) -> Result<Self> {
if !egg_path.is_dir() {
miette::bail!(
"Egg {} does not exist",
egg_path
.file_name()
.unwrap_or_default()
.to_str()
.unwrap_or_default()
)
}
miette::ensure!(
egg_path.is_dir(),
"No egg at {} does not exist",
egg_path.abbr(),
);
Ok(Self {
home_path: home.canonical()?,
egg_dir: egg_path.canonical()?,
Expand Down

0 comments on commit 952917b

Please sign in to comment.