-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellcheck
More file actions
executable file
·93 lines (84 loc) · 3.11 KB
/
shellcheck
File metadata and controls
executable file
·93 lines (84 loc) · 3.11 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
#!/usr/bin/env bash
#
# Locate ShellCheck in the current environment and pass it the given arguments.
#
# If ShellCheck is not found, provide instructions for how to install it in the given environment.
#
# To remain consistent with ShellCheck's exit codes, this script will exit with a code of 2 if
# ShellCheck cannot be found locally (unless the `--ignore-missing` option is present).
#
# Reference: https://github.com/koalaman/shellcheck/wiki/Integration#exit-codes
#
# OPTIONS:
#
# Note: All options other than those listed below will be passed directly to the ShellCheck binary.
#
# --debug Print debug messages within this script, useful when troubleshooting
# $PATH issues.
# -i, --ignore-missing Print a warning, but consider the script successful if a local instance
# of ShellCheck could not be found.
#
# This script is part of the assertwell/shellcheck package.
#
# URL: https://github.com/assertwell/shellcheck
# Author: Steve Grunwell
# License: MIT
declare -a args
# Default exit code if ShellCheck cannot be found.
exit_code=2
debug_mode=0
# Print a debug message if debug_mode=1
function debug() {
if [[ "$debug_mode" == 1 ]]; then
printf "\033[0;36m[DEBUG]\033[0;0m %s\n" "$1"
fi
}
# Look for any arguments specific to this script, and put everything else into $args.
while [ $# -gt 0 ]; do
case "$1" in
--debug)
debug_mode=1
shift
;;
-i|--ignore-missing)
exit_code=0
shift
;;
*)
args+=("$1")
shift
;;
esac
done
# When run as part of a Composer script, the bin-dir will be pushed to the front of the $PATH.
#
# However, this can lead to recursion since the first "shellcheck" script `command -v shellcheck`
# will find will be...well, this one.
#
# Composer 2.2.6 introduced the COMPOSER_RUNTIME_BIN_DIR environment variable, so we can use that
# to strip the bin-dir from the user's $PATH.
#
# For older versions of Composer, attempt to build the path by reading the Composer configuration
# (if present).
if [[ -z "$COMPOSER_RUNTIME_BIN_DIR" ]]; then
debug "COMPOSER_RUNTIME_BIN_DIR not found, checking Composer configuration"
COMPOSER_RUNTIME_BIN_DIR="$(composer config --absolute bin-dir || 'true')"
fi
# If we have a value for COMPOSER_RUNTIME_BIN_DIR, strip it from the user's $PATH.
if [[ -n "$COMPOSER_RUNTIME_BIN_DIR" ]]; then
debug "Stripping '${COMPOSER_RUNTIME_BIN_DIR}' from \$PATH"
# shellcheck disable=SC2001
PATH="$(sed -e "s|^${COMPOSER_RUNTIME_BIN_DIR}:||" <<< "$PATH")"
fi
# Find the local ShellCheck binary.
shellcheck="$(command -v shellcheck)"
# Print installation instructions if ShellCheck was not found.
if [[ -z "$shellcheck" ]]; then
echo -e "\n\033[0;33mShellCheck was not found in your \$PATH!\033[0;0m"
echo -e "Please visit \033[4mhttps://github.com/koalaman/shellcheck#installing\033[0m for installation instructions.\n"
debug "PATH: ${PATH}"
exit "$exit_code"
fi
debug "Using ${shellcheck}"
debug "Running \`shellcheck ${args[*]}\`"
"$shellcheck" "${args[@]}"