Last Updated: September 01, 2020
·
210
· colindean

A simple environment-based config Scala object for a simple app or script

case class Config(input: String, output: String)
val config =
    Some(
      List(inputTableEnvvar, outputTableEnvvar)
      .map(sys.env.get)
      .flatten
    )
    .flatMap {
      // you'll have to keep the parameters here
      // aligned with parameters of the case class
      case List(a: String, b: String) => Some( (a, b) )
      case _ => None
    }
    .map(Config.tupled)

config will be None if the any of the environment variables aren't specified or return null. If you want to eliminate empty strings, add another map before the flatten that handles that.