Skip to content

Commit

Permalink
prevent permission errors on bash_profile/rc/zshrc (#96)
Browse files Browse the repository at this point in the history
We recently had an issue where bash_profile/rc/zshrc were already
created by the root user, and it was causing permission issues in the
install script:

![image](https://github.com/ConsultingMD/homebrew-ih-public/assets/67805647/0dd9d18c-cad1-47ef-803b-00a28a7da00a)

This PR adds checks and will fix the permissions if they're incorrect.
  • Loading branch information
pb-dod authored Apr 25, 2024
1 parent 8a5e4f7 commit 57665e9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.55
0.1.56
2 changes: 1 addition & 1 deletion formula/ih-core.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class IhCore < Formula
VERSION="0.1.55"
VERSION="0.1.56"
desc "Brew formula for installing core tools used at Included Health engineering."
homepage "https://github.com/ConsultingMD/homebrew-ih-public"
license "CC BY-NC-ND 4.0"
Expand Down
19 changes: 19 additions & 0 deletions lib/core/shell/step.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ function ih::setup::core.shell::install() {
export IH_WANT_RE_SOURCE=1
}

function ih::setup::core.shell::private::ensure_correct_ownership() {
local file="$1"
if [[ -e "$file" ]]; then
local current_owner
current_owner=$(stat -f '%Su' "$file") # For macOS
if [[ "$current_owner" != "$(whoami)" ]]; then
echo "Changing ownership of $file to the current user..."
sudo chown "$(whoami)" "$file"
fi
fi
}

# shellcheck disable=SC2016
BOOTSTRAP_SOURCE_LINE='
# This loads the Included Health shell augmentations into your interactive shell
Expand All @@ -118,6 +130,10 @@ function ih::setup::core.shell::private::configure-bash() {
touch ~/.bashrc
fi

# prevent permission errors from files accidentally created as root
ih::setup::core.shell::private::ensure_correct_ownership ~/.bash_profile
ih::setup::core.shell::private::ensure_correct_ownership ~/.bashrc

# Check if .bash_profile exists and if it doesn't already source .bashrc, then add it
if [[ ! -e ~/.bash_profile ]] || ! grep -q "source ~/.bashrc" ~/.bash_profile; then
echo "Ensuring .bash_profile sources .bashrc..."
Expand Down Expand Up @@ -147,6 +163,9 @@ function ih::setup::core.shell::private::configure-zshrc() {
touch "${HOME}/.zshrc"
fi

# prevent permission errors from files accidentally created as root
ih::setup::core.shell::private::ensure_correct_ownership ~/.zshrc

# apply fix to support brew completions in zsh: https://docs.brew.sh/Shell-Completion#configuring-completions-in-zsh
chmod -R go-w "$(brew --prefix)/share"

Expand Down

0 comments on commit 57665e9

Please sign in to comment.