mirror of
https://github.com/kvz/bash3boilerplate.git
synced 2024-12-19 22:57:51 +00:00
extend acceptance test to check for code style (#81)
* extend acceptance test to check for code style * cleanup following own style guide
This commit is contained in:
parent
ea6bbd058f
commit
47be547cfa
@ -36,7 +36,7 @@ function ini_val() {
|
||||
|
||||
# Split on . for section. However, section is optional
|
||||
IFS='.' read -r section key <<< "${sectionkey}"
|
||||
if [ -z "${key}" ]; then
|
||||
if [[ ! "${key}" ]]; then
|
||||
key="${section}"
|
||||
section=""
|
||||
fi
|
||||
@ -44,15 +44,15 @@ function ini_val() {
|
||||
local current
|
||||
current=$(awk -F "${delim}" "/^${key}${delim}/ {for (i=2; i<NF; i++) printf \$i \" \"; print \$NF}" "${file}")
|
||||
|
||||
if [ -z "${val}" ]; then
|
||||
if [[ ! "${val}" ]]; then
|
||||
# get a value
|
||||
echo "${current}"
|
||||
else
|
||||
# set a value
|
||||
if [ -z "${current}" ]; then
|
||||
if [[ ! "${current}" ]]; then
|
||||
# doesn't exist yet, add
|
||||
|
||||
if [ -z "${section}" ]; then
|
||||
if [[ ! "${section}" ]]; then
|
||||
# no section was given, add to bottom of file
|
||||
echo "${key}${delim}${val}" >> "${file}"
|
||||
else
|
||||
@ -70,7 +70,7 @@ function ini_val() {
|
||||
fi
|
||||
}
|
||||
|
||||
if [ "${BASH_SOURCE[0]}" != "${0}" ]; then
|
||||
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
|
||||
export -f ini_val
|
||||
else
|
||||
ini_val "${@}"
|
||||
|
@ -53,13 +53,13 @@ function megamount () {
|
||||
|
||||
(umount -lf "${target}" || umount -f "${target}") > /dev/null 2>&1 || true
|
||||
mkdir -p "${target}"
|
||||
if [ "${proto}" = "smb://" ]; then
|
||||
if [[ "${proto}" = "smb://" ]]; then
|
||||
mount -t cifs --verbose -o "username=${user},password=${pass},hard" "//${host}/${path}" "${target}"
|
||||
elif [ "${proto}" = "afp://" ]; then
|
||||
elif [[ "${proto}" = "afp://" ]]; then
|
||||
# start syslog-ng
|
||||
# afpfsd || echo "Unable to run afpfsd. Does /dev/log exist?" && exit 1
|
||||
mount_afp "${url}" "${target}"
|
||||
elif [ "${proto}" = "nfs://" ]; then
|
||||
elif [[ "${proto}" = "nfs://" ]]; then
|
||||
mount -t nfs --verbose -o "vers=3,nolock,soft,intr,rsize=32768,wsize=32768" "${host}:/${path}" "${target}"
|
||||
else
|
||||
echo "ERR: Unknown protocol: '${proto}'"
|
||||
@ -70,7 +70,7 @@ function megamount () {
|
||||
ls -al "${target}/"
|
||||
}
|
||||
|
||||
if [ "${BASH_SOURCE[0]}" != "${0}" ]; then
|
||||
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
|
||||
export -f megamount
|
||||
else
|
||||
megamount "${@}"
|
||||
|
@ -40,26 +40,26 @@ function parse_url() {
|
||||
local port
|
||||
local path
|
||||
|
||||
proto="$(echo "$parse" | grep :// | sed -e's,^\(.*://\).*,\1,g')"
|
||||
url="${parse/$proto/}"
|
||||
userpass="$(echo "$url" | grep @ | cut -d@ -f1)"
|
||||
user="$(echo "$userpass" | grep : | cut -d: -f1)"
|
||||
pass="$(echo "$userpass" | grep : | cut -d: -f2)"
|
||||
hostport="$(echo "${url/$userpass@/}" | cut -d/ -f1)"
|
||||
host="$(echo "$hostport" | grep : | cut -d: -f1)"
|
||||
port="$(echo "$hostport" | grep : | cut -d: -f2)"
|
||||
path="$(echo "$url" | grep / | cut -d/ -f2-)"
|
||||
proto="$(echo "${parse}" | grep :// | sed -e's,^\(.*://\).*,\1,g')"
|
||||
url="${parse/${proto}/}"
|
||||
userpass="$(echo "${url}" | grep @ | cut -d@ -f1)"
|
||||
user="$(echo "${userpass}" | grep : | cut -d: -f1)"
|
||||
pass="$(echo "${userpass}" | grep : | cut -d: -f2)"
|
||||
hostport="$(echo "${url/${userpass}@/}" | cut -d/ -f1)"
|
||||
host="$(echo "${hostport}" | grep : | cut -d: -f1)"
|
||||
port="$(echo "${hostport}" | grep : | cut -d: -f2)"
|
||||
path="$(echo "${url}" | grep / | cut -d/ -f2-)"
|
||||
|
||||
[ -z "${user}" ] && user="${userpass}"
|
||||
[ -z "${host}" ] && host="${hostport}"
|
||||
if [ -z "${port}" ]; then
|
||||
[ "${proto}" = "http://" ] && port="80"
|
||||
[ "${proto}" = "https://" ] && port="443"
|
||||
[ "${proto}" = "mysql://" ] && port="3306"
|
||||
[ "${proto}" = "redis://" ] && port="6379"
|
||||
[[ ! "${user}" ]] && user="${userpass}"
|
||||
[[ ! "${host}" ]] && host="${hostport}"
|
||||
if [[ ! "${port}" ]]; then
|
||||
[[ "${proto}" = "http://" ]] && port="80"
|
||||
[[ "${proto}" = "https://" ]] && port="443"
|
||||
[[ "${proto}" = "mysql://" ]] && port="3306"
|
||||
[[ "${proto}" = "redis://" ]] && port="6379"
|
||||
fi
|
||||
|
||||
if [ -n "${need}" ]; then
|
||||
if [[ "${need}" ]]; then
|
||||
echo "${!need}"
|
||||
else
|
||||
echo ""
|
||||
@ -76,7 +76,7 @@ function parse_url() {
|
||||
fi
|
||||
}
|
||||
|
||||
if [ "${BASH_SOURCE[0]}" != "${0}" ]; then
|
||||
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
|
||||
export -f parse_url
|
||||
else
|
||||
parse_url "${@}"
|
||||
|
@ -30,11 +30,11 @@ function templater() {
|
||||
templateSrc="${1:-}"
|
||||
templateDst="${2:-}"
|
||||
|
||||
if [ ! -f "${templateSrc}" ]; then
|
||||
if [[ ! -f "${templateSrc}" ]]; then
|
||||
echo "ERROR: Template source '${templateSrc}' needs to exist"
|
||||
exit 1
|
||||
fi
|
||||
if [ ! -n "${templateDst}" ]; then
|
||||
if [[ ! "${templateDst}" ]]; then
|
||||
echo "ERROR: Template destination '${templateDst}' needs to be specified"
|
||||
exit 1
|
||||
fi
|
||||
@ -49,13 +49,13 @@ function templater() {
|
||||
# cat "${templateDst}"
|
||||
|
||||
# shellcheck disable=SC2016
|
||||
if grep '${' "${templateDst}" && [ "${ALLOW_REMAINDERS}" = "0" ]; then
|
||||
if grep '${' "${templateDst}" && [[ "${ALLOW_REMAINDERS}" = "0" ]]; then
|
||||
echo "ERROR: Unable to replace the above template vars"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
if [ "${BASH_SOURCE[0]}" != "${0}" ]; then
|
||||
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
|
||||
export -f templater
|
||||
else
|
||||
templater "${@}"
|
||||
|
@ -44,7 +44,7 @@ trap cleanup_before_exit EXIT
|
||||
cmdSed=sed
|
||||
cmdTimeout=timeout
|
||||
|
||||
if [[ "${OSTYPE}" == "darwin"* ]]; then
|
||||
if [[ "${OSTYPE}" = "darwin"* ]]; then
|
||||
cmdSed=gsed
|
||||
cmdTimeout=gtimeout
|
||||
fi
|
||||
@ -206,10 +206,11 @@ if [[ "$(command -v shellcheck)" ]]; then
|
||||
|
||||
echo -n " ${file}.. "
|
||||
|
||||
if ! shellcheck --shell=bash --external-sources "${file}" >> "${__accptstTmpDir}/shellcheck.err"; then
|
||||
echo "✗"
|
||||
failed="true"
|
||||
continue
|
||||
if ! shellcheck --shell=bash --external-sources --color=always \
|
||||
"${file}" >> "${__accptstTmpDir}/shellcheck.err"; then
|
||||
echo "✗"
|
||||
failed="true"
|
||||
continue
|
||||
fi
|
||||
|
||||
echo "✓"
|
||||
@ -223,4 +224,34 @@ if [[ "$(command -v shellcheck)" ]]; then
|
||||
fi
|
||||
fi
|
||||
|
||||
# poor man's style guide checking
|
||||
echo "==> b3bp style guide"
|
||||
pushd "${__root}" > /dev/null
|
||||
|
||||
failed="false"
|
||||
|
||||
while IFS=$'\n' read -r file; do
|
||||
[[ "${file}" =~ ^\./node_modules/ ]] && continue
|
||||
[[ "${file}" =~ ^\./website/\.lanyon/ ]] && continue
|
||||
|
||||
echo -n " ${file}.. "
|
||||
|
||||
if ! "${__root}/test/style.pl" "${file}" >> "${__accptstTmpDir}/style.err"; then
|
||||
echo "✗"
|
||||
failed="true"
|
||||
continue
|
||||
fi
|
||||
|
||||
echo "✓"
|
||||
done <<< "$(find . -type f -iname '*.sh')"
|
||||
|
||||
popd > /dev/null
|
||||
|
||||
if [[ "${failed}" = "true" ]]; then
|
||||
echo
|
||||
cat "${__accptstTmpDir}/style.err"
|
||||
echo
|
||||
exit 1
|
||||
fi
|
||||
|
||||
exit 0
|
||||
|
39
test/style.pl
Executable file
39
test/style.pl
Executable file
@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env perl
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
die "usage: $0 <file>\n" if (not @ARGV);
|
||||
|
||||
my $rc = 0;
|
||||
my $file = shift;
|
||||
|
||||
open(my $fh, '<', $file) or die "Cannot open \`$file' for read: $!\n";
|
||||
while (<$fh>) {
|
||||
next if (/^\s*#/);
|
||||
|
||||
my $errors = 0;
|
||||
|
||||
# remove everything between single quotes
|
||||
# this will remove too much in case of: echo "var='$var'"
|
||||
# and thus miss an opportunity to complain later on
|
||||
# also it mangles the input line irreversible
|
||||
s/'[^']+'/'___'/g;
|
||||
|
||||
# highlight unbraced variables--
|
||||
# unless properly backslash'ed
|
||||
$errors += s/((?:^|[^\\]))(((\\\\)+)?\$\w)/$1\033[31m$2\033[0m/g;
|
||||
|
||||
# highlight single square brackets
|
||||
$errors += s/((?:^|\s+))\[([^\[].+[^\]])\](\s*(;|&&|\|\|))/$1\033[31m\[\033[0m$2\033[31m\]\033[0m$3/g;
|
||||
|
||||
# highlight double equal sign
|
||||
$errors += s/(\[\[.*)(==)(.*\]\])/$1\033[31m$2\033[0m$3/g;
|
||||
|
||||
next if (not $errors);
|
||||
print "${file}[$.]: $_";
|
||||
$rc = 1;
|
||||
}
|
||||
close($fh);
|
||||
|
||||
exit $rc;
|
@ -69,7 +69,7 @@ EOF
|
||||
fi
|
||||
|
||||
# Add a "<- Back Home" link, if any
|
||||
echo -e $backLink >> "website/${targetName}.md"
|
||||
echo -e "${backLink}" >> "website/${targetName}.md"
|
||||
|
||||
echo "--> written website/${targetName}.md"
|
||||
done
|
||||
|
Loading…
Reference in New Issue
Block a user