Skip to content

Instantly share code, notes, and snippets.

@aleliaert
Last active October 25, 2017 19:44
Show Gist options
  • Save aleliaert/162846699b52bd3757935bde6da81e5d to your computer and use it in GitHub Desktop.
Save aleliaert/162846699b52bd3757935bde6da81e5d to your computer and use it in GitHub Desktop.
Bash functions for easier use of CF CLI
#
# Some bash functions to help use the cf cli more easily
# Include in your .bashrc directly or place in another file and source
#
#
# cfhome
#
# Get a new cf home
# Use no args to get a uniquely-scoped home
# cfhome
# Provide a name arg to have a persistent and shareable home across terms
# cfhome dev
# cfhome https://api.sys.itardev.dg-south-01.satcloud.space
#
# If argument is a URL, will invoke cf api on it
# If argument provided and session not in
#
function cfhome
{
if [[ $# -eq 0 ]]; then
randy=$(dd if=/dev/urandom | tr -dc A-Za-z0-9 | head -c32)
CF_HOME="/tmp/.cfhome__${api}__${randy}"
elif [[ $1 =~ ^https\:\/\/ ]]; then
CF_HOME=${HOME}/.cfhome__${1}
cf api $1
else
CF_HOME=${HOME}/.cfhome__${1}
fi
mkdir -p $CF_HOME
export CF_HOME
# Get cf vars available, make cf calls to "furnish" our home if needed
setCfVars
if [[ -z "${cf_api}" ]] || [[ -z "${cf_user}" ]]; then
cf login
fi
} # cfhome
#
# cfhomes
#
# Get a list of your homes
#
function cfhomes
{
for i in $HOME/.cfhome__*; do
[[ "$i" =~ .cfhome__([a-zA-Z0-9_\-\.]+) ]] && echo ${BASH_REMATCH[1]}
done
}
#
# cfprompt
#
# Make your prompt have the cf_* vars set by setCfVars
# Please feel free to customize your own
# In PS1, the first funky escape chars set terminal title
# the later ones are setting color (see http://bashrcgenerator.com)
#
# To undo
# cfprompt off
#
function cfprompt
{
if [[ "$1" == "off" ]]; then
unset PROMPT_COMMAND
PS1=$CFPROMPT_OLD_PS1
return
fi
CFPROMPT_OLD_PS1=$PS1
PROMPT_COMMAND='setCfVars; echo -ne "\033]0;${cf_api:-noapi} ${cf_org:-noorg} ${cf_space:-nospace}\007"; PS1="\[\033[38;5;10m\]${cf_api:-noapi} ${cf_user:-nouser}\[$(tput sgr0)\] $cf_org $cf_space > "'
} # cfprompt
#
# Helper to have cf_api, cf_user, cf_org, and cf_space be set when available
# Are updated before prompt PS1 is reevaluated
#
function setCfVars () {
unset cf_api
unset cf_org
unset cf_space
unset cf_user
# Look for .cf dir in CF_HOME if set, otherwise HOME
cfConfigJson="${CF_HOME:-$HOME}/.cf/config.json"
# If not found, punch out
[[ -e "$cfConfigJson" ]] || return
# Get info from file
# Using awk to strip https://
result=$(jq -r '. | .Target' $cfConfigJson | awk -F/ '{print $3}')
[[ -z "$result" ]] || cf_api=$result
result=$(jq -r '. | .OrganizationFields.Name' $cfConfigJson)
[[ -z "$result" ]] || cf_org=$result
result=$(jq -r '. | .SpaceFields.Name' $cfConfigJson)
[[ -z "$result" ]] || cf_space=$result
# Since user (if set) is embedded in a jwt token, use cf cli to get user from it
# Only do this if cf_api is set, other
if command -v cf > /dev/null 2>&1 ; then
[[ $(cf target 2>/dev/null) =~ User\:[[:space:]]+([a-zA-Z0-9_\-]+) ]] && cf_user=${BASH_REMATCH[1]}
else
return
fi
export cf_api
export cf_org
export cf_space
export cf_user
} # setCfVars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment