Skip to content

Commit

Permalink
Improve use of whoami
Browse files Browse the repository at this point in the history
If you're e.g. running Homebrew over `sudo`: shelling out to `whoami`
is less effective than just telling people which user you're running
as when we run the check.
  • Loading branch information
MikeMcQuaid committed Sep 29, 2023
1 parent e7b0110 commit 39bbc33
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Library/Homebrew/cmd/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ EOS
${HOMEBREW_REPOSITORY} is not writable. You should change the
ownership and permissions of ${HOMEBREW_REPOSITORY} back to your
user account:
sudo chown -R \$(whoami) ${HOMEBREW_REPOSITORY}
sudo chown -R ${USER-\$(whoami)} ${HOMEBREW_REPOSITORY}
EOS
fi

Expand Down
10 changes: 7 additions & 3 deletions Library/Homebrew/diagnostic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ def check_exist_directories
You should create these directories and change their ownership to your user.
sudo mkdir -p #{not_exist_dirs.join(" ")}
sudo chown -R $(whoami) #{not_exist_dirs.join(" ")}
sudo chown -R #{current_user} #{not_exist_dirs.join(" ")}
EOS
end

Expand All @@ -367,7 +367,7 @@ def check_access_directories
#{not_writable_dirs.join("\n")}
You should change the ownership of these directories to your user.
sudo chown -R $(whoami) #{not_writable_dirs.join(" ")}
sudo chown -R #{current_user} #{not_writable_dirs.join(" ")}
And make sure that your user has write permission.
chmod u+w #{not_writable_dirs.join(" ")}
Expand Down Expand Up @@ -916,7 +916,7 @@ def check_cask_staging_location
<<~EOS
The staging path #{user_tilde(path.to_s)} is not writable by the current user.
To fix, run:
sudo chown -R $(whoami):staff #{user_tilde(path.to_s)}
sudo chown -R #{current_user} #{user_tilde(path.to_s)}
EOS
end

Expand Down Expand Up @@ -1027,6 +1027,10 @@ def all
def cask_checks
all.grep(/^check_cask_/)
end

def current_user
ENV.fetch("USER", "$(whoami)")
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/reinstall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def backup(keg)
rescue Errno::EACCES, Errno::ENOTEMPTY
odie <<~EOS
Could not rename #{keg.name} keg! Check/fix its permissions:
sudo chown -R $(whoami) #{keg}
sudo chown -R #{ENV.fetch("USER", "$(whoami)")} #{keg}
EOS
end
end
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/utils/lock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ lock() {
odie <<EOS
Can't create ${name} lock in ${lock_dir}!
Fix permissions by running:
sudo chown -R \$(whoami) ${HOMEBREW_PREFIX}/var/homebrew
sudo chown -R ${USER-\$(whoami)} ${HOMEBREW_PREFIX}/var/homebrew
EOS
fi
# 200 is the file descriptor used in the lock.
Expand Down

0 comments on commit 39bbc33

Please sign in to comment.