notify.drush.inc

  1. 8.0.x commands/core/notify.drush.inc
  2. 6.x commands/core/notify.drush.inc
  3. 7.x commands/core/notify.drush.inc
  4. master commands/core/notify.drush.inc

Add system notifications as a new drush option.

Functions

Namesort descending Description
drush_notify_allowed Identify if the given Drush request should trigger a notification.
drush_notify_command_message Determine the message to send on command completion.
drush_notify_send Prepares and dispatches notifications to delivery mechanisms.
drush_notify_send_audio Send an audio-based system notification.
drush_notify_send_text Send text-based system notification.
drush_notify_shutdown_error Shutdown function to signal on errors.
notify_drush_exit Implements hook_drush_exit().
notify_drush_help Implements hook_drush_help().
notify_drush_help_alter Implements hook_drush_help_alter().

File

commands/core/notify.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Add system notifications as a new drush option.
  5. */
  6. /**
  7. * @todo there are no hooks fired after a command errors out.
  8. */
  9. register_shutdown_function('drush_notify_shutdown_error');
  10. /**
  11. * Implements hook_drush_help_alter().
  12. */
  13. function notify_drush_help_alter(&$command) {
  14. if ($command['command'] == 'global-options') {
  15. // Do not include these in options in standard help.
  16. if ($command['#brief'] === FALSE) {
  17. $command['options']['notify'] = array(
  18. 'description' => 'Use system notifications to signal command completion. If set to a number, commands that finish in fewer seconds will not trigger a notification.',
  19. 'example-value' => 60,
  20. );
  21. $command['options']['notify-audio'] = array(
  22. 'description' => 'Trigger an audio alert to signal command completion. If set to a number, commands that finish in fewer seconds will not trigger a notification.',
  23. 'example-value' => 60,
  24. );
  25. $command['sub-options']['notify']['notify-cmd'] = 'Specify the shell command to trigger the notification.';
  26. $command['sub-options']['notify']['notify-cmd-audio'] = 'Specify the shell command to trigger the audio notification.';
  27. }
  28. }
  29. }
  30. /**
  31. * Implements hook_drush_help().
  32. */
  33. function notify_drush_help($section) {
  34. switch ($section) {
  35. case 'notify:cache-clear':
  36. return dt('Caches have been cleared.');
  37. case 'notify:site-install:error':
  38. return dt('Failed on site installation');
  39. }
  40. }
  41. /**
  42. * Implements hook_drush_exit().
  43. */
  44. function notify_drush_exit() {
  45. $cmd = drush_get_command();
  46. // pm-download handles its own notification.
  47. if ($cmd['command'] != 'pm-download' && drush_notify_allowed($cmd['command'])) {
  48. $msg = dt("Command '!command' completed.", array('!command' => $cmd['command']));
  49. drush_notify_send(drush_notify_command_message($cmd['command'], $msg));
  50. }
  51. }
  52. /**
  53. * Shutdown function to signal on errors.
  54. */
  55. function drush_notify_shutdown_error() {
  56. if (drush_get_option('notify', FALSE) && drush_get_error()) {
  57. // If the only error is that notify failed, do not try to notify again.
  58. $log = drush_get_error_log();
  59. if (count($log) == 1 && array_key_exists('NOTIFY_COMMAND_NOT_FOUND', $log)) {
  60. return;
  61. }
  62. // Send an alert that the command failed.
  63. $cmd = drush_get_command();
  64. if (drush_notify_allowed($cmd['command'])) {
  65. $msg = dt("Command '!command' failed.", array('!command' => $cmd['command']));
  66. drush_notify_send(drush_notify_command_message($cmd['command'] . ':error', $msg));
  67. }
  68. }
  69. }
  70. /**
  71. * Determine the message to send on command completion.
  72. *
  73. * @param string $command
  74. * Name of the Drush command for which we check message overrides.
  75. * @param string $default
  76. * (Default: NULL) Default message to use if there are not notification message overrides.
  77. *
  78. * @return string
  79. * Message to use for notification.
  80. */
  81. function drush_notify_command_message($command, $default = NULL) {
  82. if ($msg = drush_command_invoke_all('drush_help', 'notify:' . $command)) {
  83. $msg = implode("\n", $msg);
  84. }
  85. else {
  86. $msg = $default ? $default : $msg = $command . ': No news is good news.';
  87. }
  88. return $msg;
  89. }
  90. /**
  91. * Prepares and dispatches notifications to delivery mechanisms.
  92. *
  93. * You may avoid routing a message to secondary messaging mechanisms (e.g. audio),
  94. * by direct use of the delivery functions.
  95. *
  96. * @param string $msg
  97. * Message to send via notification.
  98. */
  99. function drush_notify_send($msg) {
  100. drush_notify_send_text($msg);
  101. if (drush_get_option('notify-audio', FALSE)) {
  102. drush_notify_send_audio($msg);
  103. }
  104. }
  105. /**
  106. * Send text-based system notification.
  107. *
  108. * This is the automatic, default behavior. It is intended for use with tools
  109. * such as libnotify in Linux and Notification Center on OSX.
  110. *
  111. * @param string $msg
  112. * Message text for delivery.
  113. *
  114. * @return bool
  115. * TRUE on success, FALSE on failure
  116. */
  117. function drush_notify_send_text($msg) {
  118. $override = drush_get_option('notify-cmd', FALSE);
  119. if (!empty($override)) {
  120. $cmd = $override;
  121. }
  122. else {
  123. switch (PHP_OS) {
  124. case 'Darwin':
  125. $cmd = 'terminal-notifier -message %s -title Drush';
  126. $error_message = dt('terminal-notifier command failed. Please install it from https://github.com/alloy/terminal-notifier.');
  127. break;
  128. case 'Linux':
  129. default:
  130. $icon = drush_normalize_path(DRUSH_BASE_PATH . '/drush_logo-black.png');
  131. $cmd = "notify-send %s -i $icon";
  132. $error_message = dt('notify-send command failed. Please install it as per http://coderstalk.blogspot.com/2010/02/how-to-install-notify-send-in-ubuntu.html.');
  133. break;
  134. }
  135. }
  136. if (!drush_shell_exec($cmd, $msg)) {
  137. return drush_set_error('NOTIFY_COMMAND_NOT_FOUND', $error_message . ' ' . dt('Or you may specify an alternate command to run by specifying --notify-cmd=<my_command>'));
  138. }
  139. return TRUE;
  140. }
  141. /**
  142. * Send an audio-based system notification.
  143. *
  144. * This function is only automatically invoked with the additional use of the
  145. * --notify-audio flag or configuration state.
  146. *
  147. * @param $msg
  148. * Message for audio recital.
  149. *
  150. * @return bool
  151. * TRUE on success, FALSE on failure
  152. */
  153. function drush_notify_send_audio($msg) {
  154. $override = drush_get_option('notify-cmd-audio', FALSE);
  155. if (!empty($override)) {
  156. $cmd = $override;
  157. }
  158. else {
  159. switch (PHP_OS) {
  160. case 'Darwin':
  161. $cmd = 'say %s';
  162. break;
  163. case 'Linux':
  164. default:
  165. $cmd = drush_get_option('notify-cmd-audio', 'spd-say') . ' %s';
  166. }
  167. }
  168. if (!drush_shell_exec($cmd, $msg)) {
  169. return drush_set_error('NOTIFY_COMMAND_NOT_FOUND', dt('The third party notification utility failed.'));
  170. }
  171. }
  172. /**
  173. * Identify if the given Drush request should trigger a notification.
  174. *
  175. * @param $command
  176. * Name of the command.
  177. *
  178. * @return
  179. * Boolean
  180. */
  181. function drush_notify_allowed($command) {
  182. $notify = drush_get_option(array('notify', 'notify-audio'), FALSE);
  183. $execution = time() - $_SERVER['REQUEST_TIME'];
  184. return ($notify === TRUE ||
  185. (is_numeric($notify) && $notify > 0 && $execution > $notify));
  186. }