Browse Source

General cleanup

R. Alex Matevish 5 years ago
parent
commit
770895819f
12 changed files with 187 additions and 253 deletions
  1. 7 3
      conf.sh
  2. 58 49
      docker.sh
  3. 24 21
      git.sh
  4. 0 74
      granular.sh
  5. 46 39
      lib.sh
  6. 29 11
      mac.sh
  7. 23 4
      main.sh
  8. 0 28
      networking.sh
  9. 0 1
      python.sh
  10. 0 3
      ssh.sh
  11. 0 9
      webdev.sh
  12. 0 11
      yubikey.sh

+ 7 - 3
conf.sh

@@ -1,4 +1,8 @@
+##############################################################################
+#
+# Config Variables for sh-common
+#
+##############################################################################
 export CONFIG_OUTPUT_FILE="common.sh"
-export CONFIG_INSTALL_DIR="ABSOLUTE_PATH_TO_INSTALL_DIR"
-declare -a INSTALLED_CONFIGS=("array" "of" "files" "to" "install")
-
+export CONFIG_INSTALL_DIR="/Users/ramatevish/.configs/sh-common"
+declare -a INSTALLED_CONFIGS=("main" "python" "wintermute" "mac" "git" "docker" "ssh" "pgp")

+ 58 - 49
docker.sh

@@ -1,49 +1,58 @@
-function all_docker_containers() {
-    docker container ls -a | tail -n +2 | awk '{ print $1}'
-}
-
-function all_docker_images() {
-    docker image ls -a | tail -n +2 | awk '{ print $3}'
-}
-
-function don() {
-    docker container ls | grep $1 | awk '{print $1}' | xargs
-}
-
-
-alias dco='docker container'
-alias dci='docker image'
-alias dcv='docker volume'
-alias dcn='docker network'
-alias dcl='docker logs'
-alias dost='docker stats --format "table {{.Container}}\t{{.Name}}\t{{.CPUPerc}}\t{{.MemPerc}}\t{{.MemUsage}}\t{{.NetIO}}\t{{.BlockIO}}"'
-
-function docca () {
-    n=$'\n'
-    containers=`all_docker_containers`
-    if prompt_user "Run on the following containers?: $n$containers$n"; then
-        echo $containers | xargs docker container "$@"
-    fi;
-
-}
-
-function doccar () {
-    n=$'\n'
-    containers=`all_docker_containers`
-    if prompt_user "Delete the following containers: $n$containers$n"; then
-        echo $containers | xargs docker container rm -f
-    fi;
-}
-
-function doi () {
-    docker exec -it `don $1` /bin/bash
-}
-
-function dolo () {
-    command="multitail -o beep_method:popup "
-    for container in "$@"
-    do
-        command+="-cT ansi -l 'docker logs -f $container' "
-    done
-    eval ${command}
-}
+##############################################################################
+#
+# Docker Aliases
+#
+##############################################################################
+if command_exists docker; then
+
+    function all_docker_containers() {
+        docker container ls -a | tail -n +2 | awk '{ print $1}'
+    }
+
+    function all_docker_images() {
+        docker image ls -a | tail -n +2 | awk '{ print $3}'
+    }
+
+    function don() {
+        docker container ls | grep $1 | awk '{print $1}' | xargs
+    }
+
+
+    alias dco='docker container'
+    alias dci='docker image'
+    alias dcv='docker volume'
+    alias dcn='docker network'
+    alias dcl='docker logs'
+    alias dost='docker stats --format "table {{.Container}}\t{{.Name}}\t{{.CPUPerc}}\t{{.MemPerc}}\t{{.MemUsage}}\t{{.NetIO}}\t{{.BlockIO}}"'
+
+    function docca () {
+        n=$'\n'
+        containers=`all_docker_containers`
+        if prompt_user "Run on the following containers?: $n$containers$n"; then
+            echo $containers | xargs docker container "$@"
+        fi
+
+    }
+
+    function doccar () {
+        n=$'\n'
+        containers=`all_docker_containers`
+        if prompt_user "Delete the following containers: $n$containers$n"; then
+            echo $containers | xargs docker container rm -f
+        fi
+    }
+
+    function doi () {
+        docker exec -it `don $1` /bin/bash
+    }
+
+    function dolo () {
+        command="multitail -o beep_method:popup "
+        for container in "$@"
+        do
+            command+="-cT ansi -l 'docker logs -f $container' "
+        done
+        eval ${command}
+    }
+
+fi

+ 24 - 21
git.sh

@@ -3,26 +3,29 @@
 # Git Aliases
 #
 ##############################################################################
+if command_exists git; then
 
-alias gs='git status'
-alias ga='git add'
-alias gc='git checkout'
-alias gco='git commit'
-alias gd='git difftool'
-alias gb='git branch'
-alias gps='git push origin `git rev-parse --abbrev-ref HEAD`'
-alias gpl='git pull origin `git rev-parse --abbrev-ref HEAD`'
-alias gp='git pull origin `git rev-parse --abbrev-ref HEAD`'
-alias gst='git stash'
-alias gf='git fetch'
-alias gpgf='git pull && git gf'
+    alias gs='git status'
+    alias ga='git add'
+    alias gc='git checkout'
+    alias gco='git commit'
+    alias gd='git difftool'
+    alias gb='git branch'
+    alias gps='git push origin `git rev-parse --abbrev-ref HEAD`'
+    alias gpl='git pull origin `git rev-parse --abbrev-ref HEAD`'
+    alias gp='git pull origin `git rev-parse --abbrev-ref HEAD`'
+    alias gst='git stash'
+    alias gf='git fetch'
+    alias gpgf='git pull && git gf'
 
-function git-delete-merged () {
-    n=$'\n'
-    branches=$(git branch --merged $1 | egrep -v "(^\*|master|dev)");
-    if prompt_user "The following branches would be deleted:$n$branches$n"; then
-        git branch --merged $1 | egrep -v "(^\*|master|dev)" | xargs git branch --delete;
-    fi;
-}
-alias gbdm='git-delete-merged'
-alias glatest='git checkout master && git pull && git fetch && git-delete-merged'
+    function git-delete-merged () {
+        n=$'\n'
+        branches=$(git branch --merged $1 | egrep -v "(^\*|master|dev)");
+        if prompt_user "The following branches would be deleted:$n$branches$n"; then
+            git branch --merged $1 | egrep -v "(^\*|master|dev)" | xargs git branch --delete;
+        fi
+    }
+    alias gbdm='git-delete-merged'
+    alias glatest='git checkout master && git pull && git fetch && git-delete-merged'
+
+fi

+ 0 - 74
granular.sh

@@ -1,74 +0,0 @@
-alias rg='rm -rf /grjd_cache/stores/*; gg; vm-scripts/install_bin.sh; resetgrjd; resetgrjd postgresql://postgres:Granular1@localhost/grjd_test; cp admin/config/development.ini local.ini; python grjd/scripts/seed_db.py --commit; python grjd/scripts/seed_db.py --commit --test'
-
-alias au='alembic upgrade'
-alias ad='alembic downgrade'
-
-function ah() {
-    alembic history | grep 'Rev: .* (head)' | awk '{print $2}'
-}
-function ac() {
-    alembic current 2>&1 | gawk 'match($0, /([0-9a-f]+) -> ([0-9a-f]+)/, a) {print a[2]; exit}'
-}
-function af() {
-    CURRENT=$(ac)
-    if [ $CURRENT == $1 ]; then
-        echo "Already at $1"
-        return 0
-    fi
-    echo "Current revision: $CURRENT"
-    echo "Target revision: $1"
-
-    alembic downgrade $1 > /dev/null 2>&1
-    alembic upgrade $1 > /dev/null 2>&1
-
-    CURRENT=$(ac)
-    if [ $CURRENT != $1 ]; then
-        echo "Couldn't find revision: $1"
-        return 1
-    else
-        echo "Found revision: $1"
-        return 0
-    fi
-}
-
-_JUMP_NETWORK_INTERFACE="Wi-Fi"
-
-function _jump-on() {
-    echo "Enabling SOCKS Proxy"
-    networksetup -setsocksfirewallproxystate $_JUMP_NETWORK_INTERFACE on
-    echo "Jump: Enabled"
-    ssh jump
-}
-
-function _jump-off() {
-    TUNNEL_PROCS=`ps aux | grep "ssh jump" | grep -v grep | awk '{print $2}'`
-    if [[ $TUNNEL_PROCS ]]; then
-        echo "Killing SSH Tunnel"
-        kill -9 $TUNNEL_PROCS
-    fi
-    echo "Disabling SOCKS Proxy"
-    networksetup -setsocksfirewallproxystate $_JUMP_NETWORK_INTERFACE off
-    echo "Jump: Disabled"
-}
-
-function _jump-enabled {
-    networksetup -getsocksfirewallproxy $_JUMP_NETWORK_INTERFACE | grep '^Enabled: Yes' > /dev/null 2>&1
-}
-
-function jump() {
-    case "$1" in
-    on)
-        _jump-on
-        ;;
-    off)
-        _jump-off
-        ;;
-    *)
-        _jump-enabled
-        if [ $? -eq 0 ]; then
-            _jump-off
-        fi
-        _jump-on
-        _jump-off
-    esac
-}

+ 46 - 39
lib.sh

@@ -10,8 +10,8 @@ init () {
     # Value: yes or no (y / n)
     #
     DEBUG=no
-    
-    # 
+
+    #
     # Syslog style log messages
     #
     if ! defined LOGDATEFORMAT
@@ -22,7 +22,7 @@ init () {
     then
         LOG_FILE=$0.log
     fi
-    
+
     #
     # Enable / disable logging to a file
     # Value: yes or no (y / n)
@@ -39,7 +39,7 @@ init () {
     then
         SYSLOG_TAG=$0
     fi
-    
+
     #
     # Use colours in output.
     #
@@ -52,15 +52,15 @@ init () {
     LIGHT_BLUE="$CYAN"
     BOLD="tput bold"
     DEFAULT="tput sgr0"
-    
+
     RED_BG="tput setab 1"
     GREEN_BG="tput setab 2"
     YELLOW_BG="tput setab 3"
     BLUE_BG="tput setab 4"
     MAGENTA_BG="tput setab 5"
     CYAN_BG="tput setab 6"
-    
-    # 
+
+    #
     # Bug fix for Bash, parsing exclamation mark.
     #
     set +o histexpand
@@ -141,6 +141,13 @@ function file_exists {
     return 1
 }
 
+function command_exists {
+    if command -v "$1" > /dev/null 2>&1; then
+        return 0;
+    fi
+    return 1
+}
+
 #
 # returns lowercase string
 #
@@ -167,8 +174,8 @@ function trim {
 # Override this function if required.
 #
 show_usage () {
-   
-    MESSAGE="$1" 
+
+    MESSAGE="$1"
     echo "$MESSAGE"
     exit 1
 }
@@ -190,7 +197,7 @@ option_enabled () {
 }
 
 #
-# The log funcion just puts a string into a file, prepended with a date & time in 
+# The log funcion just puts a string into a file, prepended with a date & time in
 # syslog format.
 #
 
@@ -206,13 +213,13 @@ log2syslog () {
 #
 # This function writes messages to a log file and/or syslog
 # The only argument is a message that has to be logged.
-# 
+#
 
 log () {
-    
-    if option_enabled LOG_ENABLED || option_enabled SYSLOG_ENABLED 
+
+    if option_enabled LOG_ENABLED || option_enabled SYSLOG_ENABLED
     then
-        LOG_MESSAGE="$1" 
+        LOG_MESSAGE="$1"
         DATE=`date +"$LOGDATEFORMAT"`
 
         if has_value LOG_MESSAGE
@@ -230,8 +237,8 @@ log () {
         if option_enabled SYSLOG_ENABLED
         then
             #
-            # Syslog already prepends a date/time stamp so only the message 
-            # is logged. 
+            # Syslog already prepends a date/time stamp so only the message
+            # is logged.
             #
             log2syslog "$LOG_MESSAGE"
         fi
@@ -241,7 +248,7 @@ log () {
 
 #
 # This function basically replaces the 'echo' function in bash scripts.
-# The added functionality over echo is logging and using colors. 
+# The added functionality over echo is logging and using colors.
 #
 # The first argument is the string / message that must be displayed.
 # The second argument is the text color.
@@ -259,7 +266,7 @@ msg () {
     if has_value "MESSAGE"
     then
         $COLOR
-        echo "$MESSAGE" 
+        echo "$MESSAGE"
         $DEFAULT
         log "$MESSAGE"
     else
@@ -269,7 +276,7 @@ msg () {
 }
 
 #
-# This function echos a message 
+# This function echos a message
 # and displays the status at the end of the line.
 #
 # It can be used to create status messages other
@@ -291,8 +298,8 @@ msg_status () {
 #
 
 #
-# The following functions are shorthand for 
-# msg_status "a message" OK 
+# The following functions are shorthand for
+# msg_status "a message" OK
 # msg_status "another message" FAIL
 
 
@@ -439,7 +446,7 @@ display_status () {
 
     STATUS="$1"
 
-    case $STATUS in 
+    case $STATUS in
 
     EMERGENCY )
             STATUS="EMERGENCY"
@@ -454,30 +461,30 @@ display_status () {
             COLOR="$RED"
             ;;
     ERROR )
-            STATUS="  ERROR  " 
+            STATUS="  ERROR  "
             COLOR="$RED"
             ;;
 
     WARNING )
-            STATUS=" WARNING "  
+            STATUS=" WARNING "
             COLOR="$YELLOW"
             ;;
 
     NOTICE )
-            STATUS=" NOTICE  "  
+            STATUS=" NOTICE  "
             COLOR="$BLUE"
             ;;
     INFO )
-            STATUS="  INFO   "  
+            STATUS="  INFO   "
             COLOR="$LIGHT_BLUE"
             ;;
     DEBUG )
             STATUS="  DEBUG  "
             COLOR="$DEFAULT"
-            ;;    
+            ;;
 
-    OK  ) 
-            STATUS="   OK    "  
+    OK  )
+            STATUS="   OK    "
             COLOR="$GREEN"
             ;;
     NOT_OK)
@@ -485,18 +492,18 @@ display_status () {
             COLOR="$RED"
             ;;
 
-    PASSED ) 
-            STATUS=" PASSED  "  
+    PASSED )
+            STATUS=" PASSED  "
             COLOR="$GREEN"
             ;;
 
-    SUCCESS ) 
-            STATUS=" SUCCESS "  
+    SUCCESS )
+            STATUS=" SUCCESS "
             COLOR="$GREEN"
             ;;
-    
+
     FAILURE | FAILED )
-            STATUS=" FAILED  "  
+            STATUS=" FAILED  "
             COLOR="$RED"
             ;;
 
@@ -510,7 +517,7 @@ display_status () {
 
 
 #
-# Exit with error status 
+# Exit with error status
 #
 bail () {
 
@@ -536,7 +543,7 @@ cmd () {
     ERROR="$?"
 
     MSG="Command: ${COMMAND:0:29}..."
-    
+
     tput cuu1
 
     if [ "$ERROR" == "0" ]
@@ -564,7 +571,7 @@ now () {
 }
 
 elapsed () {
-    
+
     START="$1"
     STOP="$2"
 
@@ -642,7 +649,7 @@ function str_replace_in_file () {
     local DEST="$2"
     local FILE="$3"
 
-    has_value FILE 
+    has_value FILE
     die_if_false $? "Empty argument 'file'"
     file_exists "$FILE"
     die_if_false $? "File does not exist"

+ 29 - 11
mac.sh

@@ -4,22 +4,40 @@
 #
 ##############################################################################
 
-function subl() {
-    /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl $1
-}
+if file_exists "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl"; then
+    function subl() {
+        /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl ${1:-.}
+    }
+fi;
 
 # Record audio files
-function record() {
-  sox -d $1
-}
-
-function flush_mac_dns() {
-  sudo killall -HUP mDNSResponder;sudo killall mDNSResponderHelper;sudo dscacheutil -flushcache
-}
+if command_exists sox; then
+    alias record="sox -d ${1:-out.mp3}"
+fi
 
 # Only enable if Kaleidoscope is installed
 if grep Kaleidoscope ~/.gitconfig > /dev/null 2>&1; then
     alias gm='git mergetool -y -t Kaleidoscope';
 fi
 
-alias uuidgen="uuidgen | awk '{print tolower($1)}'"
+if command_exists uuidgen; then
+    alias uuidgen="uuidgen | awk '{print tolower($1)}'"
+fi
+
+function rmdstore() {
+    find . -name '.DS_Store' -type f -delete
+}
+
+if command_exists ykman; then
+    function 2fac () {
+        if defined $1; then
+            ykman oath code $1 | tee /dev/tty | awk '{ print $2 }' | pbcopy
+        else
+            ykman oath code
+        fi
+    }
+
+    function 2facs() {
+        watch -n1 -d ykman oath code
+    }
+fi

+ 23 - 4
main.sh

@@ -93,11 +93,30 @@ function loc() {
     find . -name "*.${1:-py}" | xargs wc -l
 }
 
-function rmdstore() {
-    find . -name '.DS_Store' -type f -delete
+# URL encode/decode
+alias urldecode='python -c "import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])"'
+alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1])"'
+
+# Get my IP
+function myip () {
+  wget http://ipinfo.io/ip -qO -;
 }
 
+#
+# Nmap stuff
+#
+
+# Get ssh host
+function nmap_ssh_host_keys() {
+    nmap -p 22 --script ssh-hostkey -v $1 -Pn
+}
+
+function nmap_host_os() {
+    sudo nmap -O -v $1
+}
+
+function nmap_scan_port() {
+    sudo nmap -p $2 $1
+}
 
-##### UUID regexp
-UUID_REGEXP='\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b'
 

+ 0 - 28
networking.sh

@@ -1,28 +0,0 @@
-##############################################################################
-#
-# Networking Commands
-#
-##############################################################################
-
-# Get my IP
-function myip () {
-  wget http://ipinfo.io/ip -qO -;
-}
-
-#
-# Nmap stuff
-#
-
-# Get ssh host
-function nmap_ssh_host_keys() {
-    nmap -p 22 --script ssh-hostkey -v $1 -Pn
-}
-
-function nmap_host_os() {
-    sudo nmap -O -v $1
-}
-
-function nmap_scan_port() {
-    sudo nmap -p $2 $1
-}
-

+ 0 - 1
python.sh

@@ -6,4 +6,3 @@
 
 alias rmpyc='find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf'
 alias workon='pyenv activate'
-

+ 0 - 3
ssh.sh

@@ -62,9 +62,6 @@ function fix-gpg-agent () {
     start-gpg-keychain;
 }
 
-start-ssh-agent;
-start-gpg-agent;
-
 #############################################################################
 #
 # SSH Utility Functions

+ 0 - 9
webdev.sh

@@ -1,9 +0,0 @@
-##############################################################################
-#
-# Web Development Aliases
-#
-##############################################################################
-
-alias urldecode='python -c "import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])"'
-alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1])"'
-

+ 0 - 11
yubikey.sh

@@ -1,11 +0,0 @@
-function 2fac () {
-    if defined $1; then
-        ykman oath code $1 | tee /dev/tty | awk '{ print $2 }' | pbcopy
-    else
-        ykman oath code
-    fi
-}
-
-function 2facs() {
-    watch -n1 -d ykman oath code
-}