https://kotlinlang.org logo
Title
d

dave08

03/28/2022, 1:27 PM
@sam I'm trying to use env vars for setting configs in intellij's run configs, but for some reason hoplite isn't picking them up (the code is in the thread
data class Settings(
    val bucketName: String,
    val endpoint: String,
    val accessKey: String,
    val secretKey: String,
)

val config by lazy {
    ConfigLoaderBuilder.default()
        .addPropertySource(EnvironmentVariablesPropertySource(false, false))
        .build()
        .loadConfigOrThrow<Settings>()
}
Trying to use something like
settings.bucketName
for env var name...
But I get:
Caused by: com.sksamuel.hoplite.ConfigException: Error loading config because:

    - Could not instantiate 'com.lomdaat.plugins.Settings' because:

        - 'bucketName': Missing from config

        - 'endpoint': Missing from config

        - 'accessKey': Missing from config

        - 'secretKey': Missing from config
Is there any way of printing out what keys hoplite actually "sees"?
Oh...
settings.
shouldn't be there... which is pretty inconsistent with the yaml format, no?
s

sam

03/28/2022, 1:37 PM
If you're on 2.0.x then you can add .report
d

dave08

03/28/2022, 1:38 PM
.report
? Doesn't seem documented in the README?
Ok, but in a scratch file it doesn't seem to work... I'm trying to use System.setProperty(...,...) to provide the values in the scratch file, but it seems to crash with the previous error and .report() doesn't give me anything...
s

sam

03/28/2022, 1:46 PM
Hmm maybe I didn't update docs for report on the latest 2.0
ConfigLoaderBuilder.default().report...
d

dave08

03/28/2022, 1:47 PM
I did that:
val config by lazy {
    ConfigLoaderBuilder.default().report()
        .addPropertySource(EnvironmentVariablesPropertySource(false, false))
        .build()
        .loadConfigOrThrow<Settings>()
}
s

sam

03/28/2022, 1:47 PM
There you go
If you want to use env you can override in the field d. That's in the docs
d

dave08

03/28/2022, 1:48 PM
I got this:
Exception in thread "main" java.lang.ExceptionInInitializerError
	at org.jetbrains.kotlin.idea.scratch.generated.ScratchFileRunnerGenerated$ScratchFileRunnerGenerated.<init>(tmp.kt:13)
	at org.jetbrains.kotlin.idea.scratch.generated.ScratchFileRunnerGenerated.main(tmp.kt:19)
Caused by: com.sksamuel.hoplite.ConfigException: Error loading config because:

    - Could not instantiate 'com.lomdaat.plugins.Settings' because:

        - 'bucketName': Missing from config

        - 'endpoint': Missing from config

        - 'accessKey': Missing from config

        - 'secretKey': Missing from config
	at com.sksamuel.hoplite.ConfigLoader$returnOrThrow$1.invoke(ConfigLoader.kt:186)
	at com.sksamuel.hoplite.ConfigLoader$returnOrThrow$1.invoke(ConfigLoader.kt:183)
	at com.sksamuel.hoplite.fp.ValidatedKt.getOrElse(Validated.kt:98)
	at com.sksamuel.hoplite.ConfigLoader.returnOrThrow(ConfigLoader.kt:183)
But no report (maybe scratch files work different?)
s

sam

03/28/2022, 1:49 PM
Hmm
I'm on a plane so will have to look later 😂
d

dave08

03/28/2022, 1:51 PM
I even rebuilt the code just in case it didn't pick up the change... do system properties expect settings.bucketName or just bucketName? Oh... have a nice flight ✈️!
s

sam

03/28/2022, 1:51 PM
There's special syntax for sys props
It's in the readme
-dconfig.override.foo or something
d

dave08

03/28/2022, 2:10 PM
Ok, as long as the startup doesn't crash, the report gets printed on the scratch's side pane... doesn't help when debugging crashes, but maybe scratches aren't a typical use-case... also, overriding with system props in scratches doesn't seem to work, so I had to use a resource application-local.yaml file that doesn't get committed to git... but I guess now it works, thanks!