Skip to content

Commit d33ef76

Browse files
committed
Fixed arg processing and go-fmt error checking
1 parent f166cf1 commit d33ef76

24 files changed

+319
-143
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ Implements a Go pretty-printer (like `go-fmt`) that also adds zero-value return
283283
284284
| Hook ID | Description
285285
|--------------|------------
286-
| `go-returns` | Run `'goreturns -l -p -d [$ARGS] $FILE'` for each staged .go file
286+
| `go-returns` | Run `'goreturns -l -d [$ARGS] $FILE'` for each staged .go file
287287
288288
##### Install
289289
```

go-build-mod.sh

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env bash
22

3+
cmd=(go build)
4+
35
export GO111MODULE=on
46

57
# Walks up the file path looking for go.mod
@@ -22,26 +24,35 @@ function find_module_roots() {
2224
}
2325

2426
FILES=()
25-
26-
# Build file list while looking for optional argument marker
27+
# Build potential options list (may just be files)
2728
#
2829
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ]; do
2930
FILES+=("$1")
3031
shift
3132
done
3233

33-
# Remove argument marker if present
34+
OPTIONS=()
35+
# If '--' next, then files = options
3436
#
3537
if [ $# -gt 0 ]; then
3638
if [ "$1" == "-" ] || [ "$1" == "--" ]; then
3739
shift
40+
OPTIONS=("${FILES[@]}")
41+
FILES=()
3842
fi
3943
fi
4044

45+
# Any remaining items are files
46+
#
47+
while [ $# -gt 0 ]; do
48+
FILES+=("$1")
49+
shift
50+
done
51+
4152
errCode=0
42-
for sub in $(find_module_roots "${FILES}" | sort -u) ; do
53+
for sub in $(find_module_roots "${FILES[@]}" | sort -u) ; do
4354
pushd "${sub}" >/dev/null
44-
go build "$@" ./...
55+
"${cmd[@]}" "${OPTIONS[@]}" ./...
4556
if [ $? -ne 0 ]; then
4657
errCode=1
4758
fi

go-build-pkg.sh

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
11
#!/usr/bin/env bash
22

3+
cmd=(go build)
4+
35
export GO111MODULE=off
46

57
FILES=()
6-
7-
# Build file list while looking for optional argument marker
8+
# Build potential options list (may just be files)
89
#
910
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ]; do
1011
FILES+=("$1")
1112
shift
1213
done
1314

14-
# Remove argument marker if present
15+
OPTIONS=()
16+
# If '--' next, then files = options
1517
#
1618
if [ $# -gt 0 ]; then
1719
if [ "$1" == "-" ] || [ "$1" == "--" ]; then
1820
shift
21+
OPTIONS=("${FILES[@]}")
22+
FILES=()
1923
fi
2024
fi
2125

26+
# Any remaining items are files
27+
#
28+
while [ $# -gt 0 ]; do
29+
FILES+=("$1")
30+
shift
31+
done
32+
2233
errCode=0
23-
for sub in $(echo "${FILES}" | xargs -n1 dirname | sort -u); do
24-
go build "$@" "./${sub}"
34+
for sub in $(echo "${FILES[@]}" | xargs -n1 dirname | sort -u); do
35+
"${cmd[@]}" "${OPTIONS[@]}" "./${sub}"
2536
if [ $? -ne 0 ]; then
2637
errCode=1
2738
fi

go-build-repo-mod.sh

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
#!/usr/bin/env bash
22

3+
cmd=(go build)
4+
35
export GO111MODULE=on
46

5-
# '-' and '--' are optional argument markers for parity with file scripts
6-
# Remove them if present
7+
OPTIONS=()
8+
# Build options list, ignoring '-', '--', and anything after
79
#
8-
if [ $# -gt 0 ]; then
9-
if [ "$1" == "-" ] || [ "$1" == "--" ]; then
10-
shift
11-
fi
12-
fi
10+
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ]; do
11+
OPTIONS+=("$1")
12+
shift
13+
done
1314

1415
errCode=0
1516
# Assume parent folder of go.mod is module root folder
1617
#
1718
for sub in $(find . -name go.mod | xargs -n1 dirname | sort -u) ; do
1819
pushd "${sub}" >/dev/null
19-
go build "$@" ./...
20+
"${cmd[@]}" "${OPTIONS[@]}" ./...
2021
if [ $? -ne 0 ]; then
2122
errCode=1
2223
fi

go-build-repo-pkg.sh

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#!/usr/bin/env bash
22
set -e
33

4+
cmd=(go build)
5+
46
export GO111MODULE=off
57

6-
# '-' and '--' are optional argument markers for parity with file scripts
7-
# Remove them if present
8+
OPTIONS=()
9+
# Build options list, ignoring '-', '--', and anything after
810
#
9-
if [ $# -gt 0 ]; then
10-
if [ "$1" == "-" ] || [ "$1" == "--" ]; then
11-
shift
12-
fi
13-
fi
11+
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ]; do
12+
OPTIONS+=("$1")
13+
shift
14+
done
1415

15-
go build "$@" ./...
16+
"${cmd[@]}" "${OPTIONS[@]}" ./...

go-critic.sh

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
#!/usr/bin/env bash
22

3-
FILES=()
3+
cmd=(gocritic check)
44

5-
# Build file list while looking for optional argument marker
5+
FILES=()
6+
# Build potential options list (may just be files)
67
#
78
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ]; do
89
FILES+=("$1")
910
shift
1011
done
1112

12-
# Remove argument marker if present
13+
OPTIONS=()
14+
# If '--' next, then files = options
1315
#
1416
if [ $# -gt 0 ]; then
1517
if [ "$1" == "-" ] || [ "$1" == "--" ]; then
1618
shift
19+
OPTIONS=("${FILES[@]}")
20+
FILES=()
1721
fi
1822
fi
1923

24+
# Any remaining items are files
25+
#
26+
while [ $# -gt 0 ]; do
27+
FILES+=("$1")
28+
shift
29+
done
30+
2031
errCode=0
21-
for file in "${FILES}"; do
22-
gocritic check "$@" "${file}"
32+
for file in "${FILES[@]}"; do
33+
"${cmd[@]}" "${OPTIONS[@]}" "${file}"
2334
if [ $? -ne 0 ]; then
2435
errCode=1
2536
fi

go-fmt.sh

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
11
#!/usr/bin/env bash
22

3-
FILES=()
3+
cmd=(gofmt -l -d)
44

5-
# Build file list while looking for optional argument marker
5+
FILES=()
6+
# Build potential options list (may just be files)
67
#
78
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ]; do
89
FILES+=("$1")
910
shift
1011
done
1112

12-
# Remove argument marker if present
13+
OPTIONS=()
14+
# If '--' next, then files = options
1315
#
1416
if [ $# -gt 0 ]; then
1517
if [ "$1" == "-" ] || [ "$1" == "--" ]; then
1618
shift
19+
OPTIONS=("${FILES[@]}")
20+
FILES=()
1721
fi
1822
fi
1923

24+
# Any remaining items are files
25+
#
26+
while [ $# -gt 0 ]; do
27+
FILES+=("$1")
28+
shift
29+
done
30+
2031
errCode=0
21-
for file in "${FILES}"; do
22-
gofmt -l -d "$@" "${file}"
23-
if [ $? -ne 0 ]; then
32+
for file in "${FILES[@]}"; do
33+
output=$("${cmd[@]}" "${OPTIONS[@]}" "${file}" 2>&1)
34+
if [ ! -z "${output}" ]; then
35+
echo -n "${output}"
2436
errCode=1
2537
fi
2638
done

go-imports.sh

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
11
#!/usr/bin/env bash
22

3-
FILES=()
3+
cmd=(goimports -l -d)
44

5-
# Build file list while looking for optional argument marker
5+
FILES=()
6+
# Build potential options list (may just be files)
67
#
78
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ]; do
89
FILES+=("$1")
910
shift
1011
done
1112

12-
# Remove argument marker if present
13+
OPTIONS=()
14+
# If '--' next, then files = options
1315
#
1416
if [ $# -gt 0 ]; then
1517
if [ "$1" == "-" ] || [ "$1" == "--" ]; then
1618
shift
19+
OPTIONS=("${FILES[@]}")
20+
FILES=()
1721
fi
1822
fi
1923

24+
# Any remaining items are files
25+
#
26+
while [ $# -gt 0 ]; do
27+
FILES+=("$1")
28+
shift
29+
done
30+
2031
errCode=0
21-
for file in "${FILES}"; do
22-
goimports -l -d "$@" "${file}"
23-
if [ $? -ne 0 ]; then
32+
for file in "${FILES[@]}"; do
33+
output=$("${cmd[@]}" "${OPTIONS[@]}" "${file}" 2>&1)
34+
if [ ! -z "${output}" ]; then
35+
echo -n "${output}"
2436
errCode=1
2537
fi
2638
done

go-lint.sh

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
#!/usr/bin/env bash
22

3-
FILES=()
3+
cmd=(golint -set_exit_status)
44

5-
# Build file list while looking for optional argument marker
5+
FILES=()
6+
# Build potential options list (may just be files)
67
#
78
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ]; do
89
FILES+=("$1")
910
shift
1011
done
1112

12-
# Remove argument marker if present
13+
OPTIONS=()
14+
# If '--' next, then files = options
1315
#
1416
if [ $# -gt 0 ]; then
1517
if [ "$1" == "-" ] || [ "$1" == "--" ]; then
1618
shift
19+
OPTIONS=("${FILES[@]}")
20+
FILES=()
1721
fi
1822
fi
1923

24+
# Any remaining items are files
25+
#
26+
while [ $# -gt 0 ]; do
27+
FILES+=("$1")
28+
shift
29+
done
30+
2031
errCode=0
21-
for file in "${FILES}"; do
22-
golint -set_exit_status "$@" "${file}"
32+
for file in "${FILES[@]}"; do
33+
"${cmd[@]}" "${OPTIONS[@]}" "${file}"
2334
if [ $? -ne 0 ]; then
2435
errCode=1
2536
fi

go-returns.sh

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
11
#!/usr/bin/env bash
22

3-
FILES=()
3+
cmd=(goreturns -l -d)
44

5-
# Build file list while looking for optional argument marker
5+
FILES=()
6+
# Build potential options list (may just be files)
67
#
78
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ]; do
89
FILES+=("$1")
910
shift
1011
done
1112

12-
# Remove argument marker if present
13+
OPTIONS=()
14+
# If '--' next, then files = options
1315
#
1416
if [ $# -gt 0 ]; then
1517
if [ "$1" == "-" ] || [ "$1" == "--" ]; then
1618
shift
19+
OPTIONS=("${FILES[@]}")
20+
FILES=()
1721
fi
1822
fi
1923

24+
# Any remaining items are files
25+
#
26+
while [ $# -gt 0 ]; do
27+
FILES+=("$1")
28+
shift
29+
done
30+
2031
errCode=0
21-
for file in "${FILES}"; do
22-
goreturns -l -p -d "$@" "${file}"
23-
if [ $? -ne 0 ]; then
32+
for file in "${FILES[@]}"; do
33+
output=$("${cmd[@]}" "${OPTIONS[@]}" "${file}" 2>&1)
34+
if [ ! -z "${output}" ]; then
35+
echo -n "${output}"
2436
errCode=1
2537
fi
2638
done

0 commit comments

Comments
 (0)