Lint shell files with shfmt

This commit is contained in:
Marco Ferrari 2020-08-28 20:42:32 +02:00
parent a29cadb890
commit 0d849cd5e0
9 changed files with 93 additions and 3 deletions

View file

@ -0,0 +1,3 @@
[*.ext]
indent_style = space
indent_size = 4

View file

@ -0,0 +1,19 @@
# SHFMT Test Cases
This folder holds the test cases for **SHFMT**.
## Additional Docs
No Additional information is needed for this test case.
## Good Test Cases
The test cases denoted: `LANGUAGE_good_FILE.EXTENSION` are all valid, and should pass successfully when linted.
- **Note:** They are linted utilizing the default linter rules.
## Bad Test Cases
The test cases denoted: `LANGUAGE_bad_FILE.EXTENSION` are **NOT** valid, and should trigger errors when linted.
- **Note:** They are linted utilizing the default linter rules.

View file

@ -0,0 +1,17 @@
#!/usr/bin/env bash
# CMD
HELLO_WORLD=$(echo "Hello World" | cut -f1 -d' ' 2>&1)
# Load the error code
ERROR_CODE=$?
# Check the shell
if [ ${ERROR_CODE} -ne 0 ]; then
echo "We did it!"
exit 0
else
echo "We done goofed it..."
echo "${HELLO_WORLD}"
exit 1
fi

View file

@ -0,0 +1,17 @@
#!/usr/bin/env bash
# CMD
HELLO_WORLD=$(echo "Hello World" | cut -f1 -d' ' 2>&1)
# Load the error code
ERROR_CODE=$?
# Check the shell
if [ ${ERROR_CODE} -ne 0 ]; then
echo "We did it!"
exit 0
else
echo "We done goofed it..."
echo "${HELLO_WORLD}"
exit 1
fi

View file

@ -281,6 +281,18 @@ RUN R -e "install.packages(list.dirs('/home/r-library',recursive = FALSE), repos
COPY --from=chktex /usr/bin/chktex /usr/bin/ COPY --from=chktex /usr/bin/chktex /usr/bin/
RUN cd ~ && touch .chktexrc RUN cd ~ && touch .chktexrc
#################
# Install shfmt #
#################
ENV GO111MODULE=on
ENV GOROOT=/usr/lib/go
ENV GOPATH=/go
ENV PATH="$PATH":"$GOROOT"/bin
ENV PATH="$PATH":"$GOPATH"/bin
RUN mkdir -p ${GOPATH}/src ${GOPATH}/bin
RUN go get mvdan.cc/sh/v3/cmd/shfmt
########################################### ###########################################
# Load GitHub Env Vars for GitHub Actions # # Load GitHub Env Vars for GitHub Actions #
########################################### ###########################################
@ -345,6 +357,7 @@ ENV ACTIONS_RUNNER_DEBUG=${ACTIONS_RUNNER_DEBUG} \
VALIDATE_R=${VALIDATE_R} \ VALIDATE_R=${VALIDATE_R} \
VALIDATE_RAKU=${VALIDATE_RAKU} \ VALIDATE_RAKU=${VALIDATE_RAKU} \
VALIDATE_RUBY=${VALIDATE_RUBY} \ VALIDATE_RUBY=${VALIDATE_RUBY} \
VALIDATE_SHELL_SHFMT=${VALIDATE_SHELL_SHFMT} \
VALIDATE_STATES=${VALIDATE_STATES} \ VALIDATE_STATES=${VALIDATE_STATES} \
VALIDATE_SQL=${VALIDATE_SQL} \ VALIDATE_SQL=${VALIDATE_SQL} \
VALIDATE_TERRAFORM=${VALIDATE_TERRAFORM} \ VALIDATE_TERRAFORM=${VALIDATE_TERRAFORM} \

View file

@ -73,7 +73,7 @@ Developers on **GitHub** can call the **GitHub Action** to lint their code base
| **R** | [lintr](https://github.com/jimhester/lintr) | | **R** | [lintr](https://github.com/jimhester/lintr) |
| **Raku** | [Raku](https://raku.org) | | **Raku** | [Raku](https://raku.org) |
| **Ruby** | [RuboCop](https://github.com/rubocop-hq/rubocop) | | **Ruby** | [RuboCop](https://github.com/rubocop-hq/rubocop) |
| **Shell** | [Shellcheck](https://github.com/koalaman/shellcheck) / [Bash-exec] | | **Shell** | [Shellcheck](https://github.com/koalaman/shellcheck) / [Bash-exec] / [shfmt](https://github.com/mvdan/sh) |
| **SQL** | [sql-lint](https://github.com/joereynolds/sql-lint) | | **SQL** | [sql-lint](https://github.com/joereynolds/sql-lint) |
| **Terraform** | [tflint](https://github.com/terraform-linters/tflint) / [terrascan](https://github.com/accurics/terrascan) | | **Terraform** | [tflint](https://github.com/terraform-linters/tflint) / [terrascan](https://github.com/accurics/terrascan) |
| **TypeScript** | [eslint](https://eslint.org/) / [standard js](https://standardjs.com/) | | **TypeScript** | [eslint](https://eslint.org/) / [standard js](https://standardjs.com/) |
@ -236,6 +236,7 @@ But if you wish to select or exclude specific linters, we give you full control
| **VALIDATE_R** | `true` | Flag to enable or disable the linting process of the R language. | | **VALIDATE_R** | `true` | Flag to enable or disable the linting process of the R language. |
| **VALIDATE_RAKU** | `true` | Flag to enable or disable the linting process of the Raku language. | | **VALIDATE_RAKU** | `true` | Flag to enable or disable the linting process of the Raku language. |
| **VALIDATE_RUBY** | `true` | Flag to enable or disable the linting process of the Ruby language. | | **VALIDATE_RUBY** | `true` | Flag to enable or disable the linting process of the Ruby language. |
| **VALIDATE_SHELL_SHFMT** | `true` | Flag to enable or disable the linting process of Shell scripts. (Utilizing: shfmt) |
| **VALIDATE_STATES** | `true` | Flag to enable or disable the linting process for AWS States Language. | | **VALIDATE_STATES** | `true` | Flag to enable or disable the linting process for AWS States Language. |
| **VALIDATE_SQL** | `true` | Flag to enable or disable the linting process of the SQL language. | | **VALIDATE_SQL** | `true` | Flag to enable or disable the linting process of the SQL language. |
| **VALIDATE_TERRAFORM** | `true` | Flag to enable or disable the linting process of the Terraform language. | | **VALIDATE_TERRAFORM** | `true` | Flag to enable or disable the linting process of the Terraform language. |

View file

@ -1079,6 +1079,7 @@ AllCops:
## Shell ## Shell
- [Shellcheck](https://github.com/koalaman/shellcheck) - [Shellcheck](https://github.com/koalaman/shellcheck)
- [shfmt](https://github.com/mvdan/sh)
### Shellcheck Config file ### Shellcheck Config file
@ -1110,6 +1111,10 @@ echo "stuff"
moreThings() moreThings()
``` ```
### shfmt Config file
shfmt [supports EditorConfig files for configuration](https://github.com/mvdan/sh#shfmt), if available.
--- ---
## SQL ## SQL

View file

@ -136,7 +136,7 @@ YAML_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${YAML_FILE_NAME}" # Path to the ya
LINTER_ARRAY=('ansible-lint' 'arm-ttk' 'asl-validator' 'bash-exec' 'black' 'cfn-lint' 'checkstyle' 'chktex' 'clj-kondo' 'coffeelint' LINTER_ARRAY=('ansible-lint' 'arm-ttk' 'asl-validator' 'bash-exec' 'black' 'cfn-lint' 'checkstyle' 'chktex' 'clj-kondo' 'coffeelint'
'dotnet-format' 'dart' 'dockerfilelint' 'dotenv-linter' 'editorconfig-checker' 'eslint' 'flake8' 'golangci-lint' 'dotnet-format' 'dart' 'dockerfilelint' 'dotenv-linter' 'editorconfig-checker' 'eslint' 'flake8' 'golangci-lint'
'hadolint' 'htmlhint' 'jsonlint' 'ktlint' 'lintr' 'lua' 'markdownlint' 'npm-groovy-lint' 'perl' 'protolint' 'hadolint' 'htmlhint' 'jsonlint' 'ktlint' 'lintr' 'lua' 'markdownlint' 'npm-groovy-lint' 'perl' 'protolint'
'pwsh' 'pylint' 'raku' 'rubocop' 'shellcheck' 'spectral' 'standard' 'stylelint' 'sql-lint' 'pwsh' 'pylint' 'raku' 'rubocop' 'shellcheck' 'shfmt' 'spectral' 'standard' 'stylelint' 'sql-lint'
'terrascan' 'tflint' 'xmllint' 'yamllint') 'terrascan' 'tflint' 'xmllint' 'yamllint')
############################# #############################
@ -146,7 +146,7 @@ LANGUAGE_ARRAY=('ANSIBLE' 'ARM' 'BASH' 'BASH_EXEC' 'CLOUDFORMATION' 'CLOJURE' 'C
'DART' 'DOCKERFILE' 'DOCKERFILE_HADOLINT' 'EDITORCONFIG' 'ENV' 'GO' 'GROOVY' 'HTML' 'DART' 'DOCKERFILE' 'DOCKERFILE_HADOLINT' 'EDITORCONFIG' 'ENV' 'GO' 'GROOVY' 'HTML'
'JAVA' 'JAVASCRIPT_ES' 'JAVASCRIPT_STANDARD' 'JSON' 'JSX' 'KOTLIN' 'LATEX' 'LUA' 'MARKDOWN' 'JAVA' 'JAVASCRIPT_ES' 'JAVASCRIPT_STANDARD' 'JSON' 'JSX' 'KOTLIN' 'LATEX' 'LUA' 'MARKDOWN'
'OPENAPI' 'PERL' 'PHP_BUILTIN' 'PHP_PHPCS' 'PHP_PHPSTAN' 'PHP_PSALM' 'POWERSHELL' 'OPENAPI' 'PERL' 'PHP_BUILTIN' 'PHP_PHPCS' 'PHP_PHPSTAN' 'PHP_PSALM' 'POWERSHELL'
'PROTOBUF' 'PYTHON_BLACK' 'PYTHON_PYLINT' 'PYTHON_FLAKE8' 'R' 'RAKU' 'RUBY' 'STATES' 'SQL' 'TERRAFORM' 'PROTOBUF' 'PYTHON_BLACK' 'PYTHON_PYLINT' 'PYTHON_FLAKE8' 'R' 'RAKU' 'RUBY' 'SHELL_SHFMT' 'STATES' 'SQL' 'TERRAFORM'
'TERRAFORM_TERRASCAN' 'TSX' 'TYPESCRIPT_ES' 'TYPESCRIPT_STANDARD' 'XML' 'YAML') 'TERRAFORM_TERRASCAN' 'TSX' 'TYPESCRIPT_ES' 'TYPESCRIPT_STANDARD' 'XML' 'YAML')
############################################ ############################################
@ -211,6 +211,7 @@ VALIDATE_R="${VALIDATE_R}" # Boolean t
VALIDATE_RAKU="${VALIDATE_RAKU}" # Boolean to validate language VALIDATE_RAKU="${VALIDATE_RAKU}" # Boolean to validate language
VALIDATE_RUBY="${VALIDATE_RUBY}" # Boolean to validate language VALIDATE_RUBY="${VALIDATE_RUBY}" # Boolean to validate language
VALIDATE_STATES="${VALIDATE_STATES}" # Boolean to validate language VALIDATE_STATES="${VALIDATE_STATES}" # Boolean to validate language
VALIDATE_SHELL_SHFMT="${VALIDATE_SHELL_SHFMT}" # Boolean to check Shell files against editorconfig
VALIDATE_SQL="${VALIDATE_SQL}" # Boolean to validate language VALIDATE_SQL="${VALIDATE_SQL}" # Boolean to validate language
VALIDATE_TERRAFORM="${VALIDATE_TERRAFORM}" # Boolean to validate language VALIDATE_TERRAFORM="${VALIDATE_TERRAFORM}" # Boolean to validate language
VALIDATE_TERRAFORM_TERRASCAN="${VALIDATE_TERRAFORM_TERRASCAN}" # Boolean to validate language VALIDATE_TERRAFORM_TERRASCAN="${VALIDATE_TERRAFORM_TERRASCAN}" # Boolean to validate language
@ -404,6 +405,8 @@ ERRORS_FOUND_RAKU=0 # Count of errors found
export ERRORS_FOUND_RAKU # Workaround SC2034 export ERRORS_FOUND_RAKU # Workaround SC2034
ERRORS_FOUND_RUBY=0 # Count of errors found ERRORS_FOUND_RUBY=0 # Count of errors found
export ERRORS_FOUND_RUBY # Workaround SC2034 export ERRORS_FOUND_RUBY # Workaround SC2034
ERRORS_FOUND_SHELL_SHFMT=0 # Count of errors found
export ERRORS_FOUND_SHELL_SHFMT
ERRORS_FOUND_STATES=0 # Count of errors found ERRORS_FOUND_STATES=0 # Count of errors found
export ERRORS_FOUND_STATES # Workaround SC2034 export ERRORS_FOUND_STATES # Workaround SC2034
ERRORS_FOUND_SQL=0 # Count of errors found ERRORS_FOUND_SQL=0 # Count of errors found
@ -1836,6 +1839,17 @@ if [ "${VALIDATE_RUBY}" == "true" ]; then
LintCodebase "RUBY" "rubocop" "rubocop -c ${RUBY_LINTER_RULES} --force-exclusion" ".*\.\(rb\)\$" "${FILE_ARRAY_RUBY[@]}" LintCodebase "RUBY" "rubocop" "rubocop -c ${RUBY_LINTER_RULES} --force-exclusion" ".*\.\(rb\)\$" "${FILE_ARRAY_RUBY[@]}"
fi fi
#################
# SHFMT LINTING #
#################
if [ "${VALIDATE_SHELL_SHFMT}" == "true" ]; then
####################################
# Lint the files with shfmt #
####################################
# LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY"
LintCodebase "SHELL_SHFMT" "shfmt" "shfmt -d" ".*\.\(sh\|bash\|dash\|ksh\)\$" "${FILE_ARRAY_BASH[@]}"
fi
###################### ######################
# AWS STATES LINTING # # AWS STATES LINTING #
###################### ######################

View file

@ -679,6 +679,7 @@ function RunTestCases() {
TestCodebase "R" "lintr" "lintr::lint()" ".*\.\(r\|rmd\)\$" "r" TestCodebase "R" "lintr" "lintr::lint()" ".*\.\(r\|rmd\)\$" "r"
TestCodebase "RAKU" "raku" "raku -c" ".*\.\(raku\|rakumod\|rakutest\|pm6\|pl6\|p6\)\$" "raku" TestCodebase "RAKU" "raku" "raku -c" ".*\.\(raku\|rakumod\|rakutest\|pm6\|pl6\|p6\)\$" "raku"
TestCodebase "RUBY" "rubocop" "rubocop -c ${RUBY_LINTER_RULES}" ".*\.\(rb\)\$" "ruby" TestCodebase "RUBY" "rubocop" "rubocop -c ${RUBY_LINTER_RULES}" ".*\.\(rb\)\$" "ruby"
TestCodebase "SHFMT" "shfmt" "shfmt -d" ".*\.\(sh\|bash\|dash\|ksh\)\$" "shell"
TestCodebase "STATES" "asl-validator" "asl-validator --json-path" ".*\.\(json\)\$" "states" TestCodebase "STATES" "asl-validator" "asl-validator --json-path" ".*\.\(json\)\$" "states"
TestCodebase "SQL" "sql-lint" "sql-lint --config ${SQL_LINTER_RULES}" ".*\.\(sql\)\$" "sql" TestCodebase "SQL" "sql-lint" "sql-lint --config ${SQL_LINTER_RULES}" ".*\.\(sql\)\$" "sql"
TestCodebase "TERRAFORM" "tflint" "tflint -c ${TERRAFORM_LINTER_RULES}" ".*\.\(tf\)\$" "terraform" TestCodebase "TERRAFORM" "tflint" "tflint -c ${TERRAFORM_LINTER_RULES}" ".*\.\(tf\)\$" "terraform"