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

Run shell functions #46

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions crab/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from crab import router, __version__
import shlex
import socket
import subprocess
import sys
from dotenv import dotenv_values
import shutil
Expand Down Expand Up @@ -78,12 +79,20 @@ def main(command=None):
port = env.setdefault("PORT", get_free_port())
command = [item.replace("$PORT", port) for item in command]

if shutil.which(command[0], path=env["PATH"]) is None:
print('Could not find "{}" in your procfile or $PATH.'.format(command[0]))
exit(1)
# Is it a shell function?
if (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should follow the semantics of shell functions in that they override items in the $PATH, rather than only being called if they don't exist.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, so swap the two checks?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed

subprocess.run(
["declare", "-f", command[0]], shell=True, stdout=subprocess.DEVNULL
).returncode
== 0
):
return os.execvpe(env["SHELL"], [env["SHELL"], "-ci"] + command, env)

if shutil.which(command[0], path=env["PATH"]) is not None:
return os.execvpe(command[0], command, env)

# off we go
os.execvpe(command[0], command, env)
print('Could not find "{}" in your procfile or $PATH.'.format(command[0]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth mentioning here about it also not being a shell function?

Although that would be quite a long message. Shells just do "command not found".

exit(1)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_missing_command(self):
cmd = subprocess.run(
["crab", "this-command-shouldnt-exist"], stdout=subprocess.PIPE
)
self.assertEqual(cmd.returncode, 1)
self.assertNotEqual(cmd.returncode, 0)
self.assertIn(
'Could not find "this-command-shouldnt-exist" in your procfile or $PATH.',
cmd.stdout.decode(),
Expand Down
2 changes: 1 addition & 1 deletion tests/test_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ def test_get_routes(self):
env={**os.environ, "VIRTUAL_HOST": "test.localhost", "PORT": "1234"},
) as subproc:
routes = get_routes()
self.assertEqual(routes, {"test.localhost": "1234"})
self.assertEqual(routes["test.localhost"], "1234")
subproc.terminate()