Modules that provide drush support want to expose options to the command, eg
drush mycommand --option=foo
These will sometimes correspond to options in the GUI that are set as variables, in an admin page.

So the module's workhorse function is designed to read these in with variable_get() when it needs them.
This won't allow drush to work.

One way to get this to work is to rewrite the workhorse so all its options come is as parameters. This *may* be a good thing from the point of view of testing, but it's not always ideal.

The other way I can see is to have the drush command callback look up variables, store them, set them to the options, run the module's workhorse, restore the variables.
This is something I am looking into for #608410: Add drush support.

It seems to me this might as well be in drush core so all modules may take advantage of it.

What I'd suggest is:

drush_temp_variable_set($var, $value); // safely sets the given variable, storing its old value
drush_temp_variable_restore($var); // restore the variable. If no parameter given, restore ALL variables previously set.

Comments

moshe weitzman’s picture

Status: Active » Fixed

You can override drupal variables on just for the duration of a drush request. see example.drushrc.php

joachim’s picture

Status: Fixed » Active

I'm not managing to get this to work from a Drush command callback:

  // using code from drush_load_config()
  $override = array(
     'site_name' => 'TEST OVERRIDE',
  );
 
  global $drush_conf_override;
  $drush_conf_override = array_merge($drush_conf_override, $override);
 
  print variable_get('site_name');

_drush_bootstrap_drupal_configuration() which is where the overriding appears to happen, is called before command callbacks are executed.

moshe weitzman’s picture

Status: Active » Closed (fixed)

Right, the built-in override stuff cannot work in a command callback. Your question is no longer drush specific. It is 'how do i temporarily override variable_get()'. To do that, you have toalter values in global $conf.

joachim’s picture

Status: Closed (fixed) » Active

Right -- as I said in my initial post, I want to do this in a command callback.
This was partly a question and partly a proposal -- if I'm going to create some functions in *my* module that allow a command callback to temporarily override variables, then how about I contribute them to drush to *other* modules can also make use of them?

Sorry if my initial post wasn't clear.
Basically: I'm going to code this feature. You want it for drush or not?

moshe weitzman’s picture

Status: Active » Closed (works as designed)

I don't think I do. This is the same question for any drupal code, not drush specific.