-
Notifications
You must be signed in to change notification settings - Fork 14
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
Don't add the sudo prefix if sudo is not on the path #78
Conversation
src/script.ts
Outdated
try { | ||
await io.which("sudo", true); | ||
installCmd = `sudo -E ${installCmd}`; | ||
} catch { | ||
// Sudo not available, do not prepend | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try to avoid using try/catch blocks for flow control.
Would this work?
const sudo = await io.which("sudo", false);
if (sudo) {
installCmd = `sudo -E ${installCmd}`;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately it won't, await
throws on rejected promises. The two primary ways to handle rejected promises are either to wrap await
in a try
-catch
block or to call the catch
method on the promise. If we chose the second option we would have to use then
instead of await
, and I personally think that makes the code less readable.
This isn't really for control flow, io.which
made the design decision to throw if the executable is not on the path when the optional argument is true
. I don't much like the decision but it does seem to be the intended use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I missed that you switched the optional argument to false
in your code snippet. In that case it would work, but I'm a little bit uncomfortable going against what seems to be the intended API of the "@actions/io" library. It probably doesn't matter though in this case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like they designed the API to match the which
behavior here: https://linux.die.net/man/1/which, including erroring if the value is not found (when the second arg is true
) so it probably isn't problematic to ignore that
This change makes it so the prefix
sudo -E
is not added to the bash command if sudo is not on the machine's path.