Improve use of whoami

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.
This commit is contained in:
Mike McQuaid 2023-09-29 12:32:22 +01:00
parent e7b0110898
commit 39bbc33a27
No known key found for this signature in database
GPG Key ID: 3338A31AFDB1D829
4 changed files with 10 additions and 6 deletions

View File

@ -411,7 +411,7 @@ EOS
${HOMEBREW_REPOSITORY} is not writable. You should change the ${HOMEBREW_REPOSITORY} is not writable. You should change the
ownership and permissions of ${HOMEBREW_REPOSITORY} back to your ownership and permissions of ${HOMEBREW_REPOSITORY} back to your
user account: user account:
sudo chown -R \$(whoami) ${HOMEBREW_REPOSITORY} sudo chown -R ${USER-\$(whoami)} ${HOMEBREW_REPOSITORY}
EOS EOS
fi fi

View File

@ -352,7 +352,7 @@ module Homebrew
You should create these directories and change their ownership to your user. You should create these directories and change their ownership to your user.
sudo mkdir -p #{not_exist_dirs.join(" ")} 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 EOS
end end
@ -367,7 +367,7 @@ module Homebrew
#{not_writable_dirs.join("\n")} #{not_writable_dirs.join("\n")}
You should change the ownership of these directories to your user. 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. And make sure that your user has write permission.
chmod u+w #{not_writable_dirs.join(" ")} chmod u+w #{not_writable_dirs.join(" ")}
@ -916,7 +916,7 @@ module Homebrew
<<~EOS <<~EOS
The staging path #{user_tilde(path.to_s)} is not writable by the current user. The staging path #{user_tilde(path.to_s)} is not writable by the current user.
To fix, run: To fix, run:
sudo chown -R $(whoami):staff #{user_tilde(path.to_s)} sudo chown -R #{current_user} #{user_tilde(path.to_s)}
EOS EOS
end end
@ -1027,6 +1027,10 @@ module Homebrew
def cask_checks def cask_checks
all.grep(/^check_cask_/) all.grep(/^check_cask_/)
end end
def current_user
ENV.fetch("USER", "$(whoami)")
end
end end
end end
end end

View File

@ -86,7 +86,7 @@ module Homebrew
rescue Errno::EACCES, Errno::ENOTEMPTY rescue Errno::EACCES, Errno::ENOTEMPTY
odie <<~EOS odie <<~EOS
Could not rename #{keg.name} keg! Check/fix its permissions: Could not rename #{keg.name} keg! Check/fix its permissions:
sudo chown -R $(whoami) #{keg} sudo chown -R #{ENV.fetch("USER", "$(whoami)")} #{keg}
EOS EOS
end end
end end

View File

@ -13,7 +13,7 @@ lock() {
odie <<EOS odie <<EOS
Can't create ${name} lock in ${lock_dir}! Can't create ${name} lock in ${lock_dir}!
Fix permissions by running: Fix permissions by running:
sudo chown -R \$(whoami) ${HOMEBREW_PREFIX}/var/homebrew sudo chown -R ${USER-\$(whoami)} ${HOMEBREW_PREFIX}/var/homebrew
EOS EOS
fi fi
# 200 is the file descriptor used in the lock. # 200 is the file descriptor used in the lock.