naivedeveloper
naivedeveloper

Reputation: 2958

How to override array in Typesafe config with environment variables?

I've been using the environment variable substitution feature in Typesafe config:

foo = "foo"
foo = ${?FOO}

This results in a "default" value of "foo" if there is no environment variable named FOO. In this case, the second value declaration (foo = ${?FOO} is simply discarded). However, if a variable named FOO exists, the library will "substitute" the value of FOO and assign it to foo.

I would like similar behavior with arrays, but unfortunately, this does not work as expected:

foo = [ "1", "2" ]
foo = [ ${?f1}, ${?f2} ]

In the case where f1 and f2 are not defined, this simply results in foo being an empty array. My goal is to have a similar effect as above (discard the second foo if no environment variables f1 and f2 are defined). Any ideas/suggestions are appreciated. Thanks.

Upvotes: 10

Views: 3085

Answers (2)

Atais
Atais

Reputation: 11275

Its 2024 and still the best solution I have found was posted in https://github.com/lightbend/config/issues/163

export FOO="abc,def"
foo="bar"
foo=${?FOO}

split(',') to list.

:(

Upvotes: 0

PAStheLoD
PAStheLoD

Reputation: 995

I found that using a = ${?VARNAME} in the HOCON config file with the Scala code expecting a List, and using -DVARNAME.0=something (or simply VARNAME.0=something) will result in the correct ["something"] value. (Tested with Play 2.6.13 and the AllowedHosts filter.)

Upvotes: 8

Related Questions