jorangreef / sudo-prompt

Run a command using sudo, prompting the user with an OS dialog if necessary.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Breaks with bash redirections

davej opened this issue · comments

For example, the following bash will give a permissions fail:

sudo-prompt.exec("echo 'some text' > /Library/some-file.txt")

It's easily fixed by doing:

sudo-prompt.exec("bash -c \"echo 'some text' > /Library/some-file.txt\"")

Perhaps sudo-prompt should do this (bash -c) at the module level instead? String escaping may need a little bit of thought but should be ok.

The idea behind sudo-prompt is to prefix a command with "sudo" but show an os dialog if permissions would be necessary.

So, sudo-prompt at the moment would take your command above and run:

sudo echo 'some text' > /Library/some-file.txt

...but it would also take care of the os prompt along the way.

The issue at the moment if I understand correctly is that calling that command using sudo also fails:

 $ sudo echo 'some text' > /Library/some-file.txt
-bash: /Library/some-file.txt: Permission denied

The echo 'some text' is run using sudo, but the redirection to /Library is not.

The bash -c trick is a good idea. I am not sure how this would affect propagating command errors out of the bash shell. I will give it a try and see if we can make the changes.

Yup that's it.

After some basic testing, stdout and stderr both propagate up to the node callback exactly as expected.

If you like I can make a PR?

Thanks that would be great.

Does the exit code also propagate?

I presume so but I haven't tested that, I'll give it a test when I have a few spare minutes.

Just tried bash -c 'exit 0'; and it doesn't seem to propagate.

Did a bit more testing, this does propagate signals: exec bash -c 'exit 0'.

edit: typo.
second edit: Actually that seems to break stdout ughh.

The more I test this, perhaps it's best to leave it out of the lib, unless there's a straightforward way to do it. I'm not familiar enough with the intricacies of bash.

Thanks for trying, at least the bash -c is documented here for those that need to redirect sudo output.

Using -e flag on bash will exit immediately on error.

@davej Zero is the code for successful exists. If you want to "throw" an error you must exit with something different than zero.

http://unix.stackexchange.com/questions/15998/what-does-the-e-do-in-a-bash-shebang