diff --git a/plugins/dot-env/README.md b/plugins/dot-env/README.md new file mode 100644 index 000000000..ff858d471 --- /dev/null +++ b/plugins/dot-env/README.md @@ -0,0 +1,80 @@ +# dot-env for oh-my-zsh + +Adds 2 main features to oh-my-zsh: + +1. Ability to customize and maintain your oh-my-zsh environments based on global preferences, machine OS type (Darwin, Linux, Solaris), and hostname +1. Ability to cascade the customizations in hierarchical fashion from global, to os type, to specific hostname + +## Example + +Let's say you work on a Macbook Pro with a hostname "John-Smiths-Macbook-Pro", but you also frequently work on the following different remote machines over SSH: + +* rx23 - An Ubuntu 10.04 LTS box +* zebra - An Ubuntu 11.04 box +* sol-15 - A Solaris box +* technozero - A BSD box +* express - A CentOS box + +You already know, if you work on different remote machines, when you login the first time none of your aliases, shell functions or environment settings are there. What dot-env does is allow you to maintain a hierarchy of oh-my-zsh environment settings from global, down to specific host and to easily propagate those environments to each remote box from your local machine. + +So we can have a local hierarchy of settings like this: + + root + +- .oh-my-zsh + +- plugins + +- dot-env + +- global + +- global_aliases.sh + +- global_functions.sh + +- other_global_files.sh + +- os + +- Darwin + +- darwin_specific_aliases.sh + +- darwin_specific_functions.sh + +- etc.sh + +- Linux + +- SunOS + +- host + +- rx23 + +- rx23_specific_aliases.sh + +- rx23_specific_functions.sh + +- etc.sh + +- John-Smiths-Macbook-Pro.local + +- local_specific_aliases.sh + +- local_specific_functions.sh + +- local_etc.sh + +- etc... + +This hierarchy gets loaded in a cascading fashion as follows: + +- If you login to rx23: + 1. dot-env-plugin.zsh + 1. global/*.sh + 1. os/Linux/*.sh + 1. host/rx23/*.sh +- If you login to John-Smiths-Macbook-Pro + 1. dot-env-plugin.zsh + 1. global/*.sh + 1. os/Darwin/*.sh + 1. host/John-Smiths-Macbook-Pro.local/*.sh + +There are some helpful functions to maintain things: + + config_omzs_for_this_host # stubs out a hierarchy for the current host that you can edit to taste + + config_omzs_for_host # stubs out a hierarchy for a specified remote hostname + + propagate_omzs_to_host # copies your ~/.oh-my-zsh directory to a remote host + + load_omzs_on_alias # run this on a remote host to setup an alias 'omzs' to load oh-my-zsh + + load_omzs_on_login # run this on a remote host to have it automatically source oh-my-zsh on login + +* `config_omzs_for_this_host` - no arguments, just creates a directory structure for your local machine and stubs out a few .sh files + +* `config_omzs_for_host` - "Usage: config_omzs_for_host HOSTNAME" - creates a directory structure for your remote machine and stubs out a few .sh files + +* The `load_omzs_on_alias` function is nice if others also use that account and you don't want to force them to use your oh-my-zsh settings. + +* `propagate_omzs_to_host` - "Usage: propagate_omzs_to_host [user@]HOSTNAME" - compresses your `~/.oh-my-zsh` directory, uploads it to the specified host account, decompresses it and reminds you to run one of `load_omzs_on_alias` or `load_omzs_on_login` + diff --git a/plugins/dot-env/global/global_functions.sh b/plugins/dot-env/global/global_functions.sh index 5baae3549..f21867213 100644 --- a/plugins/dot-env/global/global_functions.sh +++ b/plugins/dot-env/global/global_functions.sh @@ -1,86 +1,85 @@ -# Add your public SSH key to a remote host -function add_ssh_key_to_host { - if [[ $# -lt 1 ]]; then - echo_warn "Usage: add_ssh_key_to_host [user@]HOSTNAME" - return - fi - if [[ -r ~/.ssh/id_dsa.pub ]]; then - cat ~/.ssh/id_dsa.pub | ssh $1 "cat >> .ssh/authorized_keys" - elif [[ -r ~/.ssh/id_rsa.pub ]]; then - cat ~/.ssh/id_rsa.pub | ssh $1 "cat >> .ssh/authorized_keys" +function load_omzs_on_login { + profile_file="$HOME/.zshrc" + if [[ -f "${profile_file}" ]] && + ! grep '$ZSH/oh-my-zsh.sh' "$profile_file" >/dev/null 2>&1 + then + echo '[[ -r $ZSH/oh-my-zsh.sh ]] && . $ZSH/oh-my-zsh.sh' >> "$profile_file" + echo "- oh-my-zsh will now load on login." + else + echo "- oh-my-zsh is already setup to load on login." fi + return 0 } -# Propagate your entire environment system to a remote host -function propagate_env_to_host { +function load_omzs_on_alias { + profile_file="$HOME/.zshrc" + if [[ -f "${profile_file}" ]] && + ! grep 'alias omzs=". $ZSH/oh-my-zsh.sh"' "$profile_file" >/dev/null 2>&1 + then + echo 'alias omzs=". $ZSH/oh-my-zsh.sh"' >> "$profile_file" + echo "- oh-my-zsh will now load when you execute 'omzs'." + else + echo "- oh-my-zsh is already setup to load using the 'omzs' alias." + fi + return 0 +} + + +# Propagate your environment system to a remote host +function propagate_omzs_to_host { if [[ $# -lt 1 ]]; then - echo_warn "Usage: propagate_env_to_host [user@]HOSTNAME" + echo "Usage: propagate_omzs_to_host [user@]HOSTNAME" return fi host=$1 - shift 1 - ENVFILE=$HOME/env.tar.gz + OH_MY_ZSH=$HOME/env.tar.gz PWD=`pwd` cd $HOME - echo_info "Compressing local environment..." - tar cfvz $ENVFILE .env/ &> /dev/null - echo_info "Copying environment to $host..." - scp $ENVFILE $host: - if [[ $? != 0 ]]; then echo "Copy failed!"; return; fi - echo_info "Installing environment on $host..." - ssh $host "rm -rf ~/.env/ && gunzip < env.tar.gz |tar xfv -" &> /dev/null - echo_warn "Don't forget to add this your .bashrc file:" - echo_warn 'if [[ -n "$PS1" ]]; then' - echo_warn ' [[ -r $HOME/.env/source.sh ]] && . $HOME/.env/source.sh' - echo_warn 'fi' + echo "- compressing local environment..." + tar cfvz $OH_MY_ZSH .oh-my-zsh/ &> /dev/null + echo "- copying environment to $host..." + scp $OH_MY_ZSH $host: + if [[ $? != 0 ]]; then echo "> remote copy failed [scp $OH_MY_ZSH $host]!"; return; fi + echo "- installing environment on $host..." + ssh $host "rm -rf ~/.oh-my-zsh/ && gunzip < env.tar.gz |tar xfv -" &> /dev/null + echo "- done. don't forget to run 'load_omzs_on_login' or 'load_omzs_on_alias'" cd $PWD } +function _stub_new_host_environment { + mkdir -p $1 + touch "$1/env.sh" + touch "$1/functions.sh" + if [[ ! -f "$1/alias.sh" ]]; then + echo "# Add your host specific aliases here:\n# Example: alias home='cd \$HOME' " >> "$1/alias.sh" + fi + if [[ ! -f "$1/path.sh" ]]; then + echo "# Add paths like this:\n# pathmunge \"/Developer/usr/bin\"" >> "$1/path.sh" + fi +} + # Configure environment settings for your local machine. -function configthis.env { +function config_omzs_for_this_host { DIR="$DOT_ENV_PATH/host/$HOSTNAME" - mkdir -p "$DIR" - touch "$DIR/env.sh" - touch "$DIR/functions.sh" - if [[ ! -f "$DIR/alias.sh" ]]; then - echo "# Add your specific aliases here:\n# Example: alias home='cd \$HOME' " >> "$DIR/alias.sh" - fi - if [[ ! -f "$DIR/prompt.sh" ]]; then - echo "# Define your prompt here:\n# Example: PS1=\$BLUE\u@\H\$NO_COLOR " >> "$DIR/prompt.sh" - fi - if [[ ! -f "$DIR/path.sh" ]]; then - echo "# Add paths like this:\n# pathmunge \"/Developer/usr/bin\"" >> "$DIR/path.sh" - fi + _stub_new_host_environment $DIR cd "$DIR" - echo_info "Edit these files to customize your local environment." + echo "- edit these files to customize your local environment." ls -1AtF } # Configure environment settings for a specified HOSTNAME -function confighost.env { +function config_omzs_for_host { if [[ $# -lt 1 ]]; then - echo_warn "Usage: confighost.env HOSTNAME" + echo_warn "Usage: config_omzs_for_host HOSTNAME" return fi host=$1 - shift 1 DIR="$DOT_ENV_PATH/host/$host" - mkdir -p "$DIR" - touch "$DIR/env.sh" - touch "$DIR/functions.sh" - if [[ ! -f "$DIR/alias.sh" ]]; then - echo "# Add your host specific aliases here:\n# Example: alias home='cd \$HOME' " >> "$DIR/alias.sh" - fi - if [[ ! -f "$DIR/prompt.sh" ]]; then - echo "# Define your prompt here:\n# Example: PS1=\$BLUE\u@\H\$NO_COLOR " >> "$DIR/prompt.sh" - fi - if [[ ! -f "$DIR/path.sh" ]]; then - echo "# Add paths like this:\n# pathmunge \"/Developer/usr/bin\"" >> "$DIR/path.sh" - fi + _stub_new_host_environment $DIR cd "$DIR" - echo_info "Edit these files to customize your [$host] environment." - echo_info "When you are finished run 'propagate_env_to_host $host'." + echo "- edit these files to customize your [$host] environment." + echo "- when you are finished run 'propagate_omzs_to_host $host'." ls -1AtF } diff --git a/plugins/dot-env/global/global_path.sh b/plugins/dot-env/global/global_path.sh index 1b49b79ec..b78e96dc7 100644 --- a/plugins/dot-env/global/global_path.sh +++ b/plugins/dot-env/global/global_path.sh @@ -1,20 +1,26 @@ -paths="${DOT_ENV_PATH}/bin -/sbin +paths=( /bin -/usr/X11/bin +/sbin /usr/local/bin +/usr/local/sbin +/usr/bin /usr/sbin -/usr/bin" +/usr/X11/bin +) EGREP=`which egrep` function pathmunge () { - if ! echo $PATH | $EGREP "(^|:)$1($|:)" > /dev/null ; then - if [ -d "$1" ]; then - if [ "$2" = "before" ] ; then - PATH="$1:$PATH" - else - PATH="$PATH:$1" - fi + # If it exists then remove it so we can shuffle it to the end or beginning of the PATH + if echo $PATH | $EGREP "(^|:)$1($|:)" > /dev/null ; then + safe_param=$(printf "%s\n" "$1" | sed 's/[][\.*^$(){}?+|/]/\\&/g') + PATH=`echo $PATH | sed -Ee "s/(^|:)$safe_param($|:)/:/"` + fi + # add the path in the apropriate location + if [ -d "$1" ]; then + if [ "$2" = "before" ] ; then + PATH="$1:$PATH" + else + PATH="$PATH:$1" fi fi } @@ -26,6 +32,7 @@ done # Prepend path with $HOME/bin pathmunge "$HOME/bin" before +# Remove : at the beginning and duplicate :: PATH=`echo $PATH | sed -e 's/^\://' -e 's/\:\:/:/g'` unset paths export PATH