Book Image

Drush for Developers - Second Edition

By : Juan Pablo Novillo Requena, Juan P Novillo Requena
Book Image

Drush for Developers - Second Edition

By: Juan Pablo Novillo Requena, Juan P Novillo Requena

Overview of this book

Table of Contents (13 chapters)
Drush for Developers Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
2
Keeping Database Configuration and Code Together
Index

The Drush command structure


Drush offers a broad list of commands that cover practically all the aspects of a Drupal project. If you are already fluent with executing commands in the terminal, you can skip this section. Otherwise, keep on reading to discover what arguments and options are and how these affect the behavior of a command.

We can view the available list of commands by running drush help. Additionally, running drush help some-command will show you detailed information about a particular command.

Executing a command

Let's start with a very simple command such as core-status, which prints environment information about Drush and, if available, a Drupal site. Assuming that we have a Drupal project installed at /home/juampy/projects/drupal, let's run this command here and see its output:

$ drush core-status
 Drupal version                  :  7.29-dev                        
 Site URI                        :  http://default                  
 Database driver                 :  mysql                           
 Database username               :  root                            
 Database name                   :  drupal7x                        
 Database                        :  Connected                       
 Drupal bootstrap                :  Successful                      
 Drupal user                     :                                  
 Default theme                   :  bartik                          
 Administration theme            :  seven                           
 PHP executable                  :  /usr/bin/php                    
 PHP configuration               :  /etc/php5/cli/php.ini           
 PHP OS                          :  Linux                           
 Drush version                   :  7.0.0-alpha5                    
 Drush temp directory            :  /tmp                            
 Drush alias files               :                                  
 Drupal root                     :  /home/juampy/projects/drupal    
 Site path                       :  sites/default                   
 File directory path             :  sites/default/files             
 Temporary file directory path   :  /tmp                

The preceding output informs us about the main configuration of the Drupal project plus some Drush environment settings.

Providing arguments to a command

The core-status command accepts a single argument that specifies which setting is to be retrieved (you can see this information by running drush help core-status). An argument is a string of text that acts as an input data for a command. Arguments are entered after the command name and are separated by spaces. Therefore, if we need to print just the items containing version in the setting name, we can execute the following command:

$ drush core-status version
 Drupal version   :  7.29-dev     
 Drush version    :  7.0.0-alpha5 

Drush commands might accept zero to any number of arguments depending on their nature. Beware that some commands expect arguments to be given in a certain order. For example, the variable-set command, used to change Drupal environment variables, requires the first argument to be the variable name and the second argument to be the variable's new value. Hence, the following example sets the site-name variable to the My awesome site value:

$ drush variable-set site-name "My awesome site"
site-name was set to "My awesome site".        [success]

Altering a command's behavior through options

Drush commands might accept options through the command line, which alter their default behavior. Options are in the form of --option-name or --option-name=value. Additionally, some options have a shorter version. For example, you can accept all confirmations for a Drush command by appending --yes or its shorter version: -y.

Let's take a look at options with an example. The core-status command has an option to show the database password. We will now add it to the command and inspect the output:

$ cd /home/juampy/projects/drupal
$ drush core-status --show-passwords database
 Database driver     :  mysql     
 Database username   :  root      
 Database password   :  mysecretpw          
 Database name       :  drupal7x  
 Database            :  Connected 

The --show-passwords option orders the core-status command that we want to see the database password of the Drupal site being bootstrapped.

Structuring command invocations

Excluding some exceptions, there is no strict ordering for options and arguments when you run a command. Besides, Drush does a great job parsing arguments and options no matter how we mix them up in the input. However, our commands will be more readable if we follow this pattern:

$ drush [global options] [command name] [command options] [arguments]

Here is an example:

$ drush --verbose core-status --show-passwords database 

And the following are the commands used in the previous example:

  • --verbose: This is a Drush global option. You can see all the available global options by running drush topic core-global-options.

  • core-status: This is the command that we are running.

  • --show-passwords: This is an option of the core-status command.

  • database: This is an argument for the core-status command.

Besides the fact of higher clarity by using the preceding structure, there are some commands in Drush that require options to be given in this order. This is the case of the core-sync Drush command, which is a wrapper of the actual Unix rsync command used to copy files and directories. Let's take a look at the following example:

$ drush rsync @self:%files/ /tmp/files --dry-run
You will destroy data from /tmp/files and replace with data from /home/juampy/projects/drupal/sites/default/files/
Do you really want to continue? (y/n):

The preceding command copies files recursively from a Drupal project into /tmp/files. The --dry-run option is an rsync specific option that attempts to copy files but does not make any actual changes. Now, let's try to run the same command but this time placing the option before the command name:

$ drush --dry-run rsync @self:%files/ /tmp/files
Unknown option: --dry-run.  See `drush help core-rsync` for available options. To suppress this error, add the option –strict=0.  [error]

We can see in the preceding output that Drush attempted to evaluate the --dry-run option and failed as it did not recognize it. This example demonstrates that you should carefully read the description of a command by running drush help command-name in order to understand its options, arguments, and ordering.

Command aliases

Most of Drush commands support a shorter name to be used when invoking them. You can find them in parenthesis next to each command name when running drush help, or in the Aliases section when viewing the full help of a command.

For example, the core-status command can also be executed with status or just st, which means that the following commands will return identical results:

$ drush core-status
$ drush status
$ drush st

Note

For clarity, we will not use command aliases in this book, but these help us to work faster. So, it is worthwhile to use them.