Added shfmt to autoformat shell scripts (#86)

This commit is contained in:
Anton Babenko 2020-01-21 11:54:13 +01:00 committed by GitHub
commit 1e5b3af0d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 269 additions and 209 deletions

View file

@ -8,8 +8,8 @@ repos:
- id: check-case-conflict - id: check-case-conflict
- id: check-merge-conflict - id: check-merge-conflict
- id: check-executables-have-shebangs - id: check-executables-have-shebangs
#- repo: git://github.com/jumanjihouse/pre-commit-hooks - repo: git://github.com/jumanjihouse/pre-commit-hooks
# rev: 1.11.2 rev: 1.11.2
# hooks: hooks:
# - id: shellcheck - id: shfmt
# - id: shfmt args: ['-l', '-i', '2', '-ci', '-sr', '-w']

View file

@ -12,12 +12,12 @@ main() {
for argv; do for argv; do
case $argv in case $argv in
(-a|--args) -a | --args)
shift shift
args="$1" args="$1"
shift shift
;; ;;
(--) --)
shift shift
files="$@" files="$@"
break break
@ -36,7 +36,7 @@ main() {
local is_old_terraform_docs local is_old_terraform_docs
is_old_terraform_docs=$(terraform-docs version | grep -o "v0.[1-7]" | tail -1) is_old_terraform_docs=$(terraform-docs version | grep -o "v0.[1-7]" | tail -1)
if [[ -z "$is_old_terraform_docs" ]]; then # Using terraform-docs 0.8+ (preferred) if [[ -z "$is_old_terraform_docs" ]]; then # Using terraform-docs 0.8+ (preferred)
terraform_docs "0" "$args" "$files" terraform_docs "0" "$args" "$files"
@ -78,7 +78,7 @@ terraform_docs() {
tfvars_files+=("$file_with_path") tfvars_files+=("$file_with_path")
fi fi
((index+=1)) ((index += 1))
done done
readonly tmp_file=$(mktemp) readonly tmp_file=$(mktemp)
@ -94,18 +94,18 @@ terraform_docs() {
continue continue
fi fi
if [[ "$terraform_docs_awk_file" == "0" ]]; then if [[ "$terraform_docs_awk_file" == "0" ]]; then
terraform-docs md "$args" ./ > "$tmp_file" terraform-docs md "$args" ./ > "$tmp_file"
else else
# Can't append extension for mktemp, so renaming instead # Can't append extension for mktemp, so renaming instead
tmp_file_docs=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX") tmp_file_docs=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX")
mv "$tmp_file_docs" "$tmp_file_docs.tf" mv "$tmp_file_docs" "$tmp_file_docs.tf"
tmp_file_docs_tf="$tmp_file_docs.tf" tmp_file_docs_tf="$tmp_file_docs.tf"
awk -f "$terraform_docs_awk_file" ./*.tf > "$tmp_file_docs_tf" awk -f "$terraform_docs_awk_file" ./*.tf > "$tmp_file_docs_tf"
terraform-docs md "$args" "$tmp_file_docs_tf" > "$tmp_file" terraform-docs md "$args" "$tmp_file_docs_tf" > "$tmp_file"
rm -f "$tmp_file_docs_tf" rm -f "$tmp_file_docs_tf"
fi fi
# Replace content between markers with the placeholder - https://stackoverflow.com/questions/1212799/how-do-i-extract-lines-between-two-line-delimiters-in-perl#1212834 # Replace content between markers with the placeholder - https://stackoverflow.com/questions/1212799/how-do-i-extract-lines-between-two-line-delimiters-in-perl#1212834
perl -i -ne 'if (/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/../END OF PRE-COMMIT-TERRAFORM DOCS HOOK/) { print $_ if /BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/; print "I_WANT_TO_BE_REPLACED\n$_" if /END OF PRE-COMMIT-TERRAFORM DOCS HOOK/;} else { print $_ }' "$text_file" perl -i -ne 'if (/BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/../END OF PRE-COMMIT-TERRAFORM DOCS HOOK/) { print $_ if /BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK/; print "I_WANT_TO_BE_REPLACED\n$_" if /END OF PRE-COMMIT-TERRAFORM DOCS HOOK/;} else { print $_ }' "$text_file"
@ -122,7 +122,7 @@ terraform_docs() {
terraform_docs_awk() { terraform_docs_awk() {
readonly output_file=$1 readonly output_file=$1
cat <<"EOF" > "$output_file" cat << "EOF" > "$output_file"
# This script converts Terraform 0.12 variables/outputs to something suitable for `terraform-docs` # This script converts Terraform 0.12 variables/outputs to something suitable for `terraform-docs`
# As of terraform-docs v0.6.0, HCL2 is not supported. This script is a *dirty hack* to get around it. # As of terraform-docs v0.6.0, HCL2 is not supported. This script is a *dirty hack* to get around it.
# https://github.com/segmentio/terraform-docs/ # https://github.com/segmentio/terraform-docs/
@ -353,59 +353,73 @@ getopt() {
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case $1 in case $1 in
(-a|--alternative) -a | --alternative)
flags=a$flags ;; flags=a$flags
(-h|--help)
_getopt_help
return 2 # as does GNU getopt
;; ;;
(-l|--longoptions) -h | --help)
_getopt_help
return 2 # as does GNU getopt
;;
-l | --longoptions)
long="$long${long:+,}$2" long="$long${long:+,}$2"
shift ;; shift
;;
(-n|--name) -n | --name)
name=$2 name=$2
shift ;; shift
;;
(-o|--options) -o | --options)
short=$2 short=$2
have_short=true have_short=true
shift ;; shift
;;
(-q|--quiet) -q | --quiet)
flags=q$flags ;; flags=q$flags
;;
(-Q|--quiet-output) -Q | --quiet-output)
flags=Q$flags ;; flags=Q$flags
;;
(-s|--shell) -s | --shell)
case $2 in case $2 in
(sh|bash) sh | bash)
flags=${flags//t/} ;; flags=${flags//t/}
(csh|tcsh) ;;
flags=t$flags ;; csh | tcsh)
(*) flags=t$flags
;;
*)
echo 'getopt: unknown shell after -s or --shell argument' >&2 echo 'getopt: unknown shell after -s or --shell argument' >&2
echo "Try \`getopt --help' for more information." >&2 echo "Try \`getopt --help' for more information." >&2
return 2 ;; return 2
;;
esac esac
shift ;;
(-u|--unquoted)
flags=u$flags ;;
(-T|--test)
return 4 ;;
(-V|--version)
echo "pure-getopt 1.4.3"
return 0 ;;
(--)
shift shift
break ;; ;;
-u | --unquoted)
flags=u$flags
;;
-T | --test)
return 4
;;
-V | --version)
echo "pure-getopt 1.4.3"
return 0
;;
--)
shift
break
;;
esac esac
shift shift
@ -466,9 +480,9 @@ getopt() {
# for use with _getopt_resolve_abbrev # for use with _getopt_resolve_abbrev
declare -a longarr declare -a longarr
_getopt_split longarr "$long" _getopt_split longarr "$long"
longarr=( "${longarr[@]/#/--}" ) longarr=("${longarr[@]/#/--}")
longarr=( "${longarr[@]%:}" ) longarr=("${longarr[@]%:}")
longarr=( "${longarr[@]%:}" ) longarr=("${longarr[@]%:}")
# Parse and collect options and parameters # Parse and collect options and parameters
declare -a opts params declare -a opts params
@ -476,18 +490,19 @@ getopt() {
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case $1 in case $1 in
(--) --)
params=( "${params[@]}" "${@:2}" ) params=("${params[@]}" "${@:2}")
break ;; break
;;
(--*=*) --*=*)
o=${1%%=*} o=${1%%=*}
if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then
error=1 error=1
elif [[ ,"$long", == *,"${o#--}"::,* ]]; then elif [[ ,"$long", == *,"${o#--}"::,* ]]; then
opts=( "${opts[@]}" "$o" "${1#*=}" ) opts=("${opts[@]}" "$o" "${1#*=}")
elif [[ ,"$long", == *,"${o#--}":,* ]]; then elif [[ ,"$long", == *,"${o#--}":,* ]]; then
opts=( "${opts[@]}" "$o" "${1#*=}" ) opts=("${opts[@]}" "$o" "${1#*=}")
elif [[ ,"$long", == *,"${o#--}",* ]]; then elif [[ ,"$long", == *,"${o#--}",* ]]; then
if $alt_recycled; then o=${o#-}; fi if $alt_recycled; then o=${o#-}; fi
_getopt_err "$name: option '$o' doesn't allow an argument" _getopt_err "$name: option '$o' doesn't allow an argument"
@ -499,18 +514,18 @@ getopt() {
alt_recycled=false alt_recycled=false
;; ;;
(--?*) --?*)
o=$1 o=$1
if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then
error=1 error=1
elif [[ ,"$long", == *,"${o#--}",* ]]; then elif [[ ,"$long", == *,"${o#--}",* ]]; then
opts=( "${opts[@]}" "$o" ) opts=("${opts[@]}" "$o")
elif [[ ,"$long", == *,"${o#--}::",* ]]; then elif [[ ,"$long", == *,"${o#--}::",* ]]; then
opts=( "${opts[@]}" "$o" '' ) opts=("${opts[@]}" "$o" '')
elif [[ ,"$long", == *,"${o#--}:",* ]]; then elif [[ ,"$long", == *,"${o#--}:",* ]]; then
if [[ $# -ge 2 ]]; then if [[ $# -ge 2 ]]; then
shift shift
opts=( "${opts[@]}" "$o" "$1" ) opts=("${opts[@]}" "$o" "$1")
else else
if $alt_recycled; then o=${o#-}; fi if $alt_recycled; then o=${o#-}; fi
_getopt_err "$name: option '$o' requires an argument" _getopt_err "$name: option '$o' requires an argument"
@ -523,7 +538,7 @@ getopt() {
alt_recycled=false alt_recycled=false
;; ;;
(-*) -*)
if [[ $flags == *a* ]]; then if [[ $flags == *a* ]]; then
# Alternative parsing mode! # Alternative parsing mode!
# Try to handle as a long option if any of the following apply: # Try to handle as a long option if any of the following apply:
@ -533,28 +548,32 @@ getopt() {
# 4. There's a single letter and no short match # 4. There's a single letter and no short match
o=${1::2} # temp for testing #4 o=${1::2} # temp for testing #4
if [[ $1 == *=* || $1 == -?? || \ if [[ $1 == *=* || $1 == -?? || \
,$long, == *,"${1#-}"[:,]* || \ ,$long, == *,"${1#-}"[:,]* || \
,$short, != *,"${o#-}"[:,]* ]]; then ,$short, != *,"${o#-}"[:,]* ]]; then
o=$(_getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" 2>/dev/null) o=$(_getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" 2> /dev/null)
case $? in case $? in
(0) 0)
# Unambiguous match. Let the long options parser handle # Unambiguous match. Let the long options parser handle
# it, with a flag to get the right error message. # it, with a flag to get the right error message.
set -- "-$1" "${@:2}" set -- "-$1" "${@:2}"
alt_recycled=true alt_recycled=true
continue ;; continue
(1) ;;
1)
# Ambiguous match, generate error and continue. # Ambiguous match, generate error and continue.
_getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" >/dev/null _getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" > /dev/null
error=1 error=1
shift shift
continue ;; continue
(2) ;;
2)
# No match, fall through to single-character check. # No match, fall through to single-character check.
true ;; true
(*) ;;
*)
echo "getopt: assertion failed (3)" >&2 echo "getopt: assertion failed (3)" >&2
return 3 ;; return 3
;;
esac esac
fi fi
fi fi
@ -562,22 +581,22 @@ getopt() {
o=${1::2} o=${1::2}
if [[ "$short" == *"${o#-}"::* ]]; then if [[ "$short" == *"${o#-}"::* ]]; then
if [[ ${#1} -gt 2 ]]; then if [[ ${#1} -gt 2 ]]; then
opts=( "${opts[@]}" "$o" "${1:2}" ) opts=("${opts[@]}" "$o" "${1:2}")
else else
opts=( "${opts[@]}" "$o" '' ) opts=("${opts[@]}" "$o" '')
fi fi
elif [[ "$short" == *"${o#-}":* ]]; then elif [[ "$short" == *"${o#-}":* ]]; then
if [[ ${#1} -gt 2 ]]; then if [[ ${#1} -gt 2 ]]; then
opts=( "${opts[@]}" "$o" "${1:2}" ) opts=("${opts[@]}" "$o" "${1:2}")
elif [[ $# -ge 2 ]]; then elif [[ $# -ge 2 ]]; then
shift shift
opts=( "${opts[@]}" "$o" "$1" ) opts=("${opts[@]}" "$o" "$1")
else else
_getopt_err "$name: option requires an argument -- '${o#-}'" _getopt_err "$name: option requires an argument -- '${o#-}'"
error=1 error=1
fi fi
elif [[ "$short" == *"${o#-}"* ]]; then elif [[ "$short" == *"${o#-}"* ]]; then
opts=( "${opts[@]}" "$o" ) opts=("${opts[@]}" "$o")
if [[ ${#1} -gt 2 ]]; then if [[ ${#1} -gt 2 ]]; then
set -- "$o" "-${1:2}" "${@:2}" set -- "$o" "-${1:2}" "${@:2}"
fi fi
@ -594,26 +613,28 @@ getopt() {
fi fi
fi fi
error=1 error=1
fi ;; fi
;;
(*) *)
# GNU getopt in-place mode (leading dash on short options) # GNU getopt in-place mode (leading dash on short options)
# overrides POSIXLY_CORRECT # overrides POSIXLY_CORRECT
if [[ $flags == *i* ]]; then if [[ $flags == *i* ]]; then
opts=( "${opts[@]}" "$1" ) opts=("${opts[@]}" "$1")
elif [[ $flags == *p* ]]; then elif [[ $flags == *p* ]]; then
params=( "${params[@]}" "$@" ) params=("${params[@]}" "$@")
break break
else else
params=( "${params[@]}" "$1" ) params=("${params[@]}" "$1")
fi fi
;;
esac esac
shift shift
done done
if [[ $flags == *Q* ]]; then if [[ $flags == *Q* ]]; then
true # generate no output true # generate no output
else else
echo -n ' ' echo -n ' '
if [[ $flags == *[cu]* ]]; then if [[ $flags == *[cu]* ]]; then
@ -650,33 +671,39 @@ getopt() {
for a; do for a; do
if [[ $q == "$a" ]]; then if [[ $q == "$a" ]]; then
# Exact match. Squash any other partial matches. # Exact match. Squash any other partial matches.
matches=( "$a" ) matches=("$a")
break break
elif [[ $flags == *a* && $q == -[^-]* && $a == -"$q" ]]; then elif [[ $flags == *a* && $q == -[^-]* && $a == -"$q" ]]; then
# Exact alternative match. Squash any other partial matches. # Exact alternative match. Squash any other partial matches.
matches=( "$a" ) matches=("$a")
break break
elif [[ $a == "$q"* ]]; then elif [[ $a == "$q"* ]]; then
# Abbreviated match. # Abbreviated match.
matches=( "${matches[@]}" "$a" ) matches=("${matches[@]}" "$a")
elif [[ $flags == *a* && $q == -[^-]* && $a == -"$q"* ]]; then elif [[ $flags == *a* && $q == -[^-]* && $a == -"$q"* ]]; then
# Abbreviated alternative match. # Abbreviated alternative match.
matches=( "${matches[@]}" "${a#-}" ) matches=("${matches[@]}" "${a#-}")
fi fi
done done
case ${#matches[@]} in case ${#matches[@]} in
(0) 0)
[[ $flags == *q* ]] || \ [[ $flags == *q* ]] ||
printf "$name: unrecognized option %s\\n" >&2 \ printf "$name: unrecognized option %s\\n" \
"$(_getopt_quote "$q")" "$(_getopt_quote "$q")" >&2
return 2 ;;
(1) return 2
printf '%s' "${matches[0]}"; return 0 ;; ;;
(*) 1)
[[ $flags == *q* ]] || \ printf '%s' "${matches[0]}"
printf "$name: option %s is ambiguous; possibilities: %s\\n" >&2 \ return 0
"$(_getopt_quote "$q")" "$(_getopt_quote "${matches[@]}")" ;;
return 1 ;; *)
[[ $flags == *q* ]] ||
printf "$name: option %s is ambiguous; possibilities: %s\\n" \
"$(_getopt_quote "$q")" "$(_getopt_quote "${matches[@]}")" >&2
return 1
;;
esac esac
} }
@ -701,15 +728,18 @@ getopt() {
declare s i c space declare s i c space
for s; do for s; do
echo -n "$space'" echo -n "$space'"
for ((i=0; i<${#s}; i++)); do for ((i = 0; i < ${#s}; i++)); do
c=${s:i:1} c=${s:i:1}
case $c in case $c in
(\\|\'|!) \\ | \' | !)
echo -n "'\\$c'" ;; echo -n "'\\$c'"
($'\n') ;;
echo -n "\\$c" ;; $'\n')
(*) echo -n "\\$c"
echo -n "$c" ;; ;;
*)
echo -n "$c"
;;
esac esac
done done
echo -n \' echo -n \'
@ -718,7 +748,7 @@ getopt() {
} }
_getopt_help() { _getopt_help() {
cat <<-EOT >&2 cat <<- EOT >&2
Usage: Usage:
getopt <optstring> <parameters> getopt <optstring> <parameters>

View file

@ -11,12 +11,12 @@ main() {
for argv; do for argv; do
case $argv in case $argv in
(-a|--args) -a | --args)
shift shift
args="$1" args="$1"
shift shift
;; ;;
(--) --)
shift shift
files="$@" files="$@"
break break
@ -120,59 +120,73 @@ getopt() {
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case $1 in case $1 in
(-a|--alternative) -a | --alternative)
flags=a$flags ;; flags=a$flags
(-h|--help)
_getopt_help
return 2 # as does GNU getopt
;; ;;
(-l|--longoptions) -h | --help)
_getopt_help
return 2 # as does GNU getopt
;;
-l | --longoptions)
long="$long${long:+,}$2" long="$long${long:+,}$2"
shift ;; shift
;;
(-n|--name) -n | --name)
name=$2 name=$2
shift ;; shift
;;
(-o|--options) -o | --options)
short=$2 short=$2
have_short=true have_short=true
shift ;; shift
;;
(-q|--quiet) -q | --quiet)
flags=q$flags ;; flags=q$flags
;;
(-Q|--quiet-output) -Q | --quiet-output)
flags=Q$flags ;; flags=Q$flags
;;
(-s|--shell) -s | --shell)
case $2 in case $2 in
(sh|bash) sh | bash)
flags=${flags//t/} ;; flags=${flags//t/}
(csh|tcsh) ;;
flags=t$flags ;; csh | tcsh)
(*) flags=t$flags
;;
*)
echo 'getopt: unknown shell after -s or --shell argument' >&2 echo 'getopt: unknown shell after -s or --shell argument' >&2
echo "Try \`getopt --help' for more information." >&2 echo "Try \`getopt --help' for more information." >&2
return 2 ;; return 2
;;
esac esac
shift ;;
(-u|--unquoted)
flags=u$flags ;;
(-T|--test)
return 4 ;;
(-V|--version)
echo "pure-getopt 1.4.3"
return 0 ;;
(--)
shift shift
break ;; ;;
-u | --unquoted)
flags=u$flags
;;
-T | --test)
return 4
;;
-V | --version)
echo "pure-getopt 1.4.3"
return 0
;;
--)
shift
break
;;
esac esac
shift shift
@ -233,9 +247,9 @@ getopt() {
# for use with _getopt_resolve_abbrev # for use with _getopt_resolve_abbrev
declare -a longarr declare -a longarr
_getopt_split longarr "$long" _getopt_split longarr "$long"
longarr=( "${longarr[@]/#/--}" ) longarr=("${longarr[@]/#/--}")
longarr=( "${longarr[@]%:}" ) longarr=("${longarr[@]%:}")
longarr=( "${longarr[@]%:}" ) longarr=("${longarr[@]%:}")
# Parse and collect options and parameters # Parse and collect options and parameters
declare -a opts params declare -a opts params
@ -243,18 +257,19 @@ getopt() {
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case $1 in case $1 in
(--) --)
params=( "${params[@]}" "${@:2}" ) params=("${params[@]}" "${@:2}")
break ;; break
;;
(--*=*) --*=*)
o=${1%%=*} o=${1%%=*}
if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then
error=1 error=1
elif [[ ,"$long", == *,"${o#--}"::,* ]]; then elif [[ ,"$long", == *,"${o#--}"::,* ]]; then
opts=( "${opts[@]}" "$o" "${1#*=}" ) opts=("${opts[@]}" "$o" "${1#*=}")
elif [[ ,"$long", == *,"${o#--}":,* ]]; then elif [[ ,"$long", == *,"${o#--}":,* ]]; then
opts=( "${opts[@]}" "$o" "${1#*=}" ) opts=("${opts[@]}" "$o" "${1#*=}")
elif [[ ,"$long", == *,"${o#--}",* ]]; then elif [[ ,"$long", == *,"${o#--}",* ]]; then
if $alt_recycled; then o=${o#-}; fi if $alt_recycled; then o=${o#-}; fi
_getopt_err "$name: option '$o' doesn't allow an argument" _getopt_err "$name: option '$o' doesn't allow an argument"
@ -266,18 +281,18 @@ getopt() {
alt_recycled=false alt_recycled=false
;; ;;
(--?*) --?*)
o=$1 o=$1
if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then
error=1 error=1
elif [[ ,"$long", == *,"${o#--}",* ]]; then elif [[ ,"$long", == *,"${o#--}",* ]]; then
opts=( "${opts[@]}" "$o" ) opts=("${opts[@]}" "$o")
elif [[ ,"$long", == *,"${o#--}::",* ]]; then elif [[ ,"$long", == *,"${o#--}::",* ]]; then
opts=( "${opts[@]}" "$o" '' ) opts=("${opts[@]}" "$o" '')
elif [[ ,"$long", == *,"${o#--}:",* ]]; then elif [[ ,"$long", == *,"${o#--}:",* ]]; then
if [[ $# -ge 2 ]]; then if [[ $# -ge 2 ]]; then
shift shift
opts=( "${opts[@]}" "$o" "$1" ) opts=("${opts[@]}" "$o" "$1")
else else
if $alt_recycled; then o=${o#-}; fi if $alt_recycled; then o=${o#-}; fi
_getopt_err "$name: option '$o' requires an argument" _getopt_err "$name: option '$o' requires an argument"
@ -290,7 +305,7 @@ getopt() {
alt_recycled=false alt_recycled=false
;; ;;
(-*) -*)
if [[ $flags == *a* ]]; then if [[ $flags == *a* ]]; then
# Alternative parsing mode! # Alternative parsing mode!
# Try to handle as a long option if any of the following apply: # Try to handle as a long option if any of the following apply:
@ -300,28 +315,32 @@ getopt() {
# 4. There's a single letter and no short match # 4. There's a single letter and no short match
o=${1::2} # temp for testing #4 o=${1::2} # temp for testing #4
if [[ $1 == *=* || $1 == -?? || \ if [[ $1 == *=* || $1 == -?? || \
,$long, == *,"${1#-}"[:,]* || \ ,$long, == *,"${1#-}"[:,]* || \
,$short, != *,"${o#-}"[:,]* ]]; then ,$short, != *,"${o#-}"[:,]* ]]; then
o=$(_getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" 2>/dev/null) o=$(_getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" 2> /dev/null)
case $? in case $? in
(0) 0)
# Unambiguous match. Let the long options parser handle # Unambiguous match. Let the long options parser handle
# it, with a flag to get the right error message. # it, with a flag to get the right error message.
set -- "-$1" "${@:2}" set -- "-$1" "${@:2}"
alt_recycled=true alt_recycled=true
continue ;; continue
(1) ;;
1)
# Ambiguous match, generate error and continue. # Ambiguous match, generate error and continue.
_getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" >/dev/null _getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" > /dev/null
error=1 error=1
shift shift
continue ;; continue
(2) ;;
2)
# No match, fall through to single-character check. # No match, fall through to single-character check.
true ;; true
(*) ;;
*)
echo "getopt: assertion failed (3)" >&2 echo "getopt: assertion failed (3)" >&2
return 3 ;; return 3
;;
esac esac
fi fi
fi fi
@ -329,22 +348,22 @@ getopt() {
o=${1::2} o=${1::2}
if [[ "$short" == *"${o#-}"::* ]]; then if [[ "$short" == *"${o#-}"::* ]]; then
if [[ ${#1} -gt 2 ]]; then if [[ ${#1} -gt 2 ]]; then
opts=( "${opts[@]}" "$o" "${1:2}" ) opts=("${opts[@]}" "$o" "${1:2}")
else else
opts=( "${opts[@]}" "$o" '' ) opts=("${opts[@]}" "$o" '')
fi fi
elif [[ "$short" == *"${o#-}":* ]]; then elif [[ "$short" == *"${o#-}":* ]]; then
if [[ ${#1} -gt 2 ]]; then if [[ ${#1} -gt 2 ]]; then
opts=( "${opts[@]}" "$o" "${1:2}" ) opts=("${opts[@]}" "$o" "${1:2}")
elif [[ $# -ge 2 ]]; then elif [[ $# -ge 2 ]]; then
shift shift
opts=( "${opts[@]}" "$o" "$1" ) opts=("${opts[@]}" "$o" "$1")
else else
_getopt_err "$name: option requires an argument -- '${o#-}'" _getopt_err "$name: option requires an argument -- '${o#-}'"
error=1 error=1
fi fi
elif [[ "$short" == *"${o#-}"* ]]; then elif [[ "$short" == *"${o#-}"* ]]; then
opts=( "${opts[@]}" "$o" ) opts=("${opts[@]}" "$o")
if [[ ${#1} -gt 2 ]]; then if [[ ${#1} -gt 2 ]]; then
set -- "$o" "-${1:2}" "${@:2}" set -- "$o" "-${1:2}" "${@:2}"
fi fi
@ -361,26 +380,28 @@ getopt() {
fi fi
fi fi
error=1 error=1
fi ;; fi
;;
(*) *)
# GNU getopt in-place mode (leading dash on short options) # GNU getopt in-place mode (leading dash on short options)
# overrides POSIXLY_CORRECT # overrides POSIXLY_CORRECT
if [[ $flags == *i* ]]; then if [[ $flags == *i* ]]; then
opts=( "${opts[@]}" "$1" ) opts=("${opts[@]}" "$1")
elif [[ $flags == *p* ]]; then elif [[ $flags == *p* ]]; then
params=( "${params[@]}" "$@" ) params=("${params[@]}" "$@")
break break
else else
params=( "${params[@]}" "$1" ) params=("${params[@]}" "$1")
fi fi
;;
esac esac
shift shift
done done
if [[ $flags == *Q* ]]; then if [[ $flags == *Q* ]]; then
true # generate no output true # generate no output
else else
echo -n ' ' echo -n ' '
if [[ $flags == *[cu]* ]]; then if [[ $flags == *[cu]* ]]; then
@ -417,33 +438,39 @@ getopt() {
for a; do for a; do
if [[ $q == "$a" ]]; then if [[ $q == "$a" ]]; then
# Exact match. Squash any other partial matches. # Exact match. Squash any other partial matches.
matches=( "$a" ) matches=("$a")
break break
elif [[ $flags == *a* && $q == -[^-]* && $a == -"$q" ]]; then elif [[ $flags == *a* && $q == -[^-]* && $a == -"$q" ]]; then
# Exact alternative match. Squash any other partial matches. # Exact alternative match. Squash any other partial matches.
matches=( "$a" ) matches=("$a")
break break
elif [[ $a == "$q"* ]]; then elif [[ $a == "$q"* ]]; then
# Abbreviated match. # Abbreviated match.
matches=( "${matches[@]}" "$a" ) matches=("${matches[@]}" "$a")
elif [[ $flags == *a* && $q == -[^-]* && $a == -"$q"* ]]; then elif [[ $flags == *a* && $q == -[^-]* && $a == -"$q"* ]]; then
# Abbreviated alternative match. # Abbreviated alternative match.
matches=( "${matches[@]}" "${a#-}" ) matches=("${matches[@]}" "${a#-}")
fi fi
done done
case ${#matches[@]} in case ${#matches[@]} in
(0) 0)
[[ $flags == *q* ]] || \ [[ $flags == *q* ]] ||
printf "$name: unrecognized option %s\\n" >&2 \ printf "$name: unrecognized option %s\\n" \
"$(_getopt_quote "$q")" "$(_getopt_quote "$q")" >&2
return 2 ;;
(1) return 2
printf '%s' "${matches[0]}"; return 0 ;; ;;
(*) 1)
[[ $flags == *q* ]] || \ printf '%s' "${matches[0]}"
printf "$name: option %s is ambiguous; possibilities: %s\\n" >&2 \ return 0
"$(_getopt_quote "$q")" "$(_getopt_quote "${matches[@]}")" ;;
return 1 ;; *)
[[ $flags == *q* ]] ||
printf "$name: option %s is ambiguous; possibilities: %s\\n" \
"$(_getopt_quote "$q")" "$(_getopt_quote "${matches[@]}")" >&2
return 1
;;
esac esac
} }
@ -468,15 +495,18 @@ getopt() {
declare s i c space declare s i c space
for s; do for s; do
echo -n "$space'" echo -n "$space'"
for ((i=0; i<${#s}; i++)); do for ((i = 0; i < ${#s}; i++)); do
c=${s:i:1} c=${s:i:1}
case $c in case $c in
(\\|\'|!) \\ | \' | !)
echo -n "'\\$c'" ;; echo -n "'\\$c'"
($'\n') ;;
echo -n "\\$c" ;; $'\n')
(*) echo -n "\\$c"
echo -n "$c" ;; ;;
*)
echo -n "$c"
;;
esac esac
done done
echo -n \' echo -n \'
@ -485,7 +515,7 @@ getopt() {
} }
_getopt_help() { _getopt_help() {
cat <<-EOT >&2 cat <<- EOT >&2
Usage: Usage:
getopt <optstring> <parameters> getopt <optstring> <parameters>
getopt [options] [--] <optstring> <parameters> getopt [options] [--] <optstring> <parameters>

View file

@ -9,13 +9,13 @@ for file_with_path in "$@"; do
file_with_path="${file_with_path// /__REPLACED__SPACE__}" file_with_path="${file_with_path// /__REPLACED__SPACE__}"
paths[index]=$(dirname "$file_with_path") paths[index]=$(dirname "$file_with_path")
(( "index+=1" )) (("index+=1"))
done done
for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
path_uniq="${path_uniq//__REPLACED__SPACE__/ }" path_uniq="${path_uniq//__REPLACED__SPACE__/ }"
if [[ -n "$(find . -maxdepth 1 -name '*.tf' -print -quit)" ]] ; then if [[ -n "$(find . -maxdepth 1 -name '*.tf' -print -quit)" ]]; then
if ! terraform validate $path_uniq; then if ! terraform validate $path_uniq; then
error=1 error=1
echo echo
@ -25,6 +25,6 @@ for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
fi fi
done done
if [[ "${error}" -ne 0 ]] ; then if [[ "${error}" -ne 0 ]]; then
exit 1 exit 1
fi fi