Advertisement
NLinker

Runner, loading config from cmd line

Nov 18th, 2013
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.06 KB | None | 0 0
  1. //import com.tapad.dfe.config.{Configuration, ClasspathConfigResolver}
  2. //import com.typesafe.config.{ConfigValueFactory, ConfigFactory, Config}
  3. import com.typesafe.scalalogging.slf4j.Logging
  4. //import java.io.File
  5. //import org.rogach.scallop.ScallopConf
  6. //import scala.util.{Failure, Success, Try}
  7.  
  8. object Runner extends Logging {
  9.  
  10.   def main(args: Array[String]) = {
  11.     // parse command line args
  12.     val opts = new ScallopConf(List("-h")) {
  13.       version("1.0")
  14.       banner("""
  15. Delivery File Uploader runner
  16.  
  17. Example: java -jar dfe.jar [--config path/to/config/file]
  18.         java -jar dfe.jar [-c path/to/config/file]
  19.         java -Dcom.tapad.dfe.env=test -jar dfe.jar
  20.  
  21. For options see below:
  22.             """)
  23.       val config = opt[String]("config", descr = """ Provide the path to the configuration file, if the option is omitted, then
  24.          | the configuration will be loaded from the classpath, depending on your
  25.          | environment, hence the file 'dfe-${ENV}.conf' will be used.
  26.          |
  27.          |To define environment (in priority order)
  28.          | * "default"  (lowest priority)
  29.          | * TAPAD_ENV environment variable
  30.          | * the first file with *.env in the current dir (it's content doesn't matter)
  31.          | * "com.tapad.dfe.env" system property
  32.          | """.stripMargin)
  33.       val version = opt[Boolean]("version", descr = "Print version")
  34.       val help = opt[Boolean]("help", descr = "Show this message")
  35.     }
  36.     try {
  37.       // try to load the config, and assign it to the Configuration
  38.       println(Configuration.conf)
  39.       val config = opts.config
  40.       val path = config.apply()
  41.       val file = new File(path)
  42.       if (!file.exists()) {
  43.         throw new Exception(s"The configuration file $path does not exist")
  44.       }
  45.       val ref = ConfigValueFactory.fromAnyRef(file.getAbsolutePath)
  46.       Configuration.conf = Configuration.loadConf(
  47.         ConfigFactory.parseFile(file).withValue("source", ref)
  48.       )
  49.     } catch {
  50.       case err: Throwable => logger.error(err.getMessage)
  51.     }
  52.  
  53.   }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement