Skip to content

Commit

Permalink
Allow formatting xml files when cli_lint_mode is file
Browse files Browse the repository at this point in the history
  • Loading branch information
bdovaz committed Jan 10, 2023
1 parent 1d74fa0 commit 97e27bf
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
File renamed without changes.
6 changes: 6 additions & 0 deletions .automation/test/xml/bad/xml_bad_2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<note>
<to>Tove</to>
<from>Jani<from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
<note>
File renamed without changes.
6 changes: 6 additions & 0 deletions .automation/test/xml/good/xml_good_2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
12 changes: 11 additions & 1 deletion megalinter/descriptors/xml.megalinter-descriptor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@ file_extensions:
- ".xml"
linters:
# XML LINT
- linter_name: xmllint
- class: XmlLintLinter
linter_name: xmllint
variables:
- name: XML_XMLLINT_AUTOFORMAT
default_value: "false"
description: If set to `true`, it will reformat and reindent the output
- name: XML_XMLLINT_INDENT
default_value: " "
description: The number of indentation spaces when `XML_XMLLINT_AUTOFORMAT` is `true`
linter_url: http://xmlsoft.org/xmllint.html
linter_rules_url: http://xmlsoft.org/xmllint.html#diagnostics
version_extract_regex: "(?<=libxml version )\\d+(\\d+)+"
cli_lint_mode: list_of_files
cli_lint_fix_arg_name: "--format"
cli_help_arg_name: "--help"
help_command_return_code: 1
examples:
- "xmllint myfile.xml"
- "xmllint --format myfile.xml --output myfile.xml"
install:
apk:
- libc-dev
Expand Down
29 changes: 29 additions & 0 deletions megalinter/linters/XmlLintLinter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python3
"""
Use XmlLint to lint xml files
http://xmlsoft.org/xmllint.html
"""
import os

from megalinter import Linter, config


class XmlLintLinter(Linter):
# Build the CLI command to call to lint a file
def build_lint_command(self, file=None):
cmd = super().build_lint_command(file)

if (
self.apply_fixes is True
and self.cli_lint_fix_arg_name is not None
and config.get("XML_XMLLINT_AUTOFORMAT", "false") == "true"
):
if self.cli_lint_mode == "file":
os.environ["XMLLINT_INDENT"] = config.get("XML_XMLLINT_INDENT", " ")

cmd += f" {self.cli_lint_fix_arg_name} --output {file}"
else:
raise KeyError(
f"You can not apply_fixes with cli_lint_mode {self.cli_lint_mode}"
)
return cmd

0 comments on commit 97e27bf

Please sign in to comment.