<@U12AGS8JG> I'm trying to use env vars for settin...
# hoplite
d
@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
Copy code
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:
Copy code
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
If you're on 2.0.x then you can add .report
d
.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
Hmm maybe I didn't update docs for report on the latest 2.0
ConfigLoaderBuilder.default().report...
d
I did that:
Copy code
val config by lazy {
    ConfigLoaderBuilder.default().report()
        .addPropertySource(EnvironmentVariablesPropertySource(false, false))
        .build()
        .loadConfigOrThrow<Settings>()
}
s
There you go
If you want to use env you can override in the field d. That's in the docs
d
I got this:
Copy code
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
Hmm
I'm on a plane so will have to look later 😂
d
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
There's special syntax for sys props
It's in the readme
-dconfig.override.foo or something
d
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!
214 Views