Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shell aliases (or yml commands) #2943

Open
greg-1-anderson opened this issue Sep 18, 2017 · 13 comments
Open

Shell aliases (or yml commands) #2943

greg-1-anderson opened this issue Sep 18, 2017 · 13 comments
Milestone

Comments

@greg-1-anderson
Copy link
Member

greg-1-anderson commented Sep 18, 2017

We should follow the lead of Travis CI and Circle CI & c. and put simple shell scripts in our yml configuration files:

shell:
  -
    command: hello
    option: who
    default: who World
    script:
      - echo "Hello, $WHO"
  -
    command: knock:knock
    script:
      - echo "Who's there?"

Environment variables are automatically set for each defined option and argument.

We could potentially have a shortcut for Drush commands, but I'm not sure that it is necessary.

@weitzman
Copy link
Member

Look good to me, as long as we don't need much custom code to process the yaml file and execute the script. Perhaps there is code we can bring in.

@greg-1-anderson
Copy link
Member Author

Or maybe:

shell:
  -
    command: hello
    options
      who: World
      count: 1
    option-descriptions:
      who: Who to say hello to
      count: How many times to say hello
    script:
      - seq $COUNT | xargs -Iz echo "Hello, $WHO"

The question is, is either of these forms significantly simpler than a php commandfile to make this worthwhile?

@weitzman weitzman added this to the drush9 milestone Sep 25, 2017
@weitzman
Copy link
Member

weitzman commented Oct 1, 2017

If we are going for a straight replacement, the existing logic is at

drush/includes/command.inc

Lines 1930 to 1996 in fd5b1c7

function drush_shell_alias_replace($target_site_alias) {
$escape = TRUE;
$args = drush_get_arguments();
$argv = drush_get_context('argv');
$first = current($args);
// @todo drush_get_option is awkward here.
$shell_aliases = drush_get_context('shell-aliases', array());
if (isset($shell_aliases[$first])) {
// Shell alias found for first argument in the request.
$alias_value = $shell_aliases[$first];
if (!is_array($alias_value)) {
// Shell aliases can have embedded variables such as {{@target}} and {{%root}}
// that are replaced with the name of the target site alias, or the value of a
// path alias defined in the target site alias record. We only support replacements
// when the alias value is a string; if it is already broken out into an array,
// then the values therein are used literally.
$alias_variables = array( '{{@target}}' => '@none' );
if ($target_site_alias) {
$alias_variables = array( '{{@target}}' => $target_site_alias );
$target = drush_sitealias_get_record($target_site_alias);
foreach ($target as $key => $value) {
if (!is_array($value)) {
$alias_variables['{{' . $key . '}}'] = $value;
}
}
if (array_key_exists('path-aliases', $target)) {
foreach ($target['path-aliases'] as $key => $value) {
// n.b. $key will contain something like "%root" or "%files".
$alias_variables['{{' . $key . '}}'] = $value;
}
}
}
$alias_value = str_replace(array_keys($alias_variables), array_values($alias_variables), $alias_value);
// Check for unmatched replacements
$matches = array();
$match_result = preg_match('/{{[%@#]*[a-z0-9.]*}}/', $alias_value, $matches);
if ($match_result) {
$unmatched_replacements = implode(', ', $matches);
$unmatched_replacements = preg_replace('/[{}]/', '', $unmatched_replacements);
return drush_set_error('DRUSH_SHELL_ALIAS_UNMATCHED_REPLACEMENTS', dt('The shell alias @alias-name uses replacements "@unmatched". You must use this command with a site alias (e.g. `drush @myalias @alias-name ...`) that defines all of these variables.', array('@alias-name' => $first, '@unmatched' => $unmatched_replacements)));
}
if (substr($alias_value, 0, 1) == '!') {
$alias_value = ltrim($alias_value, '!');
$alias_value = array('core-execute', $alias_value);
$escape = FALSE;
}
else {
// Respect quoting. See http://stackoverflow.com/questions/2202435/php-ex
$alias_value = str_getcsv($alias_value, ' ');
}
}
drush_log(dt('Shell alias found: !key => !value', array('!key' => $first, '!value' => implode(' ', $alias_value))), LogLevel::DEBUG);
$pos = array_search($first, $argv);
$number = 1;
if ($target_site_alias && ($argv[$pos - 1] == $target_site_alias)) {
--$pos;
++$number;
}
array_splice($argv, $pos, $number, $alias_value);
if (!$escape) {
drush_set_option('escape', FALSE);
}
drush_set_context('argv', $argv);
drush_parse_args();
_drush_preflight_global_options();
}
}

@weitzman
Copy link
Member

weitzman commented Oct 9, 2017

Here is drush9 equivalent of config-list drush config-status --operation=all --format=list

@weitzman weitzman changed the title Shell aliases for Drush 9 Symfony Dispatch Shell aliases (or yml commands) for Drush 9 Feb 12, 2018
@johnpitcairn
Copy link

What is the current status of this? I'm currently pegged at drush 9.0-beta4, which appears to be the last version that supports old-style drushrc.php shell command aliases.

I'm very conscious that is not an ideal situation, but I depend fairly heavily on some custom pull-production (etc) shell aliases for my current workflow.

@weitzman
Copy link
Member

No update unfortunately. Its an open feature request. I suggest using bash aliases and drush commands/scripts to transition from drush shell aliases.

@greg-1-anderson
Copy link
Member Author

You could also consider using a similar service such as ahoy-cli

@johnpitcairn
Copy link

Thanks @greg-1-anderson . I looked at Ahoy but at present I'm all about reducing lateral dependencies, so anything that needs a custom install per platform is really a no-go. Ideally Drush would continue to handle this via an optional composer dependency. I don't especially care whether I would need to rewrite the existing PHP script definitions as YML, I'd just like to be able to do so.

@weitzman : Yeah. But then everyone is back to rolling their own solution in bash or cli-php or whatever, and isn't that what Drush was designed to avoid for the slightly savvy site builder?

@greg-1-anderson
Copy link
Member Author

The status here is "PRs welcome". Someone just needs to feel it is important enough to spend the effort.

@generalredneck
Copy link

So instead of shell aliases... the current workaround is to go ahead and write a custom command that calls the shell correct?

The reason I ask is because I rely on drush shell aliases because they are typically supported on hosting environments where calling shell via ssh or other means are not... example I can do something like drush deploy and it's something like drush updb; drush cim -y; drush cr; all in one for D8 and drush rr; drush updb -y; drush cc all; drush fra -y; drush cc all; for D7... and it's consistent so that anyone deploying only has to know the 1 command.

@greg-1-anderson
Copy link
Member Author

@generalredneck I use composer scripts for that purpose. Writing simple Drush commands in php is another option.

If someone did want to make a PR for this, perhaps it should be a feature of Robo / consolidation/config.

@weitzman
Copy link
Member

Also, I've used Ahoy for yaml aliases

@weitzman
Copy link
Member

Not a burning need for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants