I've recently started using DDEV for local Drupal development, it's very nice but the old way of using drush is very stuck in my muscle memory.

I decided too see if I could find a way to get the best of both worlds.

DDEV provides a cli that allows you to interact with the docker container, and also check the status of it (with json!). The command ddev status gives you information about the project you are in at the moment in your terminal.

So it should be possible to write a bash script/function that runs that command and checks if we are in a DDEV environment or not. Based on that we could either run the regular drush command or the ddev drush command.

It turned out parsing JSON is not that easy in bash, so I had to resort to a external dependency JQ. Installed with brew require jq.

It allows me to pipe the JSON output of the ddev command and extract the single property (status) that I'm after.

The solution

% function drush2 () {
  ddev_running=$(ddev st --json-output | jq -r '.raw.status')

  if [[ $ddev_running == "running" ]] then 
    ddev drush $@ 
    drush $@ 

This is a ZSH function that I put in ~/.oh-my-zsh/custom/functions.zsh. After restarting the terminal I get a new command drush2 that works with regular drush and ddev drush.