How can I include a multiline string in `extra_env...
# general
s
How can I include a multiline string in
extra_env_vars
in
pants.toml
that includes
==
(e.g public and private keys) I can add multiline values with
'''
or
"""
but if I add one that contains (or maybe just ends) in
==
then I get the following errors (see thread)
Untitled.txt
I've tried adding single quotes around the values inside the multi-quoted blocks and that doesn't work either
h
What's an example of a toml stanza that fails?
s
Copy code
extra_env_vars = [
  "ENVIRONMENT=test",
  '''JWT_SECRET=-----BEGIN RSA PRIVATE KEY-----
SOME KEY TEXT==
-----END RSA PRIVATE KEY-----''',
  '''JWT_PUBLIC=-----BEGIN PUBLIC KEY-----
SOME KEY TEXT==
-----END PUBLIC KEY-----''']
c
OK, there’ll be two issues here.. first getting it passed the toml parser. Then, the environment vars don’t expect multiline strings, and will only use the first line..
Copy code
In [1]: import re

In [2]: r = re.compile(r"([A-Za-z_]\w*)=(.*)")

In [3]: var = '''JWT_SECRET=-----BEGIN RSA PRIVATE KEY-----
   ...: SOME KEY TEXT==
   ...: -----END RSA PRIVATE KEY-----'''

In [4]: var
Out[4]: 'JWT_SECRET=-----BEGIN RSA PRIVATE KEY-----\nSOME KEY TEXT==\n-----END RSA PRIVATE KEY-----'

In [5]: print(var)
JWT_SECRET=-----BEGIN RSA PRIVATE KEY-----
SOME KEY TEXT==
-----END RSA PRIVATE KEY-----

In [6]: r.match(var)
Out[6]: <re.Match object; span=(0, 42), match='JWT_SECRET=-----BEGIN RSA PRIVATE KEY-----'>

In [7]: _6[1]
Out[7]: 'JWT_SECRET'

In [8]: _6[2]
Out[8]: '-----BEGIN RSA PRIVATE KEY-----'

In [9]:
Regexp from: https://github.com/pantsbuild/pants/blob/69fdaaf86a191e1c3fe2fef9d9532b2665070d16/src/python/pants/engine/environment.py#L17 Used here: https://github.com/pantsbuild/pants/blob/69fdaaf86a191e1c3fe2fef9d9532b2665070d16/src/python/pants/engine/environment.py#L65-L67
Copy code
name_value_match = name_value_re.match(env_var)
            if name_value_match:
                check_and_set(name_value_match[1], name_value_match[2])
I’d try with only having the name in the
pants.toml
file, and define the env var in the env invoking pants. That way, the value will be picked up as defined. Does that make sense, and is it a workable approach for your use case @stale-nightfall-29801 ?
s
Thanks Andreas, that's actually preferable over putting it in the
pants.toml
as the test environment already has them defined
👍 3