From 57665e98f36cbfae576e7e368c579b7ca9147523 Mon Sep 17 00:00:00 2001 From: Paul Brown <67805647+pb-dod@users.noreply.github.com> Date: Thu, 25 Apr 2024 12:02:37 -0500 Subject: [PATCH] prevent permission errors on bash_profile/rc/zshrc (#96) 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. --- VERSION | 2 +- formula/ih-core.rb | 2 +- lib/core/shell/step.sh | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index 8893a8e..c52af3e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.55 +0.1.56 diff --git a/formula/ih-core.rb b/formula/ih-core.rb index 358961e..32cc8a4 100644 --- a/formula/ih-core.rb +++ b/formula/ih-core.rb @@ -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" diff --git a/lib/core/shell/step.sh b/lib/core/shell/step.sh index 82c06bc..cfeb943 100644 --- a/lib/core/shell/step.sh +++ b/lib/core/shell/step.sh @@ -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 @@ -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..." @@ -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"