Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Files() runner method to Server #1132

Merged
merged 1 commit into from
Jun 16, 2021

Conversation

jonathansp
Copy link
Contributor

@jonathansp jonathansp commented Jun 11, 2021

Abstract

This PR exposes Files() method to tflint-plugin-sdk. Current implementation already has the File(filename string) method exposed.

Rationale

Linting is the process of spotting potential errors. These errors can cause problems not only during the code execution but also to the business logic. Clean and organised files are often mandatory to maintainability. Some rules, however, make sense to one specific project or company. tflint-plugin-sdk helps to extend tflint in a way where each company can implement their own set of rules.

Use cases

This PR exposes the Files() method in order to allow the developer to create more low-level/business-specific rules. A few examples are:

  • Checks if the body of a provider is empty, where the company does not allow it.
  • Keeps blocks in alphabetical order, when needed, for cleansing aspects.
  • Helps to stick to business-defined naming conventions.
  • Helps to block cross-environment actions, dev to prod and vice-versa.

Arguably, some of these rules can be achieved in other ways, but once we can extend tflinf in plugins, I think its utilisation is correct in these cases. This PR depends on https://github.com/terraform-linters/tflint-plugin-sdk/pull/122/files already merged.

Closes:

Any improvement that you guys think to this PR, I will be happy to change.

@bendrucker
Copy link
Member

Have a look at the failing checks. Also, a PR description indicating how you plan to use this would be helpful.

@jonathansp
Copy link
Contributor Author

cc @wata727

@jonathansp
Copy link
Contributor Author

Have a look at the failing checks. Also, a PR description indicating how you plan to use this would be helpful.

PR description updated.

@wata727 wata727 merged commit 25546f1 into terraform-linters:master Jun 16, 2021
@wata727
Copy link
Member

wata727 commented Jun 16, 2021

👍

@jonathansp
Copy link
Contributor Author

Thanks @wata727

Now we can write rules like:

func (rule *MyProjectOnlyAllowedFilesRule) Check(runner tflint.Runner) error {
	files, _ := runner.Files()
	allowedFiles := map[string]bool{"providers.tf": true, "main.tf": true}

	for name := range files {
		_, filename := path.Split(name)

		if _, exists := allowedFiles[filename]; !exists {
			message := fmt.Sprintf("File %s is not allowed here.", filename)

			return runner.EmitIssue(rule, message, hcl.Range{Start: hcl.InitialPos})
		}
	}
	return nil
}

will output:

Error: File backend.tf is not allowed here. (my_project_only_allowed_files_rule)

  on  line 1:
   (source code not available)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants