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-merge-conflict
- id: check-executables-have-shebangs
#- repo: git://github.com/jumanjihouse/pre-commit-hooks
# rev: 1.11.2
# hooks:
# - id: shellcheck
# - id: shfmt
- repo: git://github.com/jumanjihouse/pre-commit-hooks
rev: 1.11.2
hooks:
- id: shfmt
args: ['-l', '-i', '2', '-ci', '-sr', '-w']

View file

@ -12,12 +12,12 @@ main() {
for argv; do
case $argv in
(-a|--args)
-a | --args)
shift
args="$1"
shift
;;
(--)
--)
shift
files="$@"
break
@ -353,59 +353,73 @@ getopt() {
while [[ $# -gt 0 ]]; do
case $1 in
(-a|--alternative)
flags=a$flags ;;
-a | --alternative)
flags=a$flags
;;
(-h|--help)
-h | --help)
_getopt_help
return 2 # as does GNU getopt
;;
(-l|--longoptions)
-l | --longoptions)
long="$long${long:+,}$2"
shift ;;
shift
;;
(-n|--name)
-n | --name)
name=$2
shift ;;
shift
;;
(-o|--options)
-o | --options)
short=$2
have_short=true
shift ;;
shift
;;
(-q|--quiet)
flags=q$flags ;;
-q | --quiet)
flags=q$flags
;;
(-Q|--quiet-output)
flags=Q$flags ;;
-Q | --quiet-output)
flags=Q$flags
;;
(-s|--shell)
-s | --shell)
case $2 in
(sh|bash)
flags=${flags//t/} ;;
(csh|tcsh)
flags=t$flags ;;
(*)
sh | bash)
flags=${flags//t/}
;;
csh | tcsh)
flags=t$flags
;;
*)
echo 'getopt: unknown shell after -s or --shell argument' >&2
echo "Try \`getopt --help' for more information." >&2
return 2 ;;
return 2
;;
esac
shift ;;
(-u|--unquoted)
flags=u$flags ;;
(-T|--test)
return 4 ;;
(-V|--version)
echo "pure-getopt 1.4.3"
return 0 ;;
(--)
shift
break ;;
;;
-u | --unquoted)
flags=u$flags
;;
-T | --test)
return 4
;;
-V | --version)
echo "pure-getopt 1.4.3"
return 0
;;
--)
shift
break
;;
esac
shift
@ -476,11 +490,12 @@ getopt() {
while [[ $# -gt 0 ]]; do
case $1 in
(--)
--)
params=("${params[@]}" "${@:2}")
break ;;
break
;;
(--*=*)
--*=*)
o=${1%%=*}
if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then
error=1
@ -499,7 +514,7 @@ getopt() {
alt_recycled=false
;;
(--?*)
--?*)
o=$1
if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then
error=1
@ -523,7 +538,7 @@ getopt() {
alt_recycled=false
;;
(-*)
-*)
if [[ $flags == *a* ]]; then
# Alternative parsing mode!
# Try to handle as a long option if any of the following apply:
@ -537,24 +552,28 @@ getopt() {
,$short, != *,"${o#-}"[:,]* ]]; then
o=$(_getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" 2> /dev/null)
case $? in
(0)
0)
# Unambiguous match. Let the long options parser handle
# it, with a flag to get the right error message.
set -- "-$1" "${@:2}"
alt_recycled=true
continue ;;
(1)
continue
;;
1)
# Ambiguous match, generate error and continue.
_getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" > /dev/null
error=1
shift
continue ;;
(2)
continue
;;
2)
# No match, fall through to single-character check.
true ;;
(*)
true
;;
*)
echo "getopt: assertion failed (3)" >&2
return 3 ;;
return 3
;;
esac
fi
fi
@ -594,9 +613,10 @@ getopt() {
fi
fi
error=1
fi ;;
fi
;;
(*)
*)
# GNU getopt in-place mode (leading dash on short options)
# overrides POSIXLY_CORRECT
if [[ $flags == *i* ]]; then
@ -607,6 +627,7 @@ getopt() {
else
params=("${params[@]}" "$1")
fi
;;
esac
shift
@ -665,18 +686,24 @@ getopt() {
fi
done
case ${#matches[@]} in
(0)
[[ $flags == *q* ]] || \
printf "$name: unrecognized option %s\\n" >&2 \
"$(_getopt_quote "$q")"
return 2 ;;
(1)
printf '%s' "${matches[0]}"; return 0 ;;
(*)
[[ $flags == *q* ]] || \
printf "$name: option %s is ambiguous; possibilities: %s\\n" >&2 \
"$(_getopt_quote "$q")" "$(_getopt_quote "${matches[@]}")"
return 1 ;;
0)
[[ $flags == *q* ]] ||
printf "$name: unrecognized option %s\\n" \
"$(_getopt_quote "$q")" >&2
return 2
;;
1)
printf '%s' "${matches[0]}"
return 0
;;
*)
[[ $flags == *q* ]] ||
printf "$name: option %s is ambiguous; possibilities: %s\\n" \
"$(_getopt_quote "$q")" "$(_getopt_quote "${matches[@]}")" >&2
return 1
;;
esac
}
@ -704,12 +731,15 @@ getopt() {
for ((i = 0; i < ${#s}; i++)); do
c=${s:i:1}
case $c in
(\\|\'|!)
echo -n "'\\$c'" ;;
($'\n')
echo -n "\\$c" ;;
(*)
echo -n "$c" ;;
\\ | \' | !)
echo -n "'\\$c'"
;;
$'\n')
echo -n "\\$c"
;;
*)
echo -n "$c"
;;
esac
done
echo -n \'

View file

@ -11,12 +11,12 @@ main() {
for argv; do
case $argv in
(-a|--args)
-a | --args)
shift
args="$1"
shift
;;
(--)
--)
shift
files="$@"
break
@ -120,59 +120,73 @@ getopt() {
while [[ $# -gt 0 ]]; do
case $1 in
(-a|--alternative)
flags=a$flags ;;
-a | --alternative)
flags=a$flags
;;
(-h|--help)
-h | --help)
_getopt_help
return 2 # as does GNU getopt
;;
(-l|--longoptions)
-l | --longoptions)
long="$long${long:+,}$2"
shift ;;
shift
;;
(-n|--name)
-n | --name)
name=$2
shift ;;
shift
;;
(-o|--options)
-o | --options)
short=$2
have_short=true
shift ;;
shift
;;
(-q|--quiet)
flags=q$flags ;;
-q | --quiet)
flags=q$flags
;;
(-Q|--quiet-output)
flags=Q$flags ;;
-Q | --quiet-output)
flags=Q$flags
;;
(-s|--shell)
-s | --shell)
case $2 in
(sh|bash)
flags=${flags//t/} ;;
(csh|tcsh)
flags=t$flags ;;
(*)
sh | bash)
flags=${flags//t/}
;;
csh | tcsh)
flags=t$flags
;;
*)
echo 'getopt: unknown shell after -s or --shell argument' >&2
echo "Try \`getopt --help' for more information." >&2
return 2 ;;
return 2
;;
esac
shift ;;
(-u|--unquoted)
flags=u$flags ;;
(-T|--test)
return 4 ;;
(-V|--version)
echo "pure-getopt 1.4.3"
return 0 ;;
(--)
shift
break ;;
;;
-u | --unquoted)
flags=u$flags
;;
-T | --test)
return 4
;;
-V | --version)
echo "pure-getopt 1.4.3"
return 0
;;
--)
shift
break
;;
esac
shift
@ -243,11 +257,12 @@ getopt() {
while [[ $# -gt 0 ]]; do
case $1 in
(--)
--)
params=("${params[@]}" "${@:2}")
break ;;
break
;;
(--*=*)
--*=*)
o=${1%%=*}
if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then
error=1
@ -266,7 +281,7 @@ getopt() {
alt_recycled=false
;;
(--?*)
--?*)
o=$1
if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then
error=1
@ -290,7 +305,7 @@ getopt() {
alt_recycled=false
;;
(-*)
-*)
if [[ $flags == *a* ]]; then
# Alternative parsing mode!
# Try to handle as a long option if any of the following apply:
@ -304,24 +319,28 @@ getopt() {
,$short, != *,"${o#-}"[:,]* ]]; then
o=$(_getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" 2> /dev/null)
case $? in
(0)
0)
# Unambiguous match. Let the long options parser handle
# it, with a flag to get the right error message.
set -- "-$1" "${@:2}"
alt_recycled=true
continue ;;
(1)
continue
;;
1)
# Ambiguous match, generate error and continue.
_getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" > /dev/null
error=1
shift
continue ;;
(2)
continue
;;
2)
# No match, fall through to single-character check.
true ;;
(*)
true
;;
*)
echo "getopt: assertion failed (3)" >&2
return 3 ;;
return 3
;;
esac
fi
fi
@ -361,9 +380,10 @@ getopt() {
fi
fi
error=1
fi ;;
fi
;;
(*)
*)
# GNU getopt in-place mode (leading dash on short options)
# overrides POSIXLY_CORRECT
if [[ $flags == *i* ]]; then
@ -374,6 +394,7 @@ getopt() {
else
params=("${params[@]}" "$1")
fi
;;
esac
shift
@ -432,18 +453,24 @@ getopt() {
fi
done
case ${#matches[@]} in
(0)
[[ $flags == *q* ]] || \
printf "$name: unrecognized option %s\\n" >&2 \
"$(_getopt_quote "$q")"
return 2 ;;
(1)
printf '%s' "${matches[0]}"; return 0 ;;
(*)
[[ $flags == *q* ]] || \
printf "$name: option %s is ambiguous; possibilities: %s\\n" >&2 \
"$(_getopt_quote "$q")" "$(_getopt_quote "${matches[@]}")"
return 1 ;;
0)
[[ $flags == *q* ]] ||
printf "$name: unrecognized option %s\\n" \
"$(_getopt_quote "$q")" >&2
return 2
;;
1)
printf '%s' "${matches[0]}"
return 0
;;
*)
[[ $flags == *q* ]] ||
printf "$name: option %s is ambiguous; possibilities: %s\\n" \
"$(_getopt_quote "$q")" "$(_getopt_quote "${matches[@]}")" >&2
return 1
;;
esac
}
@ -471,12 +498,15 @@ getopt() {
for ((i = 0; i < ${#s}; i++)); do
c=${s:i:1}
case $c in
(\\|\'|!)
echo -n "'\\$c'" ;;
($'\n')
echo -n "\\$c" ;;
(*)
echo -n "$c" ;;
\\ | \' | !)
echo -n "'\\$c'"
;;
$'\n')
echo -n "\\$c"
;;
*)
echo -n "$c"
;;
esac
done
echo -n \'