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

Better YAML #58

Open
abitrolly opened this issue Jan 20, 2020 · 0 comments
Open

Better YAML #58

abitrolly opened this issue Jan 20, 2020 · 0 comments

Comments

@abitrolly
Copy link
Member

abitrolly commented Jan 20, 2020

YAML is not always readable. Here is an example of build config from Travis that failed, because of YAML parsing.

script: 
  - echo "default shell: $SHELL"

To see what's wrong, let's use https://yaml-online-parser.appspot.com/ to convert it to Python.

>>> a = {'script': [{'echo "default shell': '$SHELL"'}]}

The logical way to get the first command to be executed it to get the first element from the script: array.

>>> a['script'][0]
{'echo "default shell': '$SHELL"'}

But wait, it is a dict. Should it be the string?

>>> b['script'][0]
'echo "default shell: $SHELL"'

The correct YAML for b needs the whole line to be quoted.

script: 
  - 'echo "default shell: $SHELL"'

Otherwise YAML splits the command on the first found colon :. It doesn't understand internal " quoting.

Solution

"Better" YAML should not allow any spaces in dict keys and spaces between the key and the colon.

echo: hello

then will still be equal to

{'echo': 'hello'}

But these will be just plain strings.

echo  :   hello
echo "say: hello"

Maybe HashiCorp HCL was born to be the Better YAML because of this gotcha.

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

1 participant