Support ignoring cloud-init #cloud-config directives

pull/417/head
Marco Ferrari 4 years ago
parent f2e2e0c366
commit 3a767f9ed4

@ -80,6 +80,23 @@ class CommentsTestCase(RuleTestCase):
problem3=(9, 2), problem4=(10, 4), problem3=(9, 2), problem4=(10, 4),
problem5=(15, 3)) problem5=(15, 3))
def test_cloud_init_cloud_config(self):
conf = ('comments:\n'
' require-starting-space: true\n'
' ignore-cloud-init-cloud-config: false\n'
'document-start: disable\n')
self.check('#cloud-config\n',
conf, problem1=(1, 2))
self.check('# cloud-config\n', conf)
def test_ignore_cloud_init_cloud_config(self):
conf = ('comments:\n'
' require-starting-space: true\n'
' ignore-cloud-init-cloud-config: true\n'
'document-start: disable\n')
self.check('#cloud-config\n', conf)
self.check('# cloud-config\n', conf)
def test_shebang(self): def test_shebang(self):
conf = ('comments:\n' conf = ('comments:\n'
' require-starting-space: true\n' ' require-starting-space: true\n'

@ -19,8 +19,15 @@ Use this rule to control the position and formatting of comments.
.. rubric:: Options .. rubric:: Options
# cloud-init expects the first line to be #cloud-config and this conflicts with
# the rules.comments.require-starting-space directive.
# See https://yamllint.readthedocs.io/en/stable/rules.html#module-yamllint.rules.comments
* Use ``require-starting-space`` to require a space character right after the * Use ``require-starting-space`` to require a space character right after the
``#``. Set to ``true`` to enable, ``false`` to disable. ``#``. Set to ``true`` to enable, ``false`` to disable.
* Use ``ignore-cloud-init-cloud-config`` to ignore a
`#cloud-config cloud-init directive <https://cloudinit.readthedocs.io/en/latest/topics/format.html#cloud-config-data>`_ at the beginning of
the file when ``require-starting-space`` is set.
* Use ``ignore-shebangs`` to ignore a * Use ``ignore-shebangs`` to ignore a
`shebang <https://en.wikipedia.org/wiki/Shebang_(Unix)>`_ at the beginning of `shebang <https://en.wikipedia.org/wiki/Shebang_(Unix)>`_ at the beginning of
the file when ``require-starting-space`` is set. the file when ``require-starting-space`` is set.
@ -82,9 +89,11 @@ from yamllint.linter import LintProblem
ID = 'comments' ID = 'comments'
TYPE = 'comment' TYPE = 'comment'
CONF = {'require-starting-space': bool, CONF = {'require-starting-space': bool,
'ignore-cloud-init-cloud-config': bool,
'ignore-shebangs': bool, 'ignore-shebangs': bool,
'min-spaces-from-content': int} 'min-spaces-from-content': int}
DEFAULT = {'require-starting-space': True, DEFAULT = {'require-starting-space': True,
'ignore-cloud-init-cloud-config': True,
'ignore-shebangs': True, 'ignore-shebangs': True,
'min-spaces-from-content': 2} 'min-spaces-from-content': 2}
@ -107,6 +116,11 @@ def check(conf, comment):
comment.column_no == 1 and comment.column_no == 1 and
re.match(r'^!\S', comment.buffer[text_start:])): re.match(r'^!\S', comment.buffer[text_start:])):
return return
elif (conf['ignore-cloud-init-cloud-config'] and
comment.line_no == 1 and
comment.column_no == 1 and
re.match(r'^cloud-config', comment.buffer[text_start:])):
return
# We can test for both \r and \r\n just by checking first char # We can test for both \r and \r\n just by checking first char
# \r itself is a valid newline on some older OS. # \r itself is a valid newline on some older OS.
elif comment.buffer[text_start] not in {' ', '\n', '\r', '\x00'}: elif comment.buffer[text_start] not in {' ', '\n', '\r', '\x00'}:

Loading…
Cancel
Save