I want to use a command line file path with Drush (see #185373: Review code snippets). Since drush is a command line tool, this seems like a reasonable thing to do. Currently, drush does a chdir(), and all paths are relative to the Drupal root. However, I am running drush from one of my sites directories, and the path should be DRUPAL_ROOT/sites/example.com/. There are options to figuring out what file a user intended to use... such as getting the conf_path() and looking for it relative to that one. But since the current working directory is so pivotal to figuring out a file path, and since Drush is a command line tool, I was thinking that it made sense to just save this variable for anyone that wanted to use it.
Does it make sense to store the CWD before changing it?
Comment | File | Size | Author |
---|---|---|---|
#3 | 297611.patch | 575 bytes | douggreen |
oldpwd.patch | 612 bytes | douggreen |
Comments
Comment #1
douggreen CreditAttribution: douggreen commentedComment #2
moshe weitzman CreditAttribution: moshe weitzman commentedOK. I think CWD is more consistent than PWD and I would even prefix so it is DRUSH_OLDCWD.
Comment #3
douggreen CreditAttribution: douggreen commentedI used OLDPWD, because that's what the shell uses, but here's a patch using DRUPAL_OLDCWD.
Comment #4
moshe weitzman CreditAttribution: moshe weitzman commentedCommitted a slightly different fix. Get the value using drupal_get_context('DRUSH_OLDCWD').
Comment #6
Gogowitsch CreditAttribution: Gogowitsch as a volunteer and at QuoData GmbH Quality & Statistics for Merck KGaA commentedHere comes a note for future googlers:
In Drush 9, you typically extend the class
DrushCommands
. In your custom command, you can use the following to get the directory from which Drush was called:$this->getConfig()->cwd()