Skip to main content

Run commands with `dj {command_name}`. Uses aliases defined in a simple config file or defaults to Django management commands.

Project description

Why?

It is available everywhere if you install via pip, has cute aliases defined in a JSON file (.dj-config.json) per project, will run as many commands as you want, and defaults to Django management commands if an alias cannot be found.

Commands can be run sequentially by dj (e.g. dj makemigrations migrate). However, calling a long-running process (e.g. dj runserver) will prevent any other commands from being run. For example, dj runserver migrate will never run the migrate command because runserver will block the process.

Configuration file

Example .dj-config.toml

disable_django_management_command = false
python_interpreter = "python"
environment_file_path = ".env"

[[commands]]
name = "m"
help = "Does the migration dance"
execute = "./manage.py makemigrations && ./manage.py migrate"
requires_virtualenv = true

[[commands]]
name = "r"
help = "Runs all the servers"
execute = "./manage.py runserver"
requires_virtualenv = true
long_running = true

[[commands]]
name = "ls"
help = "Lists all the files, of course"
execute = "ls"

[[commands]]
name = "up"
help = "Up all the things"
execute = "pip3 install -r requirements/development.txt && ./manage.py migrate && ./manage.py runserver"
requires_virtualenv = true
long_running = true

[[commands]]
name = "restore_database"
help = "Restores a Postgres database from live to local"
execute = "PGPASSWORD=$PGPASSWORD pg_dump $DATABASE_NAME --host=$DATABASE_HOST --port=$DATABASE_PORT --username=$DATABASE_USERNAME --format=tar | pg_restore --clean --dbname=$DATABASE_NAME --no-owner --host=localhost --port=5432"

Config file location

If the --config argument is used to specify a particular file location, that is the only place dj looks for a configuration file.

Otherwise, dj will search for appropriate config files and "merge" them together. This allows you to have a base config file in ~/.dj-config.toml, but override it on a per-folder basis. dj prioritizes .toml config files over .json. So, it will look for ~/.dj-config.toml first and, if it's missing, then look for ~/.dj-config.json. Then, it will follow the same pattern for the current directory. The current directory's config file will take precedence if there is an overlap in configuration settings.

Using environment variables in commands

dj will look for a .env file to load environment variables using the wonderful python-dotenv library. You can specify environment variables in an execute command just like you would from the shell (i.e. $VARIABLE_NAME).

Basic arguments and options

  • dj --help to see all of the options
  • dj --list to see all of the available custom commands
  • dj {command_name} to run a custom command or Django management command (e.g. dj migrate)
  • dj {command_name} {command_name} {command_name} to run multiple commands (e.g. dj makemigrations migrate)
  • dj {command_name} --dry_run to show what commands would run without actually executing them

How to work on the source

  1. Clone the repo
  2. Run the source locally: poetry run python dj
  3. Test the source: poetry run pytest
  4. Build and install locally: poetry build && pip3 install --user --force-reinstall .
  5. Test with ~/.local/bin/dj migrate
  6. Publish the source to pypi: poetry publish --build --username USERNAME --password PASSWORD

Acknowledgements

  • poetry: please, please, please continue to help wrangle the complexity of 1) creating Python projects, and 2) installing dependencies; seriously, it's baffling out there without you
  • click: ridiculously full-featured library to help implement CLI programs in Python; it has all the bells and most of the whistles
  • attrs: would you like easy classes in Python? yes, please
  • delegator.py: subprocess is a pain, but delegator hides all the ugly cruft behind a nice API
  • python-dotenv: 12-factor all the things with .env files
  • toml: the fewer braces in my life the better

Prior art

This isn't a new idea and there are a few other implementations out there that do similar things. But, uh, I like mine. 😀

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dj-command-0.5.1.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

dj_command-0.5.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file dj-command-0.5.1.tar.gz.

File metadata

  • Download URL: dj-command-0.5.1.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.6.6 Darwin/18.7.0

File hashes

Hashes for dj-command-0.5.1.tar.gz
Algorithm Hash digest
SHA256 70bab07719f7f819c32cd03f009c0f2101536e1dcf842c7c69e002aa068bfceb
MD5 88b79e6f2d185b6cfecccf3e039c4f16
BLAKE2b-256 2e2f984c8edbf2aad70e8b1a88d2488a6f9e5f7c14eb1c6448a17d2de41103e7

See more details on using hashes here.

File details

Details for the file dj_command-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: dj_command-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.6.6 Darwin/18.7.0

File hashes

Hashes for dj_command-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f6c3258b89070d6b76956cedeaed2ef03dab813a307fd90a1e52208fc0af9a48
MD5 801e7da1b3d335b7dd6a06e5258271a0
BLAKE2b-256 c10d6837835f2ffa0385c23844bdf2ea3ce7942b6a2c7383609c9653935fef23

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page