- requires env file (associative array) with commands to execute
- requires to add two profiles with custom background colors to iTerm2
- both profiles should have their badge set to
\(user.badge)
| aws-dev=ssh -i ~/.ssh/aws-dev [email protected] | |
| aws-ent=ssh -i ~/.ssh/aws-ent [email protected] | |
| aws-prod=ssh -i ~/.ssh/aws-prod [email protected] |
| #!/bin/zsh | |
| if [ $# -eq 0 ] | |
| then | |
| echo No arguments supplied | |
| exit 1 | |
| fi | |
| config=~/.ssh/env | |
| declare -A MYMAP | |
| if [[ -f "$config" ]]; then | |
| while IFS= read -r line; do | |
| MYMAP[${line%%=*}]=${line#*=} | |
| done < "$config" | |
| else | |
| echo Missing configuration file | |
| exit 1 | |
| fi | |
| on_exit () { | |
| # back to default profile with its badge | |
| echo "\033]50;SetProfile=Default\a" | |
| } | |
| # want traps so we can restore default profile after we are done | |
| trap on_exit INT | |
| trap on_exit EXIT | |
| trap on_exit SIGINT | |
| trap on_exit ERR | |
| # setting variables for iTerm2 | |
| iterm2_set_user_var (){ | |
| printf "\033]1337;SetUserVar=%s=%s\007" "$1" $(printf "%s" "$2" | base64 | tr -d '\n') | |
| } | |
| # change profile of iTerm2 | |
| change_profile(){ | |
| # change profile | |
| echo "\033]50;SetProfile=$1\a" | |
| } | |
| cmd=${MYMAP[$1]} | |
| if [[ -z "$cmd" ]]; then | |
| echo $1 not congfigured | |
| exit 3 | |
| fi | |
| echo $cmd | |
| case $1 in | |
| *"dev"*) | |
| change_profile Dev | |
| ;; | |
| *"ent"*|*"prod"*) | |
| change_profile Prod | |
| ;; | |
| esac | |
| iterm2_set_user_var badge "$1" | |
| eval $cmd |