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

How to exclude certain files from yamlfmt? #34

Open
davidxia opened this issue Apr 21, 2022 · 4 comments
Open

How to exclude certain files from yamlfmt? #34

davidxia opened this issue Apr 21, 2022 · 4 comments

Comments

@davidxia
Copy link

My review build runs pre-commit run --all-files. I have some jinja templates that end with extension .yaml. Is there a way to exclude certain files from yamlfmt?

I know I can add an exclude to .pre-commit-config.yaml and change the template's extension. Is there a way to configure yamlfmt itself though?

@dmitry-mightydevops
Copy link

+1, I have run into the same issue. I have some templates with HELM blocks inside files with yaml extension

➜ git commit -m "feat: prepare prod solution"
check for added large files..............................................Passed
check for merge conflicts................................................Passed
forbid new submodules................................(no files to check)Skipped
don't commit to branch...................................................Passed
check for broken symlinks............................(no files to check)Skipped
mixed line ending........................................................Passed
fix end of files.........................................................Passed
trim trailing whitespace.................................................Passed
check for case conflicts.................................................Passed
Format YAML files........................................................Failed
- hook id: yamlfmt
- exit code: 1

staging/ppc/high-priority.yaml  Done
staging/argocd/apps/templates/velero.yamlTraceback (most recent call last):
  File "/home/dmitry/.cache/pre-commit/repotn97rwh1/py_env-python3.9/bin/yamlfmt", line 181, in <module>
    FORMATTER.format(file_name)
  File "/home/dmitry/.cache/pre-commit/repotn97rwh1/py_env-python3.9/bin/yamlfmt", line 137, in format
    FORMATTER.parse_file(path)
  File "/home/dmitry/.cache/pre-commit/repotn97rwh1/py_env-python3.9/bin/yamlfmt", line 147, in parse_file
    self.content = list(self.yaml.load_all(stream))
  File "/home/dmitry/.cache/pre-commit/repotn97rwh1/py_env-python3.9/lib/python3.9/site-packages/ruamel/yaml/main.py", line 461, in load_all
    yield constructor.get_data()
  File "/home/dmitry/.cache/pre-commit/repotn97rwh1/py_env-python3.9/lib/python3.9/site-packages/ruamel/yaml/constructor.py", line 114, in get_data
    return self.construct_document(self.composer.get_node())
  File "/home/dmitry/.cache/pre-commit/repotn97rwh1/py_env-python3.9/lib/python3.9/site-packages/ruamel/yaml/composer.py", line 66, in get_node
    return self.compose_document()
  File "/home/dmitry/.cache/pre-commit/repotn97rwh1/py_env-python3.9/lib/python3.9/site-packages/ruamel/yaml/composer.py", line 99, in compose_document
    node = self.compose_node(None, None)
  File "/home/dmitry/.cache/pre-commit/repotn97rwh1/py_env-python3.9/lib/python3.9/site-packages/ruamel/yaml/composer.py", line 143, in compose_node
    node = self.compose_mapping_node(anchor)
  File "/home/dmitry/.cache/pre-commit/repotn97rwh1/py_env-python3.9/lib/python3.9/site-packages/ruamel/yaml/composer.py", line 218, in compose_mapping_node
    item_key = self.compose_node(node, None)
  File "/home/dmitry/.cache/pre-commit/repotn97rwh1/py_env-python3.9/lib/python3.9/site-packages/ruamel/yaml/composer.py", line 143, in compose_node
    node = self.compose_mapping_node(anchor)
  File "/home/dmitry/.cache/pre-commit/repotn97rwh1/py_env-python3.9/lib/python3.9/site-packages/ruamel/yaml/composer.py", line 216, in compose_mapping_node
    while not self.parser.check_event(MappingEndEvent):
  File "/home/dmitry/.cache/pre-commit/repotn97rwh1/py_env-python3.9/lib/python3.9/site-packages/ruamel/yaml/parser.py", line 146, in check_event
    self.current_event = self.state()
  File "/home/dmitry/.cache/pre-commit/repotn97rwh1/py_env-python3.9/lib/python3.9/site-packages/ruamel/yaml/parser.py", line 743, in parse_flow_mapping_first_key
    return self.parse_flow_mapping_key(first=True)
  File "/home/dmitry/.cache/pre-commit/repotn97rwh1/py_env-python3.9/lib/python3.9/site-packages/ruamel/yaml/parser.py", line 776, in parse_flow_mapping_key
    return self.parse_flow_node()
  File "/home/dmitry/.cache/pre-commit/repotn97rwh1/py_env-python3.9/lib/python3.9/site-packages/ruamel/yaml/parser.py", line 348, in parse_flow_node
    return self.parse_node()
  File "/home/dmitry/.cache/pre-commit/repotn97rwh1/py_env-python3.9/lib/python3.9/site-packages/ruamel/yaml/parser.py", line 509, in parse_node
    raise ParserError(
ruamel.yaml.parser.ParserError: while parsing a flow node
expected the node content, but found '-'
  in "staging/argocd/apps/templates/velero.yaml", line 1, column 3
.pre-commit-config.yaml  Done
staging/coredns/coredns-hpa.yaml  Done

@shellshock1953
Copy link

Seems it should be done using pre-commit level:

- repo: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt
  rev: 0.2.1
  hooks:
  - id: yamlfmt
    args: [--mapping, '2', --sequence, '2', --offset, '0']
    exclude: ^charts/

@vvarga007
Copy link

'exclude' is a regex, so if you want to exclude multiple files, it should be something like this:

  - repo: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt
    rev: 0.2.1
    hooks:
      - id: yamlfmt
        args: [--mapping, '2', --sequence, '4', --offset, '2', --width, '160']
        exclude: (.yamllint|modules/gatus/configs/.*)

@rafaribe
Copy link

Is it possible to include a configuration file?
I've tried this way but the config file is not respected:

    - repo: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt
      rev: 0.2.1 # or other specific tag
      hooks:
          - args:
                - -c
                - .github/linters/.yamlfmt
            id: yamlfmt

houseful-chisel pushed a commit to chizmw/gh-action-sandbox that referenced this issue Nov 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants