|
1 | | -#!/bin/sh |
| 1 | +#!/bin/bash |
2 | 2 | # |
3 | 3 | # vim: syntax=ruby:expandtab:shiftwidth=2:softtabstop=2:tabstop=2 |
4 | 4 | # |
|
19 | 19 |
|
20 | 20 | set -eu |
21 | 21 |
|
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 | + |
23 | 71 | if bundle exec cookstyle --version > /dev/null 2>&1; then |
24 | 72 | COOKSTYLE='bundle exec cookstyle' |
25 | 73 | elif [ -x /opt/chef-workstation/embedded/bin/cookstyle ]; then |
26 | 74 | 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' |
29 | 75 | else |
30 | 76 | echo 'Cannot find cookstyle!' |
31 | 77 | exit 1 |
32 | 78 | fi |
33 | 79 |
|
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 |
41 | 88 | fi |
42 | 89 |
|
43 | | -if [ ! -r "$config" ]; then |
44 | | - echo "Cannot read config config: $config" |
| 90 | +if [ ! -r "$CONFIG" ]; then |
| 91 | + echo "Cannot read config config: $CONFIG" |
45 | 92 | exit 1 |
46 | 93 | fi |
47 | 94 |
|
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