thesimj / envyaml

Simple YAML configuration file parser
MIT License
78 stars 21 forks source link

Handling multi-line env #30

Open vbr-cts opened 2 years ago

vbr-cts commented 2 years ago

Hi. I'm trying to find a way to handle multi-line env variables. Basically, I have a pkcs certificate that I need to get from env through EnvYaml.

Simple test case:

user$ export VAR=$(printf "foo\nbar")
user$ echo "$VAR"
foo
bar
user$ cat a.yaml
foo: "$VAR"

Then in python:

os.environ['VAR']
'foo\nbar'
>>> envyaml.EnvYAML('a.yaml')['foo']
'foo bar'

As you can see, the line break gets stripped. This is because by default, line breaks are stripped from quoted strings in yaml. Using a multi-line block with > or | doesn't work because we break indentation.

One way that works is to force replace all line breaks in the value with \n (or two line breaks).

This way, my yaml ends up looking like this, and parsing works:

foo: "a\nb"

I guess an even cleaner way would be to completely escape strings before doing the substitution ?