forked from b3nj5m1n/xdg-ninja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdg-ninja.sh
executable file
·323 lines (274 loc) · 10.5 KB
/
xdg-ninja.sh
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env sh
has_command() {
command -v "$1" >/dev/null 2>/dev/null
return $?
}
unalias -a
init_constants() {
FX_RESET="\033[0m"
FX_BOLD="\033[1m"
FX_ITALIC="\033[3m"
FG_RED="\033[31m"
FG_GREEN="\033[32m"
FG_YELLOW="\033[33m"
FG_CYAN="\033[36m"
FG_WHITE="\033[37m"
BG_MAGENTA="\033[45m"
}
init_constants
help() {
HELPSTRING="""\
${FG_WHITE}${BG_MAGENTA}${FX_BOLD}xdg-ninja${FX_RESET}
${FX_BOLD}${FX_ITALIC}Check your \$HOME for unwanted files.${FX_RESET}
${FX_ITALIC}--help${FX_RESET} ${FX_BOLD}This help menu${FX_RESET}
${FX_ITALIC}-h\033${FX_RESET}
${FX_ITALIC}--skip-ok${FX_RESET} ${FX_BOLD}Don't display anything for files that do not exist (default)${FX_RESET}
${FX_ITALIC}--no-skip-ok${FX_RESET} ${FX_BOLD}Display messages for all files checked${FX_RESET}
${FX_ITALIC}--skip-warn${FX_RESET} ${FX_BOLD}Don't display anything for files that cannot be fixed${FX_RESET}
${FX_ITALIC}--no-skip-warn${FX_RESET} ${FX_BOLD}Display help for files that cannot be fixed (default)${FX_RESET}
${FX_ITALIC}--color=WHEN${FX_RESET} ${FX_BOLD}Color the output always, never, or auto (default)${FX_RESET}
${FX_ITALIC}--decoder=X${FX_RESET} ${FX_BOLD}Manually set the decoder used for markdown${FX_RESET}
${FX_ITALIC}--output-style=X${FX_RESET} ${FX_BOLD}Style of output; normal (default) or json${FX_RESET}
${FX_ITALIC}--quiet${FX_RESET} ${FX_BOLD}Run script quietly${FX_RESET}
${FX_ITALIC}-q${FX_RESET}
${FX_ITALIC}--verbose${FX_RESET} ${FX_BOLD}Alias for --no-skip-warn and --no-skip-ok${FX_RESET}
${FX_ITALIC}-v${FX_RESET}
"""
printf "%b\n" "$HELPSTRING"
exit 0
}
SKIP_OK=true
SKIP_WARN=false
COLOR=auto
OUTPUT_STYLE=normal
EXIT_STATUS=0
HELP=false
QUIET=false
for i in "$@"; do
case $i in
--color=*)
COLOR="${i#*=}"
;;
--help|-h)
HELP=true
;;
--skip-ok)
SKIP_OK=true
;;
--skip-warn)
SKIP_WARN=true
;;
--no-skip-warn)
SKIP_WARN=false
;;
--no-skip-ok)
SKIP_OK=false
;;
--verbose|-v)
SKIP_OK=false
SKIP_WARN=false
;;
--decoder=*)
DECODER="${i#*=}"
;;
--output-style=*)
OUTPUT_STYLE="${i#*=}"
;;
--quiet|-q)
QUIET=true
;;
--loud)
QUIET=false
;;
esac
done
auto_set_decoder() {
DECODER="cat"
if has_command glow; then
DECODER="glow -"
else
if has_command bat; then
DECODER="bat -pp --decorations=always --color=always --language markdown"
if [ "$QUIET" != false ]; then
printf "Markdown rendering will be done by bat. (Glow is recommended)\n"
fi
elif has_command pygmentize; then
DECODER="pygmentize -l markdown"
if [ "$QUIET" != false ]; then
printf "Markdown rendering will be done by pygmentize. (Glow is recommended)\n"
fi
elif has_command highlight; then
DECODER="highlight --out-format ansi --syntax markdown"
if [ "$QUIET" != false ]; then
printf "Markdown rendering will be done by highlight. (Glow is recommended)\n"
fi
else
if [ "$QUIET" != false ]; then
printf "Markdown rendering not available. (Glow is recommended)\n"
printf "Output will be raw markdown and might look weird.\n"
fi
fi
if [ "$QUIET" != false ]; then
printf "Install glow for easier reading & copy-paste.\n"
fi
fi
}
set_colors() {
case "$COLOR" in
"always")
return
;;
"auto")
if [ -t 1 ] && [ "$NO_COLOR" != false ]; then # Check if used in a pipe or if the NO_COLOR env variable is set.
return
fi
;;
esac
DECODER="cat"
JQ_COLOR_VAR="-M"
FX_RESET=""
FX_BOLD=""
FX_ITALIC=""
FG_RED=""
FG_GREEN=""
FG_YELLOW=""
FG_CYAN=""
FG_WHITE=""
BG_MAGENTA=""
}
[ $HELP = "true" ] && help
if [ -z "${XDG_DATA_HOME}" ] && [ "$QUIET" != false ]; then
printf '%b%s%b\n' "${FX_BOLD}${FG_CYAN}" "The \$XDG_DATA_HOME environment variable is not set, make sure to add it to your shell's configuration before setting any of the other environment variables!" "${FX_RESET}"
printf "%b ⤷ The recommended value is: %b\$HOME/.local/share%b\n" "${FX_BOLD}${FG_CYAN}" "${FX_BOLD}${FX_ITALIC}" "${FX_RESET}"
fi
if [ -z "${XDG_CONFIG_HOME}" ] && [ "$QUIET" != false ]; then
printf '%b%s%b\n' "${FX_BOLD}${FG_CYAN}" "The \$XDG_CONFIG_HOME environment variable is not set, make sure to add it to your shell's configuration before setting any of the other environment variables!" "${FX_RESET}"
printf "%b ⤷ The recommended value is: %b\$HOME/.config%b\n" "${FX_BOLD}${FG_CYAN}" "${FX_BOLD}${FX_ITALIC}" "${FX_RESET}"
fi
if [ -z "${XDG_STATE_HOME}" ] && [ "$QUIET" != false ]; then
printf '%b%s%b\n' "${FX_BOLD}${FG_CYAN}" "The \$XDG_STATE_HOME environment variable is not set, make sure to add it to your shell's configuration before setting any of the other environment variables!" "${FX_RESET}"
printf "%b ⤷ The recommended value is: %b\$HOME/.local/state%b\n" "${FX_BOLD}${FG_CYAN}" "${FX_BOLD}${FX_ITALIC}" "${FX_RESET}"
fi
if [ -z "${XDG_CACHE_HOME}" ] && [ "$QUIET" != false ]; then
printf '%b%s%b\n' "${FX_BOLD}${FG_CYAN}" "The \$XDG_CACHE_HOME environment variable is not set, make sure to add it to your shell's configuration before setting any of the other environment variables!" "${FX_RESET}"
printf "%b ⤷ The recommended value is: %b\$HOME/.cache%b\n" "${FX_BOLD}${FG_CYAN}" "${FX_BOLD}${FX_ITALIC}" "${FX_RESET}"
fi
if [ -z "${XDG_RUNTIME_DIR}" ] && [ "$QUIET" != false ]; then
printf '%b%s%b\n' "${FX_BOLD}${FG_CYAN}" "The \$XDG_RUNTIME_DIR environment variable is not set, make sure to add it to your shell's configuration before setting any of the other environment variables!" "${FX_RESET}"
printf "%b ⤷ The recommended value is: %b/run/user/\$UID%b\n" "${FX_BOLD}${FG_CYAN}" "${FX_BOLD}${FX_ITALIC}" "${FX_RESET}"
fi
if ! command -v jq >/dev/null 2>/dev/null; then
printf "jq is needed to run this script, but it wasn't found. Please install it to be able to use this script.\n"
exit
fi
if [ "$QUIET" != false ]; then
printf "\n"
fi
# Function to expand environment variables in string
# https://stackoverflow.com/a/20316582/11110290
apply_shell_expansion() {
data="$1"
delimiter="__apply_shell_expansion_delimiter__"
command=$(printf "cat <<%s\n%s\n%s" "$delimiter" "$data" "$delimiter")
eval "$command"
}
# Returns 0 if the path doesn't lead anywhere
# Returns 1 if the path leads to something
check_if_file_exists() {
FILE_PATH=$(apply_shell_expansion "$1")
if [ -e "$FILE_PATH" ]; then
return 1
else
return 0
fi
}
decode_string() {
printf "%s" "$1" | sed -e 's/\\n/\
/g' -e 's/\\\"/\"/g' -e '$ s/\n*$/\
\
/' # Replace \n with literal newline and \" with ", normalize number of trailing newlines to 2
}
# Function to handle the formatting of output
log() {
MODE="$1"
NAME="$2"
FILENAME="$3"
HELP="$4"
case "$MODE" in
ERR)
printf '[%b%s%b]: %b%s%b\n' "${FX_BOLD}${FG_RED}" "$NAME" "${FX_RESET}" "${FX_BOLD}${FX_ITALIC}" "$FILENAME" "${FX_RESET}"
;;
WARN)
printf '[%b%s%b]: %b%s%b\n' "${FX_BOLD}${FG_YELLOW}" "$NAME" "${FX_RESET}" "${FX_BOLD}${FX_ITALIC}" "$FILENAME" "${FX_RESET}"
;;
INFO)
printf '[%b%s%b]: %b%s%b\n' "${FX_BOLD}${FG_CYAN}" "$NAME" "${FX_RESET}" "${FX_BOLD}${FX_ITALIC}" "$FILENAME" "${FX_RESET}"
;;
SUCS)
[ "$SKIP_OK" = false ] &&
printf '[%b%s%b]: %b%s%b\n' "${FX_BOLD}${FG_GREEN}" "$NAME" "${FX_RESET}" "${FX_BOLD}${FX_ITALIC}" "$FILENAME" "${FX_RESET}"
;;
HELP)
decode_string "$HELP" | $DECODER
;;
esac
}
# Checks that the given file does not exist, otherwise outputs help
check_file() {
NAME="$1"
FILENAME="$2"
MOVABLE="$3"
HELP="$4"
JSON_FILE="$5"
check_if_file_exists "$FILENAME"
case $? in
0)
[ "$SKIP_OK" = false ] && [ "$OUTPUT_STYLE" = "json" ] && cat "$JSON_FILE" && return
log SUCS "$NAME" "$FILENAME" "$HELP"
;;
1)
if [ "$MOVABLE" = true ]; then
EXIT_STATUS=$((EXIT_STATUS + 1))
[ "$OUTPUT_STYLE" = "json" ] && cat "$JSON_FILE" > "$XDG_RUNTIME_DIR"/xdg-ninja"$NAME".json && return
log ERR "$NAME" "$FILENAME" "$HELP"
else
[ "$SKIP_WARN" = true ] && return
[ "$OUTPUT_STYLE" = "json" ] && cat "$JSON_FILE" > "$XDG_RUNTIME_DIR"/xdg-ninja/"$NAME".json && return
log WARN "$NAME" "$FILENAME" "$HELP"
fi
if [ "$HELP" ]; then
log HELP "$NAME" "$FILENAME" "$HELP"
else
log HELP "$NAME" "$FILENAME" "_No help available._"
fi
;;
esac
}
# Reads files from programs/, calls check_file on each file specified for each program
do_check_programs() {
[ "$OUTPUT_STYLE" = "json" ] && mkdir "$XDG_RUNTIME_DIR"/xdg-ninja/
while IFS="
" read -r name; read -r filename; read -r movable; read -r help; read -r json_file; do
check_file "$name" "$filename" "$movable" "$help" "$json_file"
done <<EOF
$(jq 'inputs as $input | $input.files[] as $file | $input.name, $file.path, $file.movable, $file.help, input_filename' "$(dirname "$0")"/programs/* | sed -e 's/^"//' -e 's/"$//')
EOF
[ "$OUTPUT_STYLE" = "json" ] && jq "$JQ_COLOR_VAR" -s . "$XDG_RUNTIME_DIR"/xdg-ninja/* && rm -rf "$XDG_RUNTIME_DIR/xdg-ninja"
}
check_programs() {
if [ "$QUIET" != false ]; then
printf "%bStarting to check your %b\$HOME%b.\n" "${FX_BOLD}${FX_ITALIC}" "${FG_CYAN}" "${FX_RESET}"
printf "\n"
fi
do_check_programs
if [ "$QUIET" != false ]; then
printf "%bDone checking your %b\$HOME.%b\n" "${FX_BOLD}${FX_ITALIC}" "${FG_CYAN}" "${FX_RESET}"
printf "\n"
printf "%bIf you have files in your %b\$HOME%b that shouldn't be there, but weren't recognised by xdg-ninja, please consider creating a configuration file for it and opening a pull request on github.%b\n" "${FX_ITALIC}" "${FG_CYAN}" "${FX_RESET}${FX_ITALIC}" "${FX_RESET}"
printf "\n"
fi
}
auto_set_decoder
set_colors
check_programs
exit "$EXIT_STATUS"