Skip to content

Instantly share code, notes, and snippets.

@esolitos
Last active May 4, 2022 14:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save esolitos/4bb2dff3550b529c831bf7686964fd62 to your computer and use it in GitHub Desktop.
Save esolitos/4bb2dff3550b529c831bf7686964fd62 to your computer and use it in GitHub Desktop.
Drush command "executor" on alias pattern. Built for Wodby aliases naming standard, but can work on other setups.

DrCmd - DrushComander

drush command runner for Wodby aliases

0. Install

Save the file in /usr/local/bin/ (or anywhere else as long as it is in your $PATH) and verify that it is executable.

a. Right click on "Raw" button (for drcmd) and copy the URL.

b. curl -Lo /usr/local/bin/drcmd https://gist.githubusercontent.com/esolitos/......../drcmd (Use the url from step a.)

c. chmod a+x /usr/local/bin/drcmd

d. which drcmd should return you the above path, if not you should add /usr/local/bin to your $PATH.

e. Profit

1. Usage

To execute the user-login action on an alias just run drcmd followed by a portion of the alias name. This will default on the Live instance. If more results will match you'll be promted to chose which one the comand shouyld be run into.

For example drcmd rams will match an alias like @wodby.foo.ramsalt.live and run drush command uli.

If you want to run uli in a differetn instance, just pass part of the name as the 2nd parameter

For example: drcmd rams dev will match @wodby.foobar.ramsalt.dev and run drush command uli.

Any more parameter passed is directly forwarded to drush, by default of no other parameter is passed the script assumes "user-login". You can also pass flags and options to drush see the examples.

drcmd foo dev ssh will run drush command ssh. drcmd foo dev core-cron --verbose will run drush command core-cron with the --verbose option.

Protip: Try using a single letter (or maybe two) for sitename and for the instance name, often it just works: drcmd pa l status

#!/usr/bin/env bash
#
# Searches trough the drush aliases based on a pattern and optional instance
# type performing user-login or another command.
#
# Usage:
# drcmd slug instance cmd
#
# slug = excerpt of the domain name. Example: "rams" for "ramsalt.com" or
# "goo" for "google.com"
# instance = *optional* excerpt of the Wodby instance name
# cmd... = *optional* any command to execute with optionsl flags, this is all
# passed to 'drush' so be careful on what you're passing here.
# Defaults on "user-login"
#
set -o errexit
set -o nounset
set -o pipefail
if [[ ${DEBUG:-} ]]; then
set -x
fi
readonly _self="$0"
readonly DRUSH=$(which drush)
readonly excerpt="${1:-}"
readonly instance="${2:-live}"
# Remove the first two parameters
if [[ $# -gt 2 ]]; then
shift
shift
drush_cmd="$@"
else
drush_cmd='user-login'
fi
if [[ ! -x $DRUSH ]]; then
echo "FATAL: Drush cannot be found." 1>&2
exit 1
fi
# A "excerpt" used to filter the aliases.
if [[ -z "$excerpt" ]]; then
echo "Wrong usage: you need to specify a name and optionally an instance name"
echo "Exmaple: $0 morgenbladet"
exit 2
fi
drush-get-major-version() {
local __resultvar=$1
local version_string=$($DRUSH version --pipe)
local IFS=.
declare -a v=(${version_string[@]})
if [[ ! ":${v[0]}:" =~ :[0-9]+: ]]; then
printf "FATAL: Unexpected drush version:\n> '%s'\n" "${version_string}" 1>&2
exit 1
fi
eval $__resultvar="'${v[0]}'"
}
ensure-correct-drush-version() {
drush-get-major-version drush_v
if [[ ${drush_v} -ne 8 ]]; then
printf "FATAL: Only drush 8 is currently supported.\nTry running the command like this:" 1>&2
printf "\n (cd ~ && %s %s %s %s)\n\n" "$(basename ${_self})" "${excerpt}" "${instance}" "${drush_cmd}" 1>&2
exit 1
fi
return 0
}
drush-run() {
# First is the alias name
dr_alias="$1"
# Pass all other parameters to drush
shift
dr_cmd="$@"
printf "#\n# Running command 'drush %s' on %s\n#\n" "${dr_cmd}" "${dr_alias}"
$DRUSH "$dr_alias" "$dr_cmd"
}
main() {
ensure-correct-drush-version
printf -v regex 'wodby\.[^\.]+\.%s[a-z0-9_\-]*\.%s[a-z0-9_\-]*$' "${excerpt}" "${instance}"
declare -a alias_list=(`${DRUSH} site-alias | grep -E "${regex}"`)
if [[ ${#alias_list[@]} -eq 0 ]]; then
echo "No matching alias found." 1>&2
elif [[ ${#alias_list[@]} -eq 1 ]]; then
drush-run "${alias_list[0]}" "$drush_cmd"
else
echo -e "Multiple matches, please select one\n"
select name in ${alias_list[@]}; do
drush-run "${name}" "${drush_cmd}"
exit $?
done
fi
}
main "$@"
@esolitos
Copy link
Author

esolitos commented Feb 8, 2018

Updated for bash support. (Was initially written for zsh)

@esolitos
Copy link
Author

esolitos commented Aug 6, 2018

Added missing exit

@esolitos
Copy link
Author

esolitos commented Aug 9, 2018

Added debug option and multiple parameters.

@esolitos
Copy link
Author

Fix a bug which required to pass the instance name.

@esolitos
Copy link
Author

Fixed regex to allow "exact matches" and improved code quality.

@esolitos
Copy link
Author

Fixed regex to match multiple dashes in alias

@esolitos
Copy link
Author

esolitos commented Nov 6, 2018

Updated documentation and improved matching, now wodby is required as prefix for the match.

@esolitos
Copy link
Author

Updated to handle errors from drush-launcher.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment