From 5a09ad718d2f57f069a7b55721e9091f45dca17a Mon Sep 17 00:00:00 2001
From: James Sansbury <james.sansbury@lullabot.com>
Date: Mon, 27 Jun 2011 17:43:01 -0400
Subject: [PATCH] #1201818 by q0rban: Add some basic drush commands to
 interface with the Variable module API.

---
 variable_api.drush.inc |   97 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 97 insertions(+), 0 deletions(-)
 create mode 100644 variable_api.drush.inc

diff --git a/variable_api.drush.inc b/variable_api.drush.inc
new file mode 100644
index 0000000..4e0a542
--- /dev/null
+++ b/variable_api.drush.inc
@@ -0,0 +1,97 @@
+<?php
+
+/**
+ * @file
+ * Drush commands for Variable module.
+ */
+
+/**
+ * Implements hook_drush_command().
+ */
+function variable_api_drush_command() {
+  $items['variable-get-info'] = array(
+    'description' => dt('Display information about a variable.'),
+    'arguments' => array(
+      'name' => dt('The name of the variable.'),
+    ),
+    'drupal dependencies' => array('variable'),
+    'aliases' => array('vgeti'),
+  );
+
+  $items['variable-get-value'] = array(
+    'description' => dt('Get the value of a variable, or the default if none exists.'),
+    'arguments' => array(
+      'name' => dt('The name of the variable.'),
+    ),
+    'drupal dependencies' => array('variable'),
+    'aliases' => array('vgetv'),
+  );
+
+  $items['variable-get-default'] = array(
+    'description' => dt('Get the default value of a variable.'),
+    'arguments' => array(
+      'name' => dt('The name of the variable.'),
+    ),
+    'drupal dependencies' => array('variable'),
+    'aliases' => array('vgetd'),
+  );
+
+  return $items;
+}
+
+/**
+ * Command callback for drush variable-get-info.
+ */
+function drush_variable_get_info($name = NULL) {
+  $info = variable_get_info($name);
+  drush_print_pipe("\$variables['$name'] = ". var_export($info, TRUE). ";");
+  drush_print_r($info);
+}
+
+/**
+ * Command callback for drush variable-get-value.
+ */
+function drush_variable_get_value($name) {
+  $value = variable_get_value($name);
+  if (!isset($value)) {
+    return drush_set_error('No matching variable found.');
+  }
+
+  _variable_drush_print_variable($name, $value);
+}
+
+/**
+ * Command callback for drush variable-get-default.
+ */
+function drush_variable_get_default($name) {
+  $value = variable_get_default($name);
+  if (!isset($value)) {
+    return drush_set_error('No matching variable found.');
+  }
+
+  _variable_drush_print_variable($name, $value);
+}
+
+/**
+ * Helper function to print a variable to screen.
+ */
+function _variable_drush_print_variable($name, $value = NULL) {
+  drush_print_pipe("\$conf['$name'] = ". var_export($value, TRUE). ";");
+
+  if (!isset($value)) {
+    $value = 'NULL';
+  }
+  elseif ($value === TRUE) {
+    $value = 'TRUE';
+  }
+  elseif ($value === FALSE) {
+    $value = 'FALSE';
+  }
+  elseif (is_string($value)) {
+    $value = '"' . $value . '"';
+  }
+  elseif (is_array($value) || is_object($value)) {
+    $value = print_r($value, TRUE);
+  }
+  drush_print($name . ': ' . $value);
+}
-- 
1.7.5.4

