Last active
March 20, 2022 18:33
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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