pre-commit-config-shellcheck for syntax and semantic checks automation

The reasoning

Second-checking of an already written code may be an exhausting and unreliable task for most of the programmers involved in custom software development. While refactoring the code a few days ago, we found some mistakes in shell code in the pre-commit config file. These errors have been there for a long time without any warnings. That’s why we thought if these code checks either were more reliable or proceeded automatically. Thereafter, pre-commit-config-shellcheck was created for this very purpose.

What’s the difference

This tool will automatically detect and help you to resolve mistakes in your project’s .pre-commit-config.yaml file. All the syntax and most semantic checks are done for you. Also, you will receive bug fixing recommendations. This is done by automatic check of all the shell code entries in the config file with the other tool—Shellcheck.

How it works

Shellcheck

Shellcheck is a shell script static analysis tool. The main purpose of this tool is to find popular syntax mistakes as well as to point out corner cases that may cause scripts to fail under future circumstances while giving suggestions for fixing those.

File checking

All the checks of this tool are done on the .pre-commit-config.yaml file, but this file can’t be directly analyzed by Shellcheck due to the structure of the file, which includes not only shell code but other configuration points. We found the workaround for this in creating a list of temporary files with found shell entry points and checking them separately.

Subprocesses

The main way of using Shellcheck is through a terminal, and this tool doesn’t have an adaptor for different programming languages, so we use the Python subprocess module in order to perform checks on each temporary file. Then the output from subprocesses is collected and the tool forms a single pre-commit-config-shellcheck output with the list of warnings and their locations.

Example of usage and output:

Easy to use

For the sake of keeping this tool simple for users, there are a few ways to use pre-commit-config-shellcheck:

As a command-line tool

  $ pip install pre-commit-config-shellcheck
  $ pre_commit_config_shellcheck.py .pre-commit-config.yaml

As the pre-commit hook

# .pre-commit-config.yaml
- repo: "https://github.com/Anadea/pre-commit-config-shellcheck"
 rev: "0.3.4"
 hooks:
  - id: "pre-commit-config-shellcheck"

And also as a GitHub action

- name: "pre-commit-config-shellcheck"
 uses: "action/pre-commit-config-shellcheck@0.3.4"
 id: "pre-commit-config-shellcheck"
 with:
  config: ".pre-commit-config.yaml"

Conclusion

Pre-commit-config-shellcheck was successfully created, tested and deployed for third-party usage according to the licensing. This tool, additional information and licensing can be found on GitHub: https://github.com/Anadea/pre-commit-config-shellcheck.

Keep reading

Contact us

Let's explore how our expertise can help you achieve your goals! Drop us a line, and we'll get back to you shortly.