Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit 782a6e7

Browse files
authored
Merge pull request #699 from HowJMay/fmt_shell
refactor: Format shell scripts
2 parents 7049f38 + 6582608 commit 782a6e7

File tree

15 files changed

+331
-351
lines changed

15 files changed

+331
-351
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ The codebase of this repository follows [Google's C++ guidelines](https://google
142142

143143
* buildifier
144144
* clang-format
145+
* [shfmt](https://github.com/mvdan/sh)
145146

146147
### Buildifier
147148

@@ -171,6 +172,14 @@ clang-format can be installed by command:
171172
* Debian/Ubuntu based systems: `$ sudo apt-get install clang-format`
172173
* macOS: `$ brew install clang-format`
173174

175+
### shfmt
176+
177+
It requires Go 1.13 or above, and install it with following command.
178+
179+
```shell
180+
GO111MODULE=on go get mvdan.cc/sh/v3/cmd/shfmt
181+
```
182+
174183
## Usage
175184

176185
`Tangle-accelerator` currently supports two categories of APIs

endpoint/build-legato.sh

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,21 @@ set -uo pipefail
44

55
COMMON_FILE="tests/endpoint/common.sh"
66

7-
if [ ! -f "$COMMON_FILE" ]
8-
then
9-
echo "$COMMON_FILE is not exists."
10-
echo "Always execute this script in top-level directory."
11-
exit 1
7+
if [ ! -f "$COMMON_FILE" ]; then
8+
echo "$COMMON_FILE is not exists."
9+
echo "Always execute this script in top-level directory."
10+
exit 1
1211
fi
1312
source $COMMON_FILE
1413

15-
if [ ! -d "legato" ]
16-
then
17-
if ! download_legato_repo;
18-
then
19-
echo "Download Legato AF failed. Try to download again..."
20-
rm -rf legato/ .repo/
21-
if ! download_legato_repo;
22-
then
23-
echo "Failed to download the Legato AF. Please check your connection"
24-
exit 1
25-
fi
26-
fi
27-
build_legato_repo
14+
if [ ! -d "legato" ]; then
15+
if ! download_legato_repo; then
16+
echo "Download Legato AF failed. Try to download again..."
17+
rm -rf legato/ .repo/
18+
if ! download_legato_repo; then
19+
echo "Failed to download the Legato AF. Please check your connection"
20+
exit 1
21+
fi
22+
fi
23+
build_legato_repo
2824
fi

endpoint/unit-test/driver.sh

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,52 @@ GREEN='\033[0;32m'
66
NC='\033[0m' # No Color
77
EP_TEST_CONFIG=(asan tsan ubsan)
88

9-
function cleanup(){
10-
kill -9 "${TA}"
9+
function cleanup() {
10+
kill -9 "${TA}"
1111
}
1212

13-
function success(){
14-
cleanup
15-
echo -e "${GREEN}ALL test passed${NC}"
16-
exit 0
13+
function success() {
14+
cleanup
15+
echo -e "${GREEN}ALL test passed${NC}"
16+
exit 0
1717
}
1818

19-
function failed(){
20-
cleanup
21-
echo -e "${RED}Unit-test: ${TEST_CASE} not passed${NC}"
22-
exit 1
19+
function failed() {
20+
cleanup
21+
echo -e "${RED}Unit-test: ${TEST_CASE} not passed${NC}"
22+
exit 1
2323
}
2424

25-
function run_test_suite(){
26-
bazel test -c dbg --config "$1" //endpoint/unit-test/...
27-
ret=$?
28-
if [[ ret -ne 0 ]]
29-
then
30-
TEST_CASE="$1"
31-
failed
32-
fi
25+
function run_test_suite() {
26+
bazel test -c dbg --config "$1" //endpoint/unit-test/...
27+
ret=$?
28+
if [[ ret -ne 0 ]]; then
29+
TEST_CASE="$1"
30+
failed
31+
fi
3332
}
3433

35-
function start_ta(){
36-
# Create tangle-accelerator for unit-test
37-
bazel run accelerator &
38-
TA=$!
39-
# Wait until tangle-accelerator has been initialized
40-
echo "==============Wait for TA starting=============="
41-
while read -r line
42-
do
43-
if [[ "$line" == "TA-START" ]]
44-
then
45-
echo "$line"
46-
fi
47-
done <<< $(nc -U -l $socket | tr '\0' '\n')
48-
echo "==============TA has successfully started=============="
34+
function start_ta() {
35+
# Create tangle-accelerator for unit-test
36+
bazel run accelerator &
37+
TA=$!
38+
# Wait until tangle-accelerator has been initialized
39+
echo "==============Wait for TA starting=============="
40+
while read -r line; do
41+
if [[ "$line" == "TA-START" ]]; then
42+
echo "$line"
43+
fi
44+
done <<<$(nc -U -l $socket | tr '\0' '\n')
45+
echo "==============TA has successfully started=============="
4946
}
5047

5148
echo "Start unit-test for endpoint"
5249
start_ta
5350

5451
# Run endpoint unit-test
55-
for i in ${EP_TEST_CONFIG[*]}; do
56-
run_test_suite "$i"
52+
for i in ${EP_TEST_CONFIG[*]}; do
53+
run_test_suite "$i"
5754
done
5855

59-
# Finish
56+
# Finish
6057
success

hooks/autohook.sh

Lines changed: 68 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55
# Author: Nik Kantar <http://nkantar.com>
66
# Version: 2.1.1
77
# Website: https://github.com/nkantar/Autohook
8-
# License: MIT -
8+
# License: MIT -
99

1010
# MIT License
11-
#
11+
#
1212
# Copyright (c) 2017 Nikola Kantar
13-
#
13+
#
1414
# Permission is hereby granted, free of charge, to any person obtaining a copy
1515
# of this software and associated documentation files (the "Software"), to deal
1616
# in the Software without restriction, including without limitation the rights
1717
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1818
# copies of the Software, and to permit persons to whom the Software is
1919
# furnished to do so, subject to the following conditions:
20-
#
20+
#
2121
# The above copyright notice and this permission notice shall be included in all
2222
# copies or substantial portions of the Software.
23-
#
23+
#
2424
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2525
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2626
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -29,90 +29,78 @@
2929
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3030
# SOFTWARE.
3131

32-
3332
echo() {
34-
builtin echo "[Autohook] $@";
33+
builtin echo "[Autohook] $@"
3534
}
3635

37-
3836
install() {
39-
hook_types=(
40-
"applypatch-msg"
41-
"commit-msg"
42-
"post-applypatch"
43-
"post-checkout"
44-
"post-commit"
45-
"post-merge"
46-
"post-receive"
47-
"post-rewrite"
48-
"post-update"
49-
"pre-applypatch"
50-
"pre-auto-gc"
51-
"pre-commit"
52-
"pre-push"
53-
"pre-rebase"
54-
"pre-receive"
55-
"prepare-commit-msg"
56-
"update"
57-
)
37+
hook_types=(
38+
"applypatch-msg"
39+
"commit-msg"
40+
"post-applypatch"
41+
"post-checkout"
42+
"post-commit"
43+
"post-merge"
44+
"post-receive"
45+
"post-rewrite"
46+
"post-update"
47+
"pre-applypatch"
48+
"pre-auto-gc"
49+
"pre-commit"
50+
"pre-push"
51+
"pre-rebase"
52+
"pre-receive"
53+
"prepare-commit-msg"
54+
"update"
55+
)
5856

59-
repo_root=$(git rev-parse --show-toplevel)
60-
hooks_dir="$repo_root/.git/hooks"
61-
autohook_filename="$repo_root/hooks/autohook.sh"
62-
autohook_path=$(realpath $autohook_filename)
63-
for hook_type in "${hook_types[@]}"
64-
do
65-
hook_symlink="$hooks_dir/$hook_type"
66-
ln -s $autohook_path $hook_symlink
67-
done
57+
repo_root=$(git rev-parse --show-toplevel)
58+
hooks_dir="$repo_root/.git/hooks"
59+
autohook_filename="$repo_root/hooks/autohook.sh"
60+
autohook_path=$(realpath $autohook_filename)
61+
for hook_type in "${hook_types[@]}"; do
62+
hook_symlink="$hooks_dir/$hook_type"
63+
ln -s $autohook_path $hook_symlink
64+
done
6865
}
6966

70-
7167
main() {
72-
calling_file=$(basename $0)
68+
calling_file=$(basename $0)
7369

74-
if [[ $calling_file == "autohook.sh" ]]
75-
then
76-
command=$1
77-
if [[ $command == "install" ]]
78-
then
79-
install
80-
fi
81-
else
82-
repo_root=$(git rev-parse --show-toplevel)
83-
hook_type=$calling_file
84-
symlinks_dir="$repo_root/hooks/$hook_type"
85-
files=("$symlinks_dir"/*)
86-
number_of_symlinks="${#files[@]}"
87-
tmpfile=$(mktemp)
88-
if [[ $number_of_symlinks == 1 ]]
89-
then
90-
if [[ "$(basename ${files[0]})" == "*" ]]
91-
then
92-
number_of_symlinks=0
93-
fi
94-
fi
95-
echo "Looking for $hook_type scripts to run...found $number_of_symlinks!"
96-
if [[ $number_of_symlinks -gt 0 ]]
97-
then
98-
for file in "${files[@]}"
99-
do
100-
scriptname=$(basename $file)
101-
echo "BEGIN $scriptname"
102-
eval $file > $tmpfile
103-
ret=$?
104-
if [[ $ret -ne 0 ]]
105-
then
106-
cat $tmpfile
107-
rm $tmpfile
108-
exit $ret
109-
fi
110-
echo "FINISH $scriptname"
111-
done
112-
fi
113-
rm $tmpfile
114-
fi
70+
if [[ $calling_file == "autohook.sh" ]]; then
71+
command=$1
72+
if [[ $command == "install" ]]; then
73+
install
74+
fi
75+
else
76+
repo_root=$(git rev-parse --show-toplevel)
77+
hook_type=$calling_file
78+
symlinks_dir="$repo_root/hooks/$hook_type"
79+
files=("$symlinks_dir"/*)
80+
number_of_symlinks="${#files[@]}"
81+
tmpfile=$(mktemp)
82+
if [[ $number_of_symlinks == 1 ]]; then
83+
if [[ "$(basename ${files[0]})" == "*" ]]; then
84+
number_of_symlinks=0
85+
fi
86+
fi
87+
echo "Looking for $hook_type scripts to run...found $number_of_symlinks!"
88+
if [[ $number_of_symlinks -gt 0 ]]; then
89+
for file in "${files[@]}"; do
90+
scriptname=$(basename $file)
91+
echo "BEGIN $scriptname"
92+
eval $file >$tmpfile
93+
ret=$?
94+
if [[ $ret -ne 0 ]]; then
95+
cat $tmpfile
96+
rm $tmpfile
97+
exit $ret
98+
fi
99+
echo "FINISH $scriptname"
100+
done
101+
fi
102+
rm $tmpfile
103+
fi
115104
}
116105

117-
118106
main "$@"

hooks/ci_format_check

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
root=$(git rev-parse --show-toplevel)
44
status=0
5-
for file in $(find "${@}" -type f | grep -E "\.(c|cc|cpp|h|hh|hpp)\$" | grep -Ev "/third_party/")
6-
do
7-
filepath="$root/$file"
8-
output=$(diff <(cat $filepath) <(clang-format -style=file -fallback-style=none $filepath))
9-
if [ $? -ne 0 ]
10-
then
11-
echo -e "\nFile \e[31m\""$file"\"\e[39m is not compliant with the coding style"
12-
echo "$output"
13-
status=1
14-
fi
5+
for file in $(find "${@}" -type f | grep -E "\.(c|cc|cpp|h|hh|hpp)\$" | grep -Ev "/third_party/"); do
6+
filepath="$root/$file"
7+
output=$(diff <(cat $filepath) <(clang-format -style=file -fallback-style=none $filepath))
8+
if [ $? -ne 0 ]; then
9+
echo -e "\nFile \e[31m\""$file"\"\e[39m is not compliant with the coding style"
10+
echo "$output"
11+
status=1
12+
fi
1513
done
1614
exit $status

hooks/formatter

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#!/bin/bash
22

3-
for file in $(find $(git rev-parse --show-toplevel) | egrep "\.(c|cc|cpp|h|hh|hpp|m|mm)\$" | egrep -v -f hooks/format-exclude-list)
4-
do
5-
clang-format -style=file -fallback-style=none -i $file
3+
for file in $(find $(git rev-parse --show-toplevel) | egrep "\.(c|cc|cpp|h|hh|hpp|m|mm)\$" | egrep -v "third_party|output_base"); do
4+
clang-format -style=file -fallback-style=none -i $file
65
done
76

8-
for file in $(find $(git rev-parse --show-toplevel) | egrep "\BUILD\$" | egrep -v -f hooks/format-exclude-list )
9-
do
10-
buildifier $file
7+
for file in $(find $(git rev-parse --show-toplevel) | egrep "\BUILD\$" | egrep -v "third_party|output_base"); do
8+
buildifier $file
119
done
1210
buildifier WORKSPACE
11+
12+
for file in $(find $(git rev-parse --show-toplevel) | egrep "\.sh\$" | egrep -v "third_party|output_base"); do
13+
shfmt -w $file
14+
done

0 commit comments

Comments
 (0)