Skip to content

Commit

Permalink
Use shfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
rdavid committed Mar 8, 2023
1 parent 26de355 commit c94c23a
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 50 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ludeeus/action-shellcheck@master
with:
additional_files: app/*
ignore_names: pass
- uses: ibiqlik/action-yamllint@v3
with:
file_or_dir: .github/workflows/*.yml
strict: true
- uses: ludeeus/action-shellcheck@master
with:
additional_files: app/*
ignore_names: pass
- uses: mfinelli/setup-shfmt@v2
- run: shfmt -d app
32 changes: 17 additions & 15 deletions app/copyright
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@
# https://stackoverflow.com/questions/18597697/posix-compliant-way-to-scope-variables-to-a-function-in-a-shell-script
# shellcheck disable=SC3043 # Uses local variables.
# shellcheck disable=SC1091,SC2034 # File not following, appears unused.
readonly BASE_APP_VERSION=0.9.20221222
readonly BASE_APP_VERSION=0.9.20230308
. base.sh

# Analyzes input files, fixes copyright years if needed.
fix_outdated() {
local arg copyrighted modified
for arg do
for arg; do
modified="$(git log -1 --pretty=format:'%aD' "$arg" | cut -d \ -f 4)"
copyrighted="$(
grep -E "$PATTERN" < "$arg" |
grep -E "$PATTERN" <"$arg" |
grep -Eo '[0-9]{4}' |
tail -n 1
)"
[ "$modified" != "$copyrighted" ] ||
{ log "$arg: copyrighted $copyrighted."; continue;}
[ "$modified" != "$copyrighted" ] || {
log "$arg: copyrighted $copyrighted."
continue
}
if [ "$ACTION" = true ]; then
sed -i '' \
"s/^\(.*\)-$copyrighted\(.*\)$OWNER/\1-$modified\2$OWNER/g" \
Expand All @@ -39,18 +41,18 @@ fix_outdated() {
validate() {
validate_cmd git
local arg
for arg do
for arg; do
shift
case "$arg" in
-a|--action)
ACTION=true
;;
-o|--owner)
set +o nounset
[ -n "$1" ] || die --owner requires a non-empty argument.
set -o nounset
OWNER="$1"
;;
-a | --action)
ACTION=true
;;
-o | --owner)
set +o nounset
[ -n "$1" ] || die --owner requires a non-empty argument.
set -o nounset
OWNER="$1"
;;
esac
done
var_exists ACTION || ACTION=false
Expand Down
2 changes: 1 addition & 1 deletion app/essid
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ readonly \
OLD=/"$BASE_WIP"/old

list() {
iwlist wlan0 s | grep ESSID | tr -s ' ' | cut -d':' -f2 > "$CUR"
iwlist wlan0 s | grep ESSID | tr -s ' ' | cut -d':' -f2 >"$CUR"
}

copy() {
Expand Down
2 changes: 1 addition & 1 deletion app/flactomp3
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ metaflac --export-tags-to=/dev/stdout "$SRC" |
-e 's/$/"/' \
-e 's/Album=/ALBUM=/' \
-e 's/Genre=/GENRE=/' \
-e 's/Artist=/ARTIST=/' > "$TAG"
-e 's/Artist=/ARTIST=/' >"$TAG"
cat "$TAG"

# shellcheck source=/dev/null
Expand Down
19 changes: 13 additions & 6 deletions app/install
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# https://stackoverflow.com/questions/18597697/posix-compliant-way-to-scope-variables-to-a-function-in-a-shell-script
# shellcheck disable=SC3043,SC2034 # Uses local variables, appears unused.
readonly \
BASE_APP_VERSION=0.9.20230224 \
BASE_APP_VERSION=0.9.20230308 \
DST=/usr/local/bin \
REL=0.9.20230212 \
TGT=/usr/local/bin/base.sh
Expand Down Expand Up @@ -46,18 +46,25 @@ shellbase() {
--to-stdout \
shellbase-$REL/lib/base.sh
)"
[ -n "${BASE_VERSION+x}" ] || { printf >&2 No\ shellbase.\\n; exit 14;}
[ -n "${BASE_VERSION+x}" ] || {
printf >&2 No\ shellbase.\\n
exit 14
}
}

# Certain packages should be installed.
validate() {
local pkg
for pkg in curl tar; do
command -v $pkg >/dev/null 2>&1 ||
{ printf >&2 Install\ %s.\\n $pkg; exit 12;}
command -v $pkg >/dev/null 2>&1 || {
printf >&2 Install\ %s.\\n $pkg
exit 12
}
done
curl --fail --head --output /dev/null --silent $SRC ||
{ printf >&2 '%s is unavailable.\n' $SRC; exit 13;}
curl --fail --head --output /dev/null --silent $SRC || {
printf >&2 '%s is unavailable.\n' $SRC
exit 13
}
shellbase
is_writable $DST || die $DST is not writable.
file_exists $TGT &&
Expand Down
27 changes: 19 additions & 8 deletions app/myip
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ myip() {
myip.opendns.com \
@resolver4.opendns.com \
2>&1
)" || { loge OpenDNS failed: "$ip"; return 0;}
)" || {
loge OpenDNS failed: "$ip"
return 0
}
var_exists ip >/dev/null || {
ip="$(
dig \
Expand All @@ -30,12 +33,20 @@ myip() {
o-o.myaddr.l.google.com \
@ns1.google.com \
2>&1
)" || { loge Google DNS failed: "$ip"; return 0;}
ip="$(printf %s "$ip" | awk -F\" '{print $2}')"
var_exists ip >/dev/null ||
{ loge OpenDNS and Google DNS failed to resolve my IP; return 0;}
)" || {
loge Google DNS failed: "$ip"
return 0
}
ip="$(printf %s "$ip" | awk -F\" '{print $2}')"
var_exists ip >/dev/null || {
loge OpenDNS and Google DNS failed to resolve my IP
return 0
}
}
out="$(whois "$ip" 2>&1)" || {
loge whois "$ip" failed: "$out"
return 0
}
out="$(whois "$ip" 2>&1)" || { loge whois "$ip" failed: "$out"; return 0;}

# At first tries to find a state of a country.
state="$(
Expand Down Expand Up @@ -65,10 +76,10 @@ main() {
validate_cmd awk dig whois
local i=0
while :; do
[ $((i%24)) -eq 0 ] && title
[ $((i % 24)) -eq 0 ] && title
myip
sleep 5
i=$((i+1))
i=$((i + 1))
done
}

Expand Down
2 changes: 1 addition & 1 deletion app/pingo
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# shellcheck disable=SC1091 # Disables not following the file.
. base.sh
[ $# -eq 1 ] || die Usage: pingo [network resource].
{ ping "$1" 2>&1 1>&3 3>&- | to_loge;} 3>&1 1>&2 | to_log
{ ping "$1" 2>&1 1>&3 3>&- | to_loge; } 3>&1 1>&2 | to_log
11 changes: 7 additions & 4 deletions app/scanner
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
. base.sh
MAXV=10
MINV=1
MIDL=$(((MAXV-MINV+1)/2))
MIDL=$(((MAXV - MINV + 1) / 2))
readonly \
MAXV \
MIDL \
Expand All @@ -26,7 +26,7 @@ scan() {
CNTR=$MAXV
else
if [ $CNTR -ne $MINV ]; then
CNTR=$((CNTR-1))
CNTR=$((CNTR - 1))
fi
fi
}
Expand All @@ -46,13 +46,16 @@ wait() {
is_mac() {
echo "$1" |
grep -E '^([0-9a-f]{2}:){5}[0-9a-f]{2}$' >/dev/null ||
die MAC address "$1" is invalid.
die MAC address "$1" is invalid.
}

be_root
validate_cmd arp-scan
[ "$#" -eq 1 ] || die "Usage: $BASE_IAM [MAC-ADDRESS:list]"
[ "$1" = list ] && { $SCAN; exit 0;}
[ "$1" = list ] && {
$SCAN
exit 0
}
is_mac "$1"
MACA="$1"
while :; do
Expand Down
4 changes: 2 additions & 2 deletions app/speed
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ main() {
# The index starts with 0, it guarantees the title printing from the begging.
local i=0
while :; do
[ $((i%30)) -eq 0 ] && title
[ $((i % 30)) -eq 0 ] && title
test_speed
i=$((i+1))
i=$((i + 1))
done
}

Expand Down
5 changes: 4 additions & 1 deletion app/tru
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ tid() {
out="$(transmission-remote "$SER" $AUT --list 2>&1)" ||
die Unable to communicate with "$SER": "$out"
out="$(printf %s "$out" | grep "$NME")"
[ -n "$out" ] || { logw There is no "$NME" in "$SER".; return 0;}
[ -n "$out" ] || {
logw There is no "$NME" in "$SER".
return 0
}
out="$(printf %s "$out" | awk '{print $1}')"
[ -n "$out" ] || die Wrong format: "$out".
printf %s "$out"
Expand Down
17 changes: 10 additions & 7 deletions app/ydata
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@ fi
3>&1 1>&2 | to_log

# Stops if there are no downloaded files.
is_empty "$VID" && { log There is no new downloaded video.; exit 0;}
is_empty "$VID" && {
log There is no new downloaded video.
exit 0
}

# Calculates output table width for renamr and transcode utilities.
WID=$(( $(tput cols) - $(printf '19700101-01:01:01 I ' | wc -m) ))
WID=$(($(tput cols) - $(printf '19700101-01:01:01 I ' | wc -m)))
[ "$WID" -le 1 ] && die Terminal\'s width "$(tput cold)" is too small.

# Renames all downloaded files to the same manner: ASCII, lower case, no
Expand All @@ -87,7 +90,7 @@ while read -r a; do
if file_exists "$VID/$a"*; then
mv "$VID/$a"* "$AUD"
fi
done < "$AUT"
done <"$AUT"
is_empty "$VID" ||
{
transcode \
Expand Down Expand Up @@ -126,11 +129,11 @@ nsrc=0
ndst=0
pipe="$BASE_WIP"/pipe
mkfifo "$pipe"
find "$RES" -type f -exec basename {} \; > "$pipe" &
find "$RES" -type f -exec basename {} \; >"$pipe" &
while read -r name; do
nsrc=$((nsrc+1))
[ -f "$DST/$name" ] && ndst=$((ndst+1))
done < "$pipe"
nsrc=$((nsrc + 1))
[ -f "$DST/$name" ] && ndst=$((ndst + 1))
done <"$pipe"
rm "$pipe"

# Keeps files locally in case of a failure.
Expand Down

0 comments on commit c94c23a

Please sign in to comment.