Skip to content

Instantly share code, notes, and snippets.

@DanielaSfregola
Last active March 20, 2022 18:33
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save DanielaSfregola/603a39c8c5ae7b620aec to your computer and use it in GitHub Desktop.
Save DanielaSfregola/603a39c8c5ae7b620aec to your computer and use it in GitHub Desktop.
A simple script that will try to load configurations in the following order: 1) From properly named environment variables 2) From command line paramenters 3) From the configuration file. See article http://danielasfregola.com/2015/06/01/loading-configurations-in-scala/
import com.typesafe.config.ConfigFactory
import scala.util.Properties
class MyConfig(fileNameOption: Option[String] = None) {
val config = fileNameOption.fold(
ifEmpty = ConfigFactory.load() )(
file => ConfigFactory.load(file) )
def envOrElseConfig(name: String): String = {
Properties.envOrElse(
name.toUpperCase.replaceAll("""\.""", "_"),
config.getString(name)
)
}
}
val myConfig = new MyConfig()
val value = myConfig.envOrElseConfig("my.secret.value")
println(s"My secret value is $value")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment