diff --git a/README.md b/README.md index d9aa8d2..45d0898 100644 --- a/README.md +++ b/README.md @@ -1,72 +1,77 @@ Linter for shellcheck ShellCheck is a comprehensive linter for shell scripts to highlight syntax issues, seantic problems and more subtle caveats, to avoid scripts to behave counter-intuitively or stop to work in some odd conditions. For more information about ShellCheck, see http://www.shellcheck.net/ **REQUIREMENTS** This library requires Arcanist and ShellCheck installed. **INSTALLATION** Clone this repository or deploy to the same folder where you've arcanist and libphutil. For example if they live in /opt/phabricator you should deploy this as /opt/phabricator/shellcheck-linter. **USE** 1. Add to your .arcconfig the requirements to load the extra library: ```lang=json "load": [ "shellcheck-linter" ] ``` Replace the absolute value by a relative path to the folder if you aren't able to install globally as instructed above: ```lang=json "load": [ "./vendor/shellcheck-linter" ] ``` 2. Configure .arclint to lint shell: ``` { "linters": { "shell": { "type": "shellcheck", "include": [ "(\\.sh$)" ] } } } ``` +**OPTIONS** + +Please see `arc linters shellcheck` for the list of options supported by this +linter. + **TIPS** As Arcanist currently doesn't detect file formats or doesn't parse shebangs, you can find useful to enforce a convention scripts MUST be appended by an extension like .py .sh in your repository. This may be more convenient for other CI tasks too. That doesn't mean you have to install them with the extension, both cp and install commands are happy to accept an arbitray filename as target, including a target without the extension. **CERDITS** - Joshua Spence: linter development - Sébastien Santoro: library packaging, maintainer **LICENSE** This linter for Arcanist is released under the Apache 2.0 license. diff --git a/lint/linter/ArcanistShellCheckLinter.php b/lint/linter/ArcanistShellCheckLinter.php index 8684b0e..25ed4b2 100644 --- a/lint/linter/ArcanistShellCheckLinter.php +++ b/lint/linter/ArcanistShellCheckLinter.php @@ -1,142 +1,162 @@ array( 'type' => 'optional string', 'help' => pht( 'Specify shell dialect (%s, %s, %s, %s).', 'bash', 'sh', 'ksh', 'zsh'), ), + 'shellcheck.exclude' => array( + 'type' => 'optional list', + 'help' => pht('Specify excluded checks, e.g.: SC2035.'), + ), ); return $options + parent::getLinterConfigurationOptions(); } public function setLinterConfigurationValue($key, $value) { switch ($key) { case 'shellcheck.shell': $this->setShell($value); return; + case 'shellcheck.exclude': + $this->setExclude($value); + return; + default: return parent::setLinterConfigurationValue($key, $value); } } public function setShell($shell) { $this->shell = $shell; return $this; } + public function setExclude($exclude) { + $this->exclude = $exclude; + return $this; + } + public function getDefaultBinary() { return 'shellcheck'; } public function getInstallInstructions() { return pht( 'Install ShellCheck with `%s`.', 'cabal install shellcheck'); } protected function getMandatoryFlags() { $options = array(); $options[] = '--format=checkstyle'; if ($this->shell) { $options[] = '--shell='.$this->shell; } + if ($this->exclude) { + foreach ($this->exclude as $code) { + $options[] = '--exclude='.$code; + } + } + return $options; } public function getVersion() { list($stdout, $stderr) = execx( '%C --version', $this->getExecutableCommand()); $matches = null; if (preg_match('/^version: (\d(?:\.\d){2})$/', $stdout, $matches)) { return $matches[1]; } return null; } protected function parseLinterOutput($path, $err, $stdout, $stderr) { $report_dom = new DOMDocument(); $ok = @$report_dom->loadXML($stdout); if (!$ok) { return false; } $files = $report_dom->getElementsByTagName('file'); $messages = array(); foreach ($files as $file) { foreach ($file->getElementsByTagName('error') as $child) { $code = str_replace('ShellCheck.', '', $child->getAttribute('source')); $message = id(new ArcanistLintMessage()) ->setPath($path) ->setLine($child->getAttribute('line')) ->setChar($child->getAttribute('column')) ->setName($this->getLinterName()) ->setCode($code) ->setDescription($child->getAttribute('message')); switch ($child->getAttribute('severity')) { case 'error': $message->setSeverity(ArcanistLintSeverity::SEVERITY_ERROR); break; case 'warning': $message->setSeverity(ArcanistLintSeverity::SEVERITY_WARNING); break; case 'info': $message->setSeverity(ArcanistLintSeverity::SEVERITY_ADVICE); break; default: $message->setSeverity(ArcanistLintSeverity::SEVERITY_ERROR); break; } $messages[] = $message; } } return $messages; } }