Skip to content

Instantly share code, notes, and snippets.

@yaauie
Last active November 11, 2024 22:23
Show Gist options
  • Save yaauie/26a3503b616f447dc596a1cf2f523cf7 to your computer and use it in GitHub Desktop.
Save yaauie/26a3503b616f447dc596a1cf2f523cf7 to your computer and use it in GitHub Desktop.
Zsh-compliant utility function that allows you to cd into a local checkout of a Logstash Plugin's source code, whether or not you have previously cloned the repository
#!/usr/bin/env zsh
#
# Utility that allows you to cd into a local checkout of a Logstash Plugin's
# source code, whether or not you have previously cloned the repository.
#
# Usage:
# cdlp <type> <qualifier>
#
# Example:
# cdlp output elasticsearch
#
# Installation:
# set `CDLP_DIR` and source this script in your shell's rc:
#
# ~~~
# export CDLP_DIR="${HOME}/src/elastic/logstash-plugins"
# source "${HOME}/scripts/cdlp"
# ~~~
#
# Environment:
# - `CDLP_DIR` : the local directory that holds logstash plugins
# - `CDLP_ORG` : optional github org from which to clone (default: `logstash-plugins`)
# - `CDLP_ORIGIN` : optional git origin name to use if a clone is necessary (default: `origin`)
# - `CDLP_FORK` : optional git fork username (default: none)
cdlp() {
cdlp_dir="${CDLP_DIR:?CDLP_DIR required}" # required
cdlp_org="${CDLP_ORG:-logstash-plugins}" # optional
cdlp_origin="${CDLP_ORIGIN:-origin}" # optional
cdlp_fork=${CDLP_FORK} # optional, no default
type_req="${1:?type required}"
case $type_req in
'-h'|'--help' )
_cdlp_help
return 2
;;
'i'|'input' ) type='input' ;;
'o'|'output' ) type='output';;
'f'|'filter' ) type='filter';;
'c'|'codec' ) type='codec';;
'm'|'mixin' ) type='mixin';;
'x'|'integration' ) type='integration';;
*) >&2 echo "unknown logstash plugin type '${type_req}'"
type="${type_req}"
esac;
qual_req="${2:?qualifier required}"
plugin_name="logstash-${type}-${qual_req}"
plugin_directory="${cdlp_dir}/${plugin_name}"
if [ -d "${plugin_directory}" ]; then
cd "$plugin_directory"
else
mkdir -p "${cdlp_dir}"
plugin_uri="[email protected]:${cdlp_org}/${plugin_name}.git"
>&2 echo "Cloning from '${plugin_uri}'..."
git clone --origin="${cdlp_origin}" "${plugin_uri}" "${plugin_directory}" || return 1
cd "$plugin_directory"
if [ ! -v 'CDLP_FORK' ]; then
vared -p "Type your fork user name (leave empty to skip):" -c 'CDLP_FORK'
fi
if [ ! -z "${CDLP_FORK}" ]; then
git remote add "${CDLP_FORK}" "[email protected]:${CDLP_FORK}/${plugin_name}.git"
git fetch "${CDLP_FORK}" || echo "don't forget to set up your '${CDLP_FORK}' fork on github:" $'\n' "-> https://github.com/${cdlp_org}/${plugin_name}/fork"
else
echo "Fork setup skipped (CDLP_FORK is empty)"
fi
fi
}
_cdlp_help() {
if [ ! -z "${1}" ]; then
echo "ERROR: ${1}"
else
echo 'cdlp is a tool that allows you to cd into a local checkout of a'
echo "Logstash plugin's source code, whether or not you have previously"
echo 'cloned the repository.'
fi
echo 'USAGE:'
echo ' $ cdlp <type> <qualifier>'
echo 'ENVIRONMENT:'
echo ' CDLP_DIR (req): path to a local directory that holds logstash plugins'
echo ' CDLP_ORG (opt): the github org from which to clone if necessary'
echo ' (default: `logstash-plugins`)'
echo ' CDLP_ORIGIN (opt): the origin name to use if a clone is necessary'
echo ' (default: `origin`)'
echo ' CDLP_FORK (opt): the fork name to use if a clone is necessary'
echo ' (default: prompt)'
}
$(return >/dev/null 2>&1) && sourced=1 || sourced=0
if [ "$sourced" = "0" ]; then
cdlp "${@}" &&
>&2 echo 'WARN: cdlp has not been sourced; you will need to `cd` manually:' &&
echo "${PWD}"
exit "$?"
fi
@jsvd
Copy link

jsvd commented Mar 4, 2019

neat! would be nice if it also set up the fork, maybe with a CDLP_FORK=yaauie + git remote add $CDLP_FORK [email protected]/$CDLP_FORK/${plugin_name}

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