Skip to content

Instantly share code, notes, and snippets.

@allex
Last active February 13, 2023 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allex/42883ae32402ea3dc181545b702ef19f to your computer and use it in GitHub Desktop.
Save allex/42883ae32402ea3dc181545b702ef19f to your computer and use it in GitHub Desktop.
#!/bin/bash
# vim: set ft=sh fdm=marker ts=2 sw=2 sts=2 tw=85 et:
set -euEo pipefail
# ================================================
# Description: #{$Description}
#
# Usage:
# curl-api [-h, --json <JSON>, --parse-token <TOKEN_NAME>]
#
# # call api with env token parsing
# export TK=xxx; curl-api http://192.168.0.34:3007/file/upload -F "imgFile=@/bg.jpeg" -F "foo=1" --parse-token TK
#
# Last Modified: Mon Feb 13, 2023 16:58
# Author: Allex Wang (allex.wxn@gmail.com)
#
# GistID: 42883ae32402ea3dc181545b702ef19f
# GistURL: https://gist.github.com/42883ae32402ea3dc181545b702ef19f
#
# Require: [curl, encode-json]
#
# ================================================
PROG=$(basename "$0")
SH_DIR="$(cd -P -- "$(dirname -- "$(readlink -f "$0")")" && pwd -P)"
WORKDIR="$(umask 0077; mktemp -d)"
DEBUG=
### Helpers {{{
BOLD="$(tput bold 2>/dev/null || echo '')"
GREY="$(tput setaf 0 2>/dev/null || echo '')"
UNDERLINE="$(tput smul 2>/dev/null || echo '')"
RED="$(tput setaf 1 2>/dev/null || echo '')"
GREEN="$(tput setaf 2 2>/dev/null || echo '')"
YELLOW="$(tput setaf 3 2>/dev/null || echo '')"
BLUE="$(tput setaf 4 2>/dev/null || echo '')"
MAGENTA="$(tput setaf 5 2>/dev/null || echo '')"
CYAN="$(tput setaf 6 2>/dev/null || echo '')"
NO_COLOR="$(tput sgr0 2>/dev/null || echo '')"
info() { printf "${BOLD}${GREY}>${NO_COLOR} %s\n" "$*"; }
warn() { printf "${YELLOW}! %s${NO_COLOR}\n" "$*" >&2; }
error() { printf "${RED}x %s${NO_COLOR}\n" "$*" >&2; }
complete() { printf "${GREEN}✓${NO_COLOR} %s\n" "$*"; }
die() { [ "${1-}" ] && error "fatal: ${1}"; exit "${2-1}"; }
assert() { [ -n "$1" ] || die "${2?assert message required}"; }
### }}}
parse_token () {
local token_name=${1-}
local tk=
if [ -f ".env" ]; then
while read -r line; do
if [ -n "$line" ]; then
key=${line%%=*}
val=${line#*=}
if [ -n "${token_name}" ]; then
if [ "$key" == "$token_name" ]; then
tk=$val
break
fi
else
case "$key" in
TOKEN | ACCESS_TOKEN | PRIVATE_TOKEN) tk=$val; break ;;
esac
fi
fi
done < .env
else
tk=$(eval "echo \${${token_name}-}")
fi
printf "%s" "$tk"
}
show_usage() {
cat <<-HELP
Usage: ${PROG} [options]
--json [json_data]
Handle request body with json data
--parse-token [token_name]
Parse ENV or .env file for authorization token header
--debug
Show curl commmand with full build args
-h, --help
Show this help info
HELP
exit 1
}
handle_exit() {
local ec="$?"
# declare -F on_exit, not avaiable in alipine/sh
if type on_exit >/dev/null 2>&1; then on_exit; fi
local tmpDir=${TMP:-${TMPDIR:-/tmp/}}
if [ "${WORKDIR##${tmpDir%%/}/*}" != "${WORKDIR}" ]; then
rm -rf "${WORKDIR--}"
else
warn "For security, The \$WORKDIR:${WORKDIR} not allot at system tmpdir, will not be cleanup"
fi
trap - INT TERM EXIT
exit $ec
}
trap handle_exit 0 1 2 3 6 15
args=()
while [ $# -gt 0 ]; do
OPT=$1
[ "$OPT" = "--" ] && break
[ "$OPT" = "${OPT#-*}" ] && {
args+=("$OPT")
shift
continue
}
if [ "${OPT##=*}" = "$OPT" ]; then
OPTARG="${2-}"
if [ "${OPTARG##-*}" = "${OPTARG}" ]; then
shift
else
OPTARG=
fi
else
OPT=$(echo "$1" | awk -F= '{print $1}')
OPTARG=$(echo "$1" | awk -F= '{print $2}')
fi
case $OPT in
--debug)
DEBUG=1
args+=(-v)
;;
-h | --help)
show_usage
;;
--json)
encode-json "$OPTARG" > "${WORKDIR}/data"
args+=(-H '"Accept: application/json"')
args+=(-H '"Content-Type: application/json"')
args+=("--data-binary" "@${WORKDIR}/data")
;;
--parse-token)
# parse env for token, default find the env key name of TOKEN | ACCESS_TOKEN | PRIVATE_TOKEN
tk="$(parse_token "$OPTARG")"
if [ -n "${tk}" ]; then
args+=(-H "\"Authorization: Bearer ${tk}\"")
fi
;;
-*)
args+=("$OPT")
[ "$OPTARG" ] && args+=("\"$OPTARG\"")
;;
esac
[ $# -gt 0 ] && shift
done
[ ${#args[@]} -gt 0 ] && eval set -- "${args[@]}"
curl -sL "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment