feat: add config option to set allowed literal bools (#225)

This commit is contained in:
Robert Kaussow 2021-10-07 22:59:39 +02:00 committed by GitHub
parent 0b625d3bb3
commit c5f4f2212f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 7 deletions

View File

@ -6,19 +6,21 @@ from ansiblelater.standard import StandardBase
class CheckLiteralBoolFormat(StandardBase):
sid = "ANSIBLE0014"
description = "Literal bools should start with a capital letter"
helptext = "literal bools should be written as `True/False` or `yes/no`"
description = "Literal bools should be consistent"
helptext = "literal bools should be written as `{bools}`"
version = "0.1"
types = ["playbook", "task", "handler", "rolevars", "hostvars", "groupvars"]
def check(self, candidate, settings):
yamllines, errors = self.get_normalized_yaml(candidate, settings)
uppercase_bool = re.compile(r"([=!]=|:)\s*(true|false|TRUE|FALSE|Yes|No|YES|NO)\s*$")
litera_bools = re.compile(r"(?:[=!]=|:)\s*(true|false|yes|no|on|off)\s*$", re.IGNORECASE)
allowed = settings["ansible"]["literal-bools"]
if not errors:
for i, line in yamllines:
if uppercase_bool.findall(line):
errors.append(self.Error(i, self.helptext))
matches = litera_bools.findall(line)
if any(m not in allowed for m in matches):
errors.append(self.Error(i, self.helptext.format(bools=", ".join(allowed))))
return self.Result(candidate.path, errors)

View File

@ -134,6 +134,7 @@ class Settings(object):
"min-spaces-inside": 1,
"max-spaces-inside": 1,
},
"literal-bools": ["True", "False", "yes", "no"],
},
"yamllint": {
"empty-lines": {

View File

@ -19,6 +19,13 @@ ansible:
max-spaces-inside: 1
min-spaces-inside: 1
# List of allowed literal bools (ANSIBLE0014)
literal-bools:
- "True"
- "False"
- "yes"
- "no"
# Global logging configuration
# If you would like to force colored output (e.g. non-tty)
# set environment variable `PY_COLORS=1`

View File

@ -18,7 +18,7 @@ Reviews are nothing without some rules or standards against which to review. ans
| CheckLineBetweenTasks | ANSIBLE0001 | Single tasks should be separated by an empty line. | |
| CheckMetaMain | ANSIBLE0002 | Meta file should contain a basic subset of parameters. | author, description, min_ansible_version, platforms, dependencies |
| CheckUniqueNamedTask | ANSIBLE0003 | Tasks and handlers must be uniquely named within a file. | |
| CheckBraces | ANSIBLE0004 | YAML should use consistent number of spaces around variables. | |
| CheckBraces | ANSIBLE0004 | YAML should use consistent number of spaces around variables. | {double-braces: max-spaces-inside: 1, min-spaces-inside: 1} |
| CheckScmInSrc | ANSIBLE0005 | Use SCM key rather than `src: scm+url` in requirements file. | |
| CheckNamedTask | ANSIBLE0006 | Tasks and handlers must be named. | excludes: meta, debug, include\_\*, import\_\*, block |
| CheckNameFormat | ANSIBLE0007 | Name of tasks and handlers must be formatted. | formats: first letter capital |
@ -28,7 +28,7 @@ Reviews are nothing without some rules or standards against which to review. ans
| CheckCommandHasChanges | ANSIBLE0011 | Commands should be idempotent and only used with some checks. | |
| CheckCompareToEmptyString | ANSIBLE0012 | Don't compare to "" - use `when: var` or `when: not var`. | |
| CheckCompareToLiteralBool | ANSIBLE0013 | Don't compare to True/False - use `when: var` or `when: not var`. | |
| CheckLiteralBoolFormat | ANSIBLE0014 | Literal bools should be written as `True/False` or `yes/no`. | forbidden values are `true false TRUE FALSE Yes No YES NO` |
| CheckLiteralBoolFormat | ANSIBLE0014 | Literal bools should be consistent. | {literal-bools: [True, False, yes, no]} |
| CheckBecomeUser | ANSIBLE0015 | Become should be combined with become_user. | |
| CheckFilterSeparation | ANSIBLE0016 | Jinja2 filters should be separated with spaces. | |
| CheckCommandInsteadOfArgument | ANSIBLE0017 | Commands should not be used in place of module arguments. | |