From b03c9900458cc59a327a3e5acd46887c9ff85acd Mon Sep 17 00:00:00 2001 From: nvuillam Date: Tue, 25 Jan 2022 08:34:17 +0100 Subject: [PATCH 1/3] Fix tflint initialization Fix tflint initialization so it uses configuration file when defined ([#1134](https://github.com/megalinter/megalinter/issues/1134)) --- CHANGELOG.md | 3 +- .../terraform.megalinter-descriptor.yml | 6 ++-- megalinter/linters/TfLintLinter.py | 32 +++++++++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 megalinter/linters/TfLintLinter.py diff --git a/CHANGELOG.md b/CHANGELOG.md index dc016637e64..acf6f6ac6bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), Note: Can be used with `megalinter/megalinter@beta` in your GitHub Action mega-linter.yml file, or with `megalinter/megalinter:beta` docker image - New reporter **GITLAB_COMMENT_REPORTER** allowing to post MegaLinter results as comments on Gitlab merge requests -- Fix v5 doc deployment when there is a new release ([1190](https://github.com/megalinter/megalinter/issues/1190)) +- Fix v5 doc deployment when there is a new release ([#1190](https://github.com/megalinter/megalinter/issues/1190)) +- Fix tflint initialization so it uses configuration file when defined ([#1134](https://github.com/megalinter/megalinter/issues/1134)) - Linter versions upgrades - [stylelint](https://stylelint.io) from 14.2.0 to **14.3.0** on 2022-01-23 diff --git a/megalinter/descriptors/terraform.megalinter-descriptor.yml b/megalinter/descriptors/terraform.megalinter-descriptor.yml index 52d721fe803..c4b7fa57adb 100644 --- a/megalinter/descriptors/terraform.megalinter-descriptor.yml +++ b/megalinter/descriptors/terraform.megalinter-descriptor.yml @@ -6,16 +6,14 @@ file_extensions: - ".tf" linters: # TFLINT - - linter_name: tflint + - class: TfLintLinter + linter_name: tflint name: TERRAFORM_TFLINT linter_url: https://github.com/terraform-linters/tflint linter_rules_url: https://github.com/terraform-linters/tflint/tree/master/docs/rules#rules linter_rules_configuration_url: https://github.com/terraform-linters/tflint/blob/master/docs/guides/config.md linter_rules_inline_disable_url: https://github.com/terraform-linters/tflint/blob/master/docs/guides/annotations.md config_file_name: .tflint.hcl - pre_commands: - - command: tflint --init - cwd: workspace examples: - "tflint myfile.tf" - "tflint -c .tflint.hcl myfile.tf" diff --git a/megalinter/linters/TfLintLinter.py b/megalinter/linters/TfLintLinter.py new file mode 100644 index 00000000000..dcd86ba7a53 --- /dev/null +++ b/megalinter/linters/TfLintLinter.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +""" +Use TfLint to lint terraform files +https://github.com/terraform-linters/tflint +""" +import logging +import os +import shutil +import subprocess +import sys + +import megalinter + + +class TfLintLinter(megalinter.Linter): + + # To execute before linting files + def before_lint_files(self): + # Build pre-command + tflint_init_command = 'tflint --init' + if self.config_file is not None: + tflint_init_command += f" --config {self.config_file}" + logging.debug("tflint before_lint_files: " + tflint_init_command) + # Add to pre-commands + tflint_pre_command = { + "command": tflint_init_command, + "cwd": self.workspace + } + if self.pre_commands is None: + self.pre_commands = [] + self.pre_commands.append(tflint_pre_command) + From e3179906c4b72a07f7f544327e7e159c9e899ad6 Mon Sep 17 00:00:00 2001 From: nvuillam Date: Tue, 25 Jan 2022 08:36:52 +0100 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index acf6f6ac6bf..1a65f6d9b55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Note: Can be used with `megalinter/megalinter@beta` in your GitHub Action mega-l - New reporter **GITLAB_COMMENT_REPORTER** allowing to post MegaLinter results as comments on Gitlab merge requests - Fix v5 doc deployment when there is a new release ([#1190](https://github.com/megalinter/megalinter/issues/1190)) +- Add configuration file option for SQLFluff ([#1200](https://github.com/megalinter/megalinter/pull/1200)) - Fix tflint initialization so it uses configuration file when defined ([#1134](https://github.com/megalinter/megalinter/issues/1134)) - Linter versions upgrades From 6fefb14d995cffe888f41aa47058c727ee59bd29 Mon Sep 17 00:00:00 2001 From: nvuillam Date: Thu, 27 Jan 2022 00:14:02 +0100 Subject: [PATCH 3/3] Fix python lint errors --- megalinter/linters/TfLintLinter.py | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/megalinter/linters/TfLintLinter.py b/megalinter/linters/TfLintLinter.py index dcd86ba7a53..e8abb208b63 100644 --- a/megalinter/linters/TfLintLinter.py +++ b/megalinter/linters/TfLintLinter.py @@ -4,10 +4,6 @@ https://github.com/terraform-linters/tflint """ import logging -import os -import shutil -import subprocess -import sys import megalinter @@ -17,16 +13,12 @@ class TfLintLinter(megalinter.Linter): # To execute before linting files def before_lint_files(self): # Build pre-command - tflint_init_command = 'tflint --init' + tflint_init_command = "tflint --init" if self.config_file is not None: - tflint_init_command += f" --config {self.config_file}" + tflint_init_command += f" --config {self.config_file}" logging.debug("tflint before_lint_files: " + tflint_init_command) # Add to pre-commands - tflint_pre_command = { - "command": tflint_init_command, - "cwd": self.workspace - } + tflint_pre_command = {"command": tflint_init_command, "cwd": self.workspace} if self.pre_commands is None: self.pre_commands = [] self.pre_commands.append(tflint_pre_command) -