|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +function safeExit() { |
| 4 | + exit 0 |
| 5 | +} |
| 6 | + |
| 7 | +############## Default values ################### |
| 8 | +scriptName="git whp.run" |
| 9 | +verbose=false |
| 10 | +version=0.1.0 |
| 11 | +ecsImage=ghcr.io/justcoded/http-request-proxy:${version} |
| 12 | +#ecsImage=docker.io/library/http-proxy-client-whp |
| 13 | +args=() |
| 14 | + |
| 15 | +############## Main Script Here ################### |
| 16 | + |
| 17 | +function handle() { |
| 18 | + greeting |
| 19 | + |
| 20 | + dockerTty='' |
| 21 | + if [ -t 0 ] ; then |
| 22 | + dockerTty='-t' |
| 23 | + fi; |
| 24 | + |
| 25 | + cmd="docker run --network host -i $dockerTty --rm $ecsImage" |
| 26 | + |
| 27 | + if [ $channel ]; then |
| 28 | + cmd="${cmd} --channel=${channel}" |
| 29 | + fi |
| 30 | + |
| 31 | + if [ $forward_url ]; then |
| 32 | + cmd="${cmd} --forward-url=${forward_url}" |
| 33 | + fi |
| 34 | + |
| 35 | + if $verbose; then |
| 36 | + echo "Running: $cmd" |
| 37 | + fi |
| 38 | + |
| 39 | + eval $cmd |
| 40 | +} |
| 41 | + |
| 42 | +greeting() { |
| 43 | + BRed='\033[1;31m' |
| 44 | + NC='\033[0m' # No Color |
| 45 | + echo -e "${BRed}JustCoded Http Request Proxy Tool ${version}${NC}" |
| 46 | + echo '' |
| 47 | +} |
| 48 | + |
| 49 | +die() { |
| 50 | + echo "$@" 1>&2 |
| 51 | + exit 1 |
| 52 | +} |
| 53 | + |
| 54 | +############## Begin Options and Usage ################### |
| 55 | + |
| 56 | +# Print usage |
| 57 | +usage() { |
| 58 | + echo -n "${scriptName} [OPTIONS] --channel=<channel> --forward-url=<forward_url> |
| 59 | +
|
| 60 | +This is a script template. Edit this description to print help to users. |
| 61 | +
|
| 62 | + Options: |
| 63 | + -h, --help Display this help and exit |
| 64 | + -c, --channel Channel UUID or webhook channel URL |
| 65 | + -u, --forward-url The URL to forward the request to |
| 66 | + -v, --verbose Run in verbose mode |
| 67 | +" |
| 68 | +} |
| 69 | + |
| 70 | +# Iterate over options breaking -ab into -a -b when needed and --foo=bar into |
| 71 | +# --foo bar |
| 72 | +optstring=h |
| 73 | +unset options |
| 74 | +while (($#)); do |
| 75 | + case $1 in |
| 76 | + # If option is of type -ab |
| 77 | + -[!-]?*) |
| 78 | + # Loop over each character starting with the second |
| 79 | + for ((i=1; i < ${#1}; i++)); do |
| 80 | + c=${1:i:1} |
| 81 | + |
| 82 | + # Add current char to options |
| 83 | + options+=("-$c") |
| 84 | + |
| 85 | + # If option takes a required argument, and it's not the last char make |
| 86 | + # the rest of the string its argument |
| 87 | + if [[ $optstring = *"$c:"* && ${1:i+1} ]]; then |
| 88 | + options+=("${1:i+1}") |
| 89 | + break |
| 90 | + fi |
| 91 | + done |
| 92 | + ;; |
| 93 | + |
| 94 | + # If option is of type --foo=bar |
| 95 | + --?*=*) options+=("${1%%=*}" "${1#*=}") ;; |
| 96 | + # add --endopts for -- |
| 97 | + --) options+=(--endopts) ;; |
| 98 | + # Otherwise, nothing special |
| 99 | + *) options+=("$1") ;; |
| 100 | + esac |
| 101 | + shift |
| 102 | +done |
| 103 | +set -- "${options[@]}" |
| 104 | +unset options |
| 105 | + |
| 106 | +# Print help if no arguments were passed. |
| 107 | +# Uncomment to force arguments when invoking the script |
| 108 | +# [[ $# -eq 0 ]] && set -- "--help" |
| 109 | + |
| 110 | +# Read the options and set stuff |
| 111 | +while [[ $1 = -?* ]]; do |
| 112 | + case $1 in |
| 113 | + -h|--help) usage >&2; safeExit ;; |
| 114 | + -c|--channel) shift; export channel="$1" ;; |
| 115 | + -u|--forward-url) shift; export forward_url="$1" ;; |
| 116 | + -v | --verbose) verbose=true ;; |
| 117 | + --endopts) shift; break ;; |
| 118 | + *) die "invalid option: '$1'." ;; |
| 119 | + esac |
| 120 | + shift |
| 121 | +done |
| 122 | + |
| 123 | +# Store the remaining part as arguments. |
| 124 | +args+=("$@") |
| 125 | + |
| 126 | +############## End Options and Usage ################### |
| 127 | + |
| 128 | + |
| 129 | +############## Script Run Code ################### |
| 130 | + |
| 131 | +# Set IFS to preferred implementation |
| 132 | +IFS=$'\n\t' |
| 133 | + |
| 134 | +# Exit on error. Append '||true' when you run the script if you expect an error. |
| 135 | +set -o errexit |
| 136 | + |
| 137 | +# Bash will remember & return the highest exitcode in a chain of pipes. |
| 138 | +# This way you can catch the error in case mysqldump fails in `mysqldump |gzip`, for example. |
| 139 | +set -o pipefail |
| 140 | + |
| 141 | +# Run your script |
| 142 | +handle |
| 143 | + |
| 144 | +# Exit cleanly |
| 145 | +safeExit |
0 commit comments