Last active
July 30, 2022 21:34
-
-
Save gburgett/65f732f9af9e4f75ccbec13e3e8785a5 to your computer and use it in GitHub Desktop.
Bash Script Template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
COLOR_NC='\033[0m' # No Color | |
COLOR_LGREEN='\033[1;32m' | |
COLOR_GRAY='\033[1;30m' | |
COLOR_LGRAY='\033[0;37m' | |
COLOR_RED='\033[0;31m' | |
logv() { | |
[[ -z "$VERBOSE" ]] && return 0; | |
>&2 echo -e "${COLOR_GRAY}$@${COLOR_NC}" || true | |
} | |
logerr() { | |
>&2 echo -e "${COLOR_RED}$@${COLOR_NC}" | |
} | |
log() { | |
>&2 echo -e "${COLOR_LGREEN}$@${COLOR_NC}" | |
} | |
panic() { | |
logerr "$@" | |
exit -1; | |
} | |
curlv() { | |
execv curl "$@" | |
} | |
execv() { | |
logv "$@" | |
"$@" | |
} | |
# Write your usage | |
usage() { | |
echo "$0 [command] [arg1] | |
Do something interesting | |
command: This does something. | |
arg1: This is the parameter | |
defaults to blahblah... | |
" && \ | |
grep " .)\ #" $0; exit 0; | |
} | |
parse_args() { | |
OPTIND=1 | |
local s=$(echo "$1" | tr '[:upper:]' '[:lower:]') | |
case "$s" in | |
# add other subcommands here | |
help|h|\?) | |
export subcommand=$s | |
OPTIND=2 | |
;; | |
esac | |
# Parse flags | |
while getopts ":hv" arg; do | |
case $arg in | |
v) # Verbose mode - extra output | |
VERBOSE=true | |
FLAGS="$FLAGS -v" | |
;; | |
h | *) # Display help. | |
usage | |
exit 0 | |
;; | |
esac | |
done | |
export OPTIND | |
} | |
parse_args $@ && shift $(($OPTIND - 1)) | |
# If they put args before the command like 'bin/contentful -s 1xab migrate -y', try parsing again | |
[[ -z "$subcommand" ]] && parse_args $@ && shift $(($OPTIND - 1)) | |
require_env() { | |
# Test for existence of a command | |
command -v jq >/dev/null 2>&1 || logerr "I require 'jq' but it's not installed." | |
if [[ -z "${SOME_API_KEY}" ]]; then | |
load_rc_file | |
logv "setting SOME_API_KEY" | |
export SOME_API_KEY=$(jq -r '.SOME_API_KEY' ~/.whateverrc) | |
fi | |
[[ ! -z "$SOME_API_KEY" ]] || panic "Could not load SOME_API_KEY" | |
} | |
load_rc_file() { | |
if [ -f ~/.whateverrc ]; then | |
if [[ ! -z $(jq -r '.SOME_API_KEY' ~/.whateverrc) ]]; then | |
return 0 | |
fi | |
fi | |
log "Please paste in your API key:" | |
read -s password | |
[[ -z $password ]] && panic "No api key supplied!" | |
(cat ~/.whateverrc 2>/dev/null || echo "{}") | jq ". + {SOME_API_KEY: \"$password\"}" > ~/.whateverrc.tmp | |
mv ~/.whateverrc.tmp ~/.whateverrc | |
return 0 | |
} | |
do_it() { | |
logv "Verbose to stderr" | |
logvv "Extra Verbose to stderr" | |
log "Regular Logging to stdout" | |
# Curl an API and get the ID element, logging the request to stderr when '-v' given | |
curlv -s https://some.api/entry.json | jq -r .id | |
# Run a command and log to stderr the command that was run together with result | |
logv $(execv npm install typescript) | |
# Run this same script line by line over a list of inputs | |
if [[ "$LINES" == "all" ]]; then | |
LINES=`...` # get the list | |
LAST='' | |
while read -r line; do | |
$0 $FLAGS $line $LAST | |
[[ $? -ne 0 ]] && exit -1 | |
done <<< "${LINES}" | |
exit 0 | |
fi | |
# Read a body from a file, or from stdin, or as command line args | |
if [[ -z "$1" ]]; then | |
BODY=$(cat) | |
elif [[ -f $1 ]]; then | |
BODY=$(<$1) | |
else | |
BODY="$@" | |
fi | |
# Complex print formatting | |
FORMAT="%12s $P %7d $P ${COLOR_LGREEN}%+9d${COLOR_NC} $P ${COLOR_RED}%+9d${COLOR_NC} $P %8d $P ${COLOR_LGREEN}%+6d ${COLOR_RED}%+6d${COLOR_NC} %7s \n" | |
printf $FORMAT $key $commits[$key] $additions[$key] $del $activity $spec_additions[$key] $spec_del "($specs_activity)" | |
logerr "Failure! (stderr)" && exit -1 | |
} | |
set -e | |
case $subcommand in | |
do_it) | |
require_env | |
do_it "$@" | |
;; | |
help|h|\?) | |
usage | |
;; | |
version|v) | |
echo "$VERSION" | |
;; | |
*) | |
logerr "Unknown command: '$1'" | |
usage | |
exit -1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment