CLI: Started implementing CLI configuration setup.

This commit is contained in:
Jonathan Bernard 2016-02-09 17:47:00 -06:00
parent a2c9348a99
commit f5ab973b43

View File

@ -1,14 +1,24 @@
package com.jdbernard.wdiwtlt.interfaces
package com.jdbernard.wdiwtlt.cli
import com.jdbernard.wdiwtlt.ConfigWrapper
import com.jdbernard.wdiwtlt.MediaLibrary
import com.jdbernard.wdiwtlt.db.ORM
import com.jdbernard.io.NonBlockingInputStreamReader
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import org.docopt.Docopt
public class CommandLineInterface {
public static final VERSION = "alpha"
public static final VERSION = "ALPHA"
public static final def DOC = """\
wdiwtlt v$VERSION
Usage:
wdiwtlt [options]
wdiwtlt --version
wdiwtlt --help
Options:
-L --library-root <root-dir>
@ -16,13 +26,73 @@ Options:
The path to a local media library directory. Providing a local library
causes wdiwtlt to run in local mode.
-D --database-config <config-file>
The path to a data source configuration file. wdiwtlt cli uses HikariCP
for database connection pooling, so the given configuration file is
expected to be formatted so that it can be fed directly to the
HikariConfig constructor.
(see https://github.com/brettwooldridge/HikariCP#configuration-knobs-baby)
Configuration:
"""
private MediaLibrary library
public static void main(String[] args) {
def opts = new Docopt(DOC).withVersion("wdiwtlt v$VERSION").parse(args)
println opts
def exitErr = { msg ->
System.out.err.println("wdiwtlt: $msg")
System.exit(1) }
def wdiwtltCfg = new ConfigWrapper()
// Get the library root (if local)
File libRoot = opts["--library-root"] ?
new File(opts["--library-root"]) :
new File(wdiwtltCfg.libraryRootPath)
if (libRoot && (!libRoot.exist() || !libRoot.isDirectory()))
exitErr("Library root does not exist or is not a directory: " +
libRoot.canonicalPath)
// Get the library database
HikariConfig hcfg
File dbCfgFile = opts["--database-config"] ?
new File(opts["--database-config"]) : null
if (dbCfgFile && dbCfgFile.exists() && dbCfgFile.isFile()) {
Properties props = new Properties()
try { dbCfgFile.withInputStream { props.load(it) } }
catch (Exception e) { props.clear() }
if (props) hcfg = new HikariConfig(props) }
if (!hcfg) hcfg = wdiwtltCfg.hikariConfig
if (!hcfg) exitErr("Cannot load database configuation.")
// Create our database instance
def orm
try {
def hds = new HikariDataSource(hcfg)
orm = new ORM(hds) }
catch (Exception e) {
exitErr("Could not establish a connection to the database:\n\t" +
e.localizedMessage) }
// Create our CLI object
def cliInst = new CommandLineInterface(new MediaLibrary(orm, libRoot))
cliInst.repl()
}
public CommandLineInterface(MediaLibrary library) {
this.library = library
}
public void repl(InputStream sin, PrintStream out) {
}
}