Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline commenting in YAML files

Tags:

yaml

I'm using the following linter to check if my yml is valid: http://www.yamllint.com/

I tried putting inline comments, but they are removed. I read the spec here http://www.yaml.org/spec/1.2/spec.html#id2780069

Am I correct that the following is actually valid and that the linting website is wrong by removing the comments?

cache: 
  paths: 
    - node_modules/ # some comment here
like image 224
basickarl Avatar asked Jun 16 '17 07:06

basickarl


1 Answers

Your source is correct. If you want to run such a check with preservation of the comments, or reformat preserving the comments, then use a small Python program based on ruamel.yaml, which can preserve your comments on round-trip and normalize the indentation (disclaimer: I am author of ruamel.yaml):

import sys
from ruamel.yaml import YAML
from ruamel.yaml.util import load_yaml_guess_indent
    
with open(sys.argv[1]) as fp:
    data, ind, offset = load_yaml_guess_indent(fp)
yaml = YAML()
yaml.indent(mapping=ind, sequence=ind, offset=offset)
yaml.dump(data, sys.stdout)

just provide the input file as parameter on the commandline.

This has the advantage over all web based checkers that your possible sensitive data doesn't get "published". It also has the advantage over yamllint.com and some other sites, that it supports YAML 1.2. yamllint.com only supports YAML 1.1, what you can see if you try it with an explicit YAML document directive:

%YAML 1.2
--- 
a: 0o7
...

That site throws an error that that version is not supported. Which is better than what e.g. http://yaml-online-parser.appspot.com/ does (directive ignored, parsed as if YAML 1.1, with the octal integer scalar as if it where a string scalar) or the half-fledged implementation of YAML at http://beautifytools.com/yaml-validator.php (Error: unable to parse))

like image 108
Anthon Avatar answered Oct 11 '22 06:10

Anthon