Skip to content

Commit 1cf9390

Browse files
jaymzhfacebook-github-bot
authored andcommitted
Update run_cookstyle to be more user friendly (facebook#260)
Summary: This enables people to autocorrect, target specific files/dirs, etc. However, by default, it keeps the old (sorta broken) UI for compatibility with internal Meta systems. Signed-off-by: Phil Dibowitz <phil@ipom.com> Pull Request resolved: facebook#260 Differential Revision: D69407588 fbshipit-source-id: 74a1ad60a963661d70b960a716b42ec282cbdb77
1 parent fb907d3 commit 1cf9390

File tree

1 file changed

+66
-12
lines changed

1 file changed

+66
-12
lines changed

scripts/run_cookstyle

Lines changed: 66 additions & 12 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,7 +19,55 @@
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
@@ -29,18 +77,24 @@ else
2977
exit 1
3078
fi
3179

32-
if [ "$#" -eq 0 ]; then
33-
config="$default_config"
34-
elif [ "$#" -eq 1 ]; then
35-
config="$1"
36-
else
37-
echo "Usage: $0 [config]"
38-
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
3988
fi
4089

41-
if [ ! -r "$config" ]; then
42-
echo "Cannot read config config: $config"
90+
if [ ! -r "$CONFIG" ]; then
91+
echo "Cannot read config config: $CONFIG"
4392
exit 1
4493
fi
4594

46-
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)