Skip to content

Commit 5902d14

Browse files
committed
Sync from upstream - update run_cookstyle to be more user friendly
1 parent b619b8f commit 5902d14

File tree

1 file changed

+66
-14
lines changed

1 file changed

+66
-14
lines changed

scripts/run_cookstyle

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/bash
22
#
33
# vim: syntax=ruby:expandtab:shiftwidth=2:softtabstop=2:tabstop=2
44
#
@@ -19,30 +19,82 @@
1919

2020
set -eu
2121

22-
default_config='.cookstyle_combined.yml'
22+
usage() {
23+
cat <<EOF
24+
Usage:
25+
$0 [<config>]]
26+
$0 -C [<options>] [<file>...]
27+
28+
By default this script runs in backwards-compatibility mode where
29+
the only arguments is a config. However, if you pass in -C, it will
30+
parse options, and you can pass in a config with -c, and any additional
31+
arguments will be passed as files/dirs to be linted.
32+
33+
Options:
34+
-a Enable autocorrect
35+
-C Disable compatibility mode
36+
-c <config> Use <config> file for cookstyle
37+
-h Print this message and exist
38+
EOF
39+
}
40+
41+
CONFIG='.cookstyle_combined.yml'
42+
AUTOCORRECT=0
43+
COMPAT_MODE=1
44+
45+
while getopts 'ac:Ch' opt; do
46+
case "$opt" in
47+
a)
48+
AUTOCORRECT=1
49+
;;
50+
c)
51+
CONFIG="$OPTARG"
52+
;;
53+
C)
54+
COMPAT_MODE=0
55+
;;
56+
h)
57+
usage
58+
exit 0
59+
;;
60+
\?)
61+
echo "Unknown option: -$OPTARG" >&2
62+
exit 1
63+
;;
64+
esac
65+
done
66+
67+
# shift away args we parsed, what's left should be a config
68+
# file, if anything
69+
shift "$((OPTIND - 1))"
70+
2371
if bundle exec cookstyle --version > /dev/null 2>&1; then
2472
COOKSTYLE='bundle exec cookstyle'
2573
elif [ -x /opt/chef-workstation/embedded/bin/cookstyle ]; then
2674
COOKSTYLE='/opt/chef-workstation/embedded/bin/cookstyle'
27-
elif [ -x /opt/cinc-workstation/embedded/bin/cookstyle ]; then
28-
COOKSTYLE='/opt/cinc-workstation/embedded/bin/cookstyle'
2975
else
3076
echo 'Cannot find cookstyle!'
3177
exit 1
3278
fi
3379

34-
if [ "$#" -eq 0 ]; then
35-
config="$default_config"
36-
elif [ "$#" -eq 1 ]; then
37-
config="$1"
38-
else
39-
echo "Usage: $0 [config]"
40-
exit 1
80+
if [ "$COMPAT_MODE" -eq 1 ]; then
81+
if [ "$#" -eq 1 ]; then
82+
CONFIG="$1"
83+
shift
84+
elif [ "$#" -ne 0 ]; then
85+
usage
86+
exit 1
87+
fi
4188
fi
4289

43-
if [ ! -r "$config" ]; then
44-
echo "Cannot read config config: $config"
90+
if [ ! -r "$CONFIG" ]; then
91+
echo "Cannot read config config: $CONFIG"
4592
exit 1
4693
fi
4794

48-
exec $COOKSTYLE --display-cop-names -c "$config"
95+
declare -a options
96+
if [ "$AUTOCORRECT" -eq 1 ]; then
97+
options+=("-a")
98+
fi
99+
100+
exec $COOKSTYLE --display-cop-names -c "$CONFIG" "${options[@]}" "$@"

0 commit comments

Comments
 (0)