forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-style.sh
More file actions
executable file
·126 lines (113 loc) · 2.69 KB
/
check-style.sh
File metadata and controls
executable file
·126 lines (113 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env bash
set -euo pipefail
# check-style.sh
#
# SUMMARY
#
# Checks that all text files have correct line endings and no trailing spaces.
#
# USAGE
#
# ./scripts/check-style.sh [--fix] [--all]
#
# --fix: Fix issues instead of just reporting them
# --all: Check all files (default: only check modified files)
MODE="check"
CHECK_ALL=false
# Parse arguments
for arg in "$@"; do
case "$arg" in
--fix)
MODE="fix"
;;
--all)
CHECK_ALL=true
;;
*)
echo "Unknown option: $arg"
echo "Usage: $0 [--fix] [--all]"
exit 1
;;
esac
done
ised() {
local PAT="$1"
local FILE="$2"
# In-place `sed` that uses the form of `sed -i` which works
# on both GNU and macOS.
sed -i.bak "$PAT" "$FILE"
rm "$FILE.bak"
}
# Determine which files to check
if [ "$CHECK_ALL" = true ]; then
# Check all files tracked by git
FILES=$(git ls-files)
else
# Check only files changed in current branch compared to origin/master
FILES=$(git diff --name-only "origin/master"...HEAD)
# If no changed files, fall back to checking all files
if [ -z "$FILES" ]; then
FILES=$(git ls-files)
fi
fi
EXIT_CODE=0
for FILE in $FILES; do
# Ignore binary files and generated files.
case "$FILE" in
*png) continue;;
*svg) continue;;
*gif) continue;;
*ico) continue;;
*sig) continue;;
*html) continue;;
*desc) continue;;
tests/data*) continue;;
lib/codecs/tests/data*) continue;;
lib/vector-core/tests/data*) continue;;
distribution/kubernetes/*/*.yaml) continue;;
tests/helm-snapshots/*/snapshot.yaml) continue;;
lib/remap-tests/tests/*.vrl) continue;;
lib/datadog/grok/patterns/*.pattern) continue;;
esac
# Skip all directories (usually this only happens when we have symlinks).
if [[ -d "$FILE" ]]; then
continue
fi
# check that the file contains trailing newline
if [ -n "$(tail -c1 "$FILE" | tr -d $'\n')" ]; then
case "$MODE" in
check)
echo "File \"$FILE\" doesn't end with a newline"
EXIT_CODE=1
;;
fix)
echo >> "$FILE"
;;
esac
fi
# check that the file uses LF line breaks
if grep $'\r$' "$FILE" > /dev/null; then
case "$MODE" in
check)
echo "File \"$FILE\" contains CRLF line breaks instead of LF line breaks"
EXIT_CODE=1
;;
fix)
ised 's/\r$//' "$FILE"
;;
esac
fi
# check that the lines don't contain trailing spaces
if grep -n ' $' "$FILE"; then
case "$MODE" in
check)
echo "File \"$FILE\" contains trailing spaces in some of the lines"
EXIT_CODE=1
;;
fix)
ised 's/ *$//' "$FILE"
;;
esac
fi
done
exit "$EXIT_CODE"