Skip to content

Instantly share code, notes, and snippets.

@yaauie yaauie/cdlp
Last active Mar 4, 2019

Embed
What would you like to do?
Bash/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 bash
#
# 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() {
cdlp_dir="${CDLP_DIR:?}" # required
cdlp_org="${CDLP_ORG:-logstash-plugins}" # optional
cdlp_origin="${CDLP_ORIGIN:-origin}" # optional
type_req="${1:?type required}"
qual_req="${2:?qualifier required}"
case $type_req in
'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;
plugin_name="logstash-${type}-${qual_req}"
plugin_directory="${cdlp_dir}/${plugin_name}"
if [ ! -d "${plugin_directory}" ]; then
mkdir -p "${cdlp_dir}"
plugin_uri="git@github.com:${cdlp_org}/${plugin_name}.git"
>&2 echo "Cloning from '${plugin_uri}'..."
git clone --origin="${cdlp_origin}" "${plugin_uri}" "${plugin_directory}" || return 1
fi
cd "$plugin_directory"
}
$(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

This comment has been minimized.

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 git@github.com/$CDLP_FORK/${plugin_name}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.