David K. Bainbridge | 7ae4f19 | 2016-09-29 09:01:38 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | PROG=$(basename $0) |
| 4 | |
| 5 | usage() { |
| 6 | echo "$PROG [-s|--server <head-node-ip>] [-p|--port <head-node-ssh-port>] [-u|--user <head-node-ssh-user>] <command> <options>" |
| 7 | |
| 8 | # Find all files in path that match the pattern "cord-*" |
| 9 | ALL_FILES= |
| 10 | for DIR in $(echo $PATH | sed -e 's/:/ /g'); do |
| 11 | ALL_FILES="$ALL_FILES $(/bin/ls -1 $DIR/cord-* 2> /dev/null)" |
| 12 | done |
| 13 | |
| 14 | # Filter that down to only those files that we can "execute" |
| 15 | COMMANDS= |
| 16 | for CMD in $ALL_FILES; do |
| 17 | test -x "$CMD" && COMMANDS="$COMMANDS $CMD" |
| 18 | done |
David K. Bainbridge | 6e23ac8 | 2016-12-07 12:55:41 -0800 | [diff] [blame^] | 19 | |
David K. Bainbridge | 7ae4f19 | 2016-09-29 09:01:38 -0700 | [diff] [blame] | 20 | # Process comands for usage information |
| 21 | # Output all commands and their help information to file |
| 22 | # so it can be sorted. The format will be: |
| 23 | # |
| 24 | # command usage_message |
| 25 | |
| 26 | # Find longest command name for table spacing |
| 27 | MAX=0 |
| 28 | for CMD in $COMMANDS; do |
| 29 | LEN=$(echo $(basename $CMD | sed -e 's/^[^-]*-//g') | wc -c) |
| 30 | test $LEN -gt $MAX && MAX=$LEN |
| 31 | done |
| 32 | |
| 33 | FILE=$(mktemp) |
| 34 | # Process all the commands into the usage file |
| 35 | for CMD in $COMMANDS; do |
| 36 | NAME=$(basename $CMD | sed -e 's/^[^-]*-//g') |
| 37 | DESC=$(grep "^#D " $CMD | head -1 | sed -e 's/^#D\w*//g' ) |
| 38 | printf " %-${MAX}s %s\n" $NAME "$DESC" >> $FILE |
| 39 | done |
| 40 | sort -u $FILE |
| 41 | |
| 42 | # clean up |
| 43 | rm -f $FILE |
| 44 | COMMANDS=$(echo $COMMANDS | sed -e 's/w+/ /g') |
| 45 | } |
| 46 | |
| 47 | if [ $# -eq 0 ]; then |
| 48 | usage |
| 49 | exit 1 |
| 50 | fi |
| 51 | |
| 52 | SHORT= |
| 53 | while [ "$SHORT x" == " x" -a $# -gt 0 ]; do |
| 54 | case $1 in |
| 55 | help|-h|--help) |
| 56 | usage |
| 57 | exit 0 |
| 58 | ;; |
| 59 | -s|--server) |
| 60 | shift |
| 61 | if [ $# -eq 0 ]; then |
| 62 | >&2 echo "Server parameter must be specified with '--server' option" |
| 63 | usage |
| 64 | exit 1 |
| 65 | fi |
| 66 | export CORD_HEAD_NODE="$1" |
| 67 | ;; |
| 68 | -u|--user) |
| 69 | shift |
| 70 | if [ $# -eq 0 ]; then |
| 71 | >&2 echo "User parameter must be specified with '--user' option" |
| 72 | usage |
| 73 | exit 1 |
| 74 | fi |
| 75 | export CORD_HEAD_NODE_USER="$1" |
| 76 | ;; |
| 77 | -p|--port) |
| 78 | shift |
| 79 | if [ $# -eq 0 ]; then |
| 80 | >&2 echo "Port parameter must be specified with '--port' option" |
| 81 | usage |
| 82 | exit 1 |
| 83 | fi |
| 84 | export CORD_HEAD_NODE_PORT="$1" |
| 85 | ;; |
| 86 | -*) |
| 87 | >&2 echo "Unknown command line option '$1'." |
| 88 | usage |
| 89 | exit 1 |
| 90 | ;; |
| 91 | *) |
| 92 | SHORT=$1 |
| 93 | ;; |
| 94 | esac |
| 95 | shift |
| 96 | done |
| 97 | |
| 98 | if [ "$SHORT x" == " x" ]; then |
| 99 | >&2 echo "CORD command must be specified" |
| 100 | usage |
| 101 | exit 1 |
| 102 | fi |
| 103 | |
| 104 | COMMAND="cord-$SHORT" |
| 105 | FULL_COMMAND=$(which $COMMAND) |
| 106 | |
| 107 | if [ ! -x "$FULL_COMMAND" ]; then |
| 108 | >&2 echo "Unknown command specified '$SHORT'." |
| 109 | usage |
| 110 | exit 1 |
| 111 | fi |
| 112 | |
| 113 | test -z $CORD_HEAD_NODE && export CORD_HEAD_NODE="localhost" |
| 114 | test -z $CORD_HEAD_NODE_USER && export CORD_HEAD_NODE_USER="ubuntu" |
| 115 | test -z $CORD_HEAD_NODE_PORT && export CORD_HEAD_NODE_PORT="22" |
| 116 | |
| 117 | exec "$FULL_COMMAND" $* |
| 118 | |
| 119 | |