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

Fix apply not working for relative paths #1

Merged
merged 2 commits into from
May 20, 2024

Conversation

mmorella-dev
Copy link
Contributor

This PR fixes a bug where trying to use apply with a relative path to a directory of YAML files causes a panic:

 macos-defaults apply data/macos-defaults/
Error:
   0: Failed to read bytes from path data/macos-defaults/data/macos-defaults/accessibility.yaml.
   1: No such file or directory (os error 2)

Location:
   src/main.rs:93

Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it.
Run with RUST_BACKTRACE=full to include source snippets.

The issue is caused here:

let mut files = path
.read_dir_utf8()?
.filter_map(Result::ok)
.filter(|entry| entry.path().extension().map_or(false, |ext| ext == "yml" || ext == "yaml"))
.map(|entry| path.join(entry.path()))
.collect::<Vec<Utf8PathBuf>>();

Calling entry.path() gets a complete path to each entry (e.g. data/macos-defaults/accessibility.yaml), which is then erroneously joined to the path of its parent directory (data/macos-defaults/), resulting in a non-existent path like data/macos-defaults/data/macos-defaults/accessibility.yaml. This incidentally works when the path is absolute, because in that case the new path simply replaces the old one).

This PR changes the implementation to avoid improperly appending the string, and function as intended.

macos-defaults apply data/macos-defaults/
  ▶ Accessibility
  ▶ Activity Monitor
  ▶ Audio & Sound
  ▶ Calendar
  ▶ Contacts
  ▶ Displays
  ▶ Dock
  ▶ Finder
  ▶ Globals
  ▶ Keyboard
  ▶ Mac App Store
  ▶ Mail
  ▶ Menu Bar
  ▶ Messages
  ▶ Photos & Image Capture
  ▶ Safari
  ▶ Security
  ▶ Siri
  ▶ Spotlight
  ▶ Terminal
  ▶ Text Edit
  ▶ Time Machine
  ▶ Trackpad
  ▶ Trackpad Currenthost
  ▶ Xcode

@mmorella-dev
Copy link
Contributor Author

Also wanted to say, I discovered this tool today and it looks very promising! Thank you for maintaining it.

@dsully dsully merged commit 4e609a1 into dsully:main May 20, 2024
6 checks passed
@dsully
Copy link
Owner

dsully commented May 20, 2024

Thanks for the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants