Skip to content
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

Merged
merged 6 commits into from
Dec 18, 2023
Merged

Conversation

sameagen-MW
Copy link
Contributor

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.

src/script.ts Outdated
Comment on lines 38 to 43
try {
await io.which("sudo", true);
installCmd = `sudo -E ${installCmd}`;
} catch {
// Sudo not available, do not prepend
}
Copy link
Member

@mcafaro mcafaro Dec 18, 2023

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}`;
}

Copy link
Contributor Author

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

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

@sameagen-MW sameagen-MW merged commit fd8d40c into mpm Dec 18, 2023
5 checks passed
@sameagen-MW sameagen-MW deleted the no-sudo branch December 18, 2023 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants