This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeclimate-wrapper
executable file
·182 lines (150 loc) · 4.46 KB
/
codeclimate-wrapper
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/sh
invalid_setup() {
local reason=$1
cat >&2 <<EOF
Your Docker setup does not support the codeclimate wrapper script:
> $reason
We require a local Docker daemon that supports communication via the default
socket path.
Please use \`docker run' to run the \`codeclimate/codeclimate' image directly.
See https://github.com/codeclimate/codeclimate for more details.
EOF
exit 1
}
socket_missing() {
invalid_setup "/var/run/docker.sock must exist as a Unix domain socket"
}
invalid_docker_host() {
local host=$1
invalid_setup "invalid DOCKER_HOST=$host, must be unset or unix:///var/run/docker.sock"
}
analysis_file() {
local file_name="";
local help_mode=0;
local analyze_mode=0;
local skip=0;
for arg; do
if [ $skip -gt 0 ]; then
skip=$(( $skip - 1 ))
else
case "$arg" in
help)
help_mode=1
;;
analyze)
if [ "$help_mode" -eq 0 ]; then
analyze_mode=1;
fi
;;
-*)
if [ $analyze_mode -ne 0 ]; then
case $arg in
-e | -f | --format)
skip=1
;;
esac
fi
;; # We don't care about flags
*)
if [ $analyze_mode -ne 0 ]; then
if [ -n "$file_name" ]; then
printf "Please supply only one file for analysis\n" >&2
exit 1
else
file_name="$arg"
fi
fi
;;
esac
fi
done
if [ -n "$file_name" ]; then
printf "%s" "$file_name"
else
printf "Please supply a file name for analysis\n" >&2
exit 1
fi
}
ln_dir() {
local path="$1"
if [ -z "$path" ] || [ "$path" = "." ] ; then
return
fi
local source_base="$2"
local dest_base="$3"
local path_dir=$(dirname "$path")
mkdir -p "$dest_base"/"$path_dir"
if [ "$path_dir" = "." ]; then
path_dir=""
fi
find "$source_base/$path_dir" -depth 1 -type f -iname "*" | while read f; do
if [ ! -e "$dest_base/$path_dir/$(basename "$f")" ]; then
ln "$f" "$dest_base/$path_dir/$(basename "$f")"
fi
done
ln_dir "$path_dir" "$source_base" "$dest_base"
}
docker_run() {
docker run \
--interactive --rm \
--env CODECLIMATE_CODE \
--env CODECLIMATE_TMP \
--env CODECLIMATE_DEBUG \
--env CODECLIMATE_VERSIONS_URL \
--env CONTAINER_MAXIMUM_OUTPUT_BYTES \
--env CONTAINER_TIMEOUT_SECONDS \
--env ENGINE_MEMORY_LIMIT_BYTES \
--volume "$CODECLIMATE_CODE":/code \
--volume "$CODECLIMATE_TMP":/tmp/cc \
--volume "$CC_CONFIG":/config.yml \
--volume "$CC_CACHE":/cache.yml \
--volume /var/run/docker.sock:/var/run/docker.sock \
"$@"
}
: ${XDG_CONFIG_HOME:=$HOME/.config}
CC_CONFIG="${CODECLIMATE_CONFIG:-$XDG_CONFIG_HOME/codeclimate/config.yml}"
: ${XDG_CACHE_HOME:=$HOME/.cache}
CC_CACHE="${CODECLIMATE_CACHE:-$XDG_CACHE_HOME/codeclimate/cache.yml}"
for f in "$CC_CONFIG" "$CC_CACHE"; do
mkdir -p "$(dirname "$f")"
touch "$f"
done
if [ -z "$CODECLIMATE_CODE" ]; then
export CODECLIMATE_CODE=$PWD
fi
if [ -z "$CODECLIMATE_TMP" ]; then
export CODECLIMATE_TMP=/tmp/cc
fi
if [ ! -t 0 ]; then
stdin_stash=$(mktemp "$(dirname "$CC_CACHE")/cc-stdin.XXXXXX")
cat <&0 >"$stdin_stash"
trap "rm '$stdin_stash'" EXIT
if [ -n "$(cat "$stdin_stash")" ]; then
tmp_source_dir=$(mktemp -d "$(dirname $CC_CACHE)/cc-code.XXXXXX")
trap "rm -rf '$tmp_source_dir'" EXIT
chmod a+rx "$tmp_source_dir"
focus_path=$(analysis_file "$@") || exit 1
source_file="$tmp_source_dir/$focus_path"
mkdir -p "$(dirname "$source_file")"
mv "$stdin_stash" "$source_file"
chmod a+r "$source_file"
ln_dir "$focus_path" "$CODECLIMATE_CODE" "$tmp_source_dir"
export CODECLIMATE_CODE="$tmp_source_dir"
fi
fi
if [ -n "$DOCKER_MACHINE_NAME" ] && command -v docker-machine > /dev/null 2>&1; then
docker-machine ssh $DOCKER_MACHINE_NAME -- \
test -S /var/run/docker.sock > /dev/null 2>&1 || socket_missing
docker-machine ssh $DOCKER_MACHINE_NAME -- \
'test -n "$DOCKER_HOST" -a "$DOCKER_HOST" != "unix:///var/run/docker.sock"' > /dev/null 2>&1 \
&& invalid_docker_host $(docker-machine ssh $DOCKER_MACHINE_NAME -- 'echo "$DOCKER_HOST"')
else
test -S /var/run/docker.sock || socket_missing
test -n "$DOCKER_HOST" -a "$DOCKER_HOST" != "unix:///var/run/docker.sock" \
&& invalid_docker_host "$DOCKER_HOST"
fi
if [ -t 0 ] && [ -t 1 ]; then
docker_run --tty codeclimate/codeclimate "$@"
else
docker_run codeclimate/codeclimate "$@"
fi