Skip to content

Instantly share code, notes, and snippets.

@rileyberton
Last active March 20, 2020 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rileyberton/492b71bdacd82829a60a64b36d98eab1 to your computer and use it in GitHub Desktop.
Save rileyberton/492b71bdacd82829a60a64b36d98eab1 to your computer and use it in GitHub Desktop.
Useful bash prompts when dealing with GCP
source kube-prompt.sh
source ssh-prompt.sh
RESET="\[$(tput sgr0)\]"
BLUE="\[$(tput setaf 4)\]"
YELLOW="\[$(tput setaf 3)\]"
GREEN="\[$(tput setaf 2)\]"
export PS1="${BLUE}\w\n ${GREEN}\u${YELLOW}\$(__kube_ps1)\n\$(__ssh_ps1)${RESET} \$ "
#!/bin/bash
gcloud compute ssh --project $(cat ~/.gcloud_project) --internal-ip $1
#!/bin/bash
__kube_ps1()
{
# Get current context
CONTEXT=$(kubectl config current-context)
if [ -z "$CONTEXT" ]; then
return
fi
LEN=${#CONTEXT}
if [ $LEN -lt 30 ]; then
CTX=$CONTEXT
else
let LAST="$LEN - 24"
CTX="$(echo $CONTEXT | cut -c1-3)...$(echo $CONTEXT | cut -c${LAST}-${LEN})"
fi
if [ -n "$CTX" ]; then
echo "(k8s: ${CTX})"
fi
}
kubectl_switch_context()
{
echo "Current context is: $(kubectl config current-context)"
PS3='Please choose context: '
options=($(kubectl config get-contexts --no-headers -o name))
select opt in "${options[@]}"
do
echo "Setting context: ($REPLY) $opt"
kubectl config use-context $opt
break
done
}
#!/bin/bash
__ssh_ps1()
{
# Get current context
CONTEXT=$(cat ~/.gcloud_project)
if [ -z "$CONTEXT" ]; then
return
fi
echo "(ssh: ${CONTEXT})"
}
ssh_switch_context()
{
echo "Current context is: $(cat ~/.gcloud_project)"
PS3='Please choose ssh context: '
options=($(gcloud projects list | awk 'NR > 1 {print $1}'))
select opt in "${options[@]}"
do
echo "Setting context: ($REPLY) $opt, use gssh to get on the box"
echo "$opt" > ~/.gcloud_project
break
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment