Skip to content

Commit

Permalink
Merge pull request #2 from nicholasmhughes/valueregex-add-configurabl…
Browse files Browse the repository at this point in the history
…e-error-msg

add configurable error message
  • Loading branch information
nicholasmhughes authored Feb 3, 2023
2 parents 321bd88 + ff547b5 commit 2110e35
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.3.0
rev: v4.4.0
hooks:
- id: debug-statements
- id: check-ast
Expand All @@ -16,7 +16,7 @@ repos:
- id: check-merge-conflict

- repo: https://github.com/psf/black
rev: 20.8b1
rev: 22.12.0
hooks:
- id: black
language_version: python3
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ Add this to your `.pre-commit-config.yaml`
```yaml
repos:
- repo: https://github.com/eitrtechnologies/pre-commit-yamlpolicy
rev: v1.2.0 # Use the ref you want to point to
rev: v1.3.0 # Use the ref you want to point to
hooks:
- id: bannedk8skinds
- id: disallowunquoted
- id: valueregex
args: [--jmespath, '*.matchers[].match', --regex, '\([^ ]|[^ ]\)']
args:
- --jmespath
- '*.matchers[].match'
- --regex
- '\([^ ]|[^ ]\)'
- --error-message
- Found parentheses too close together. Can haz fix plz?
```
### Hooks Available
Expand Down Expand Up @@ -44,5 +50,7 @@ values in YAML.
the values to run a regex against. *REQUIRED*
- `--regex` - Regex which will cause the hook to fail if it matches any of the
values returned by the JMESPath query. *REQUIRED*
- `--error-message` - Message to display when a match is found. This allows
a more user-friendly message to be displayed for a given regex match.
- `--allow-multiple-documents` - Allow YAML files which use the
[multi-document syntax](http://www.yaml.org/spec/1.2/spec.html#YAML)
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = yamlpolicy
version = 1.2.0
version = 1.3.0
description = Allows an organization to specify YAML usage policy.
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down
10 changes: 9 additions & 1 deletion valueregex/valueregex.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
"--jmespath",
required=True,
)
optional.add_argument(
"-e",
"--error-message",
)
required.add_argument("filenames", nargs="*", help="Filenames to check.")
args: argparse.Namespace = parser.parse_args(argv)

Expand All @@ -43,6 +47,10 @@ def main(argv: Optional[Sequence[str]] = None) -> int:

retval: int = 0
for filename in args.filenames:
error_message: str = f'Restricted value found for JMESPath "{search}"'
if args.error_message:
error_message = args.error_message

try:
with open(filename, encoding="UTF-8") as f:
if args.multi:
Expand All @@ -54,7 +62,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
match: re.Match = regex.search(val)
if match:
print(
f'{filename}: Restricted value found for JMESPath "{search}" = {match.group(0).rstrip()}'
f"{filename}: {error_message} = {match.group(0).rstrip()}"
)
retval = 1
except ruamel.yaml.YAMLError as exc:
Expand Down

0 comments on commit 2110e35

Please sign in to comment.