Skip to content

Instantly share code, notes, and snippets.

@corentinbettiol
Last active August 22, 2023 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corentinbettiol/689d5d9f0989b2f71613b56ab5bc67f6 to your computer and use it in GitHub Desktop.
Save corentinbettiol/689d5d9f0989b2f71613b56ab5bc67f6 to your computer and use it in GitHub Desktop.

Install

You should never download a file before checking it, but here you go: just put the function run in your ~/.zshrc.

curl https://gist.githubusercontent.com/corentinbettiol/689d5d9f0989b2f71613b56ab5bc67f6/raw/run.sh >> ~/.zshrc
source ~/.zshrc

Explanations

Suppose you have this structure for your multisite-with-one-database django project:

mydumbwebsite/
  - src/
    - website/
      - apps/
      - settings/
      - urls.py
      - [...]
    - intranet/
      - apps/
      - [...]
    - other_website/
      - apps/
      - [...]
  - config files [...]

And you're using poetry.

In order to launch a command from the website site, you need to execute this command:

poetry run python3 src/website/manage.py <command>

What's all this poetry run python3 src/website/manage.py crap? Why can't we have a thing similar to the ol' good django-admin command (but shorter)?

Here's run

Run makes it easy to run a command on the "default" website site:

run <command>

But now you want to run a command on the other_website site.

You can do that with this command:

poetry run python3 src/other_website/manage.py <command>

Or you could use run:

run -other_website loaddata things.json

Just add the site name after a - char, and the command will be launched on this website!

What's even better than a simple, short command that allows you tu run commands on multiple websites?

A way to share this command.

run will print the complete command (in a dim style for your convenience), so you can share this command to other people that do not know about run.

How cool is that?

image

function run() {
# Run django commands on websites with ease!
# Examples:
# run runserver 0.0.0.0:8000 -> run command on default "website" project
# run -backend runserver 0.0.0.0:8000 -> run command on "backend" project
# run -other_website runserver 0.0.0.0:8000 -> run command on "other_website" project
if [ "${1:0:1}" = "-" ]; then
echo "\e[2mpoetry run python3 src/${1:1}/manage.py ${@:2}\e[0m"
poetry run python3 src/${1:1}/manage.py ${@:2}
else
echo "\e[2mpoetry run python3 src/website/manage.py $@\e[0m"
poetry run python3 src/website/manage.py $@
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment