Created
December 17, 2010 22:20
-
-
Save hellectronic/745812 to your computer and use it in GitHub Desktop.
sbt tasks for mongodb
This file contains hidden or 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
val host = "localhost" | |
val port = 27017 | |
val dbname = "test" | |
val mongodbSciptPath = testResourcesPath / "mongodb" | |
val datagenFile = mongodbSciptPath / "datagen.js" absolutePath | |
val cleanDbFile = mongodbSciptPath / "cleandb.js" absolutePath | |
lazy val dropDb = withArgsTest("please provide a db name", _.length > 0) { | |
args => MongoCli().eval("db.dropDatabase()").host(host).port(port).db(args(0)).cmd | |
} | |
lazy val fillDb = withArgsTest("please provide a db name", _.length > 0) { | |
args => MongoCli().host(host).port(port).db(args(0)).files(datagenFile).cmd | |
} | |
lazy val cleanDb = withArgsTest("please provide a db name", _.length > 0) { | |
args => MongoCli().host(host).port(port).db(args(0)).files(cleanDbFile).cmd | |
} | |
def withArgsTest(err: String, argsTest: Array[String] => Boolean)(cmd: Array[String] => String) = { | |
task { args: Array[String] => | |
if (argsTest(args)) task { cmd(args) !! log; None } else task { Some(err) } | |
} | |
} | |
case class MongoCli() { | |
private val sep = " " | |
private var _eval = "" | |
private var _host = "" | |
private var _port = "" | |
private var _db = "" | |
private var _jsFiles: Seq[String] = List[String]() | |
def eval(js: String): MongoCli = { | |
_eval = js | |
this | |
} | |
def host(h: String): MongoCli = { | |
_host = h | |
this | |
} | |
def port(p: Int): MongoCli = { | |
_port = p.toString | |
this | |
} | |
def db(n: String): MongoCli = { | |
_db = n | |
this | |
} | |
def files(f: String*): MongoCli = { | |
_jsFiles = f | |
this | |
} | |
def cmd(): String = { | |
if (_eval.isEmpty && _jsFiles.isEmpty) throw new IllegalArgumentException("specify js for eval or js files!") | |
var sb = List("mongo") | |
if (!_host.isEmpty) sb = sb ::: List("--host "+ _host) | |
if (!_port.isEmpty) sb = sb ::: List("--port "+ _port) | |
if (!_eval.isEmpty) sb = sb ::: List("--eval \""+ _eval +"\"") | |
if (!_db.isEmpty) sb = sb ::: List(_db) | |
if (!_jsFiles.isEmpty) sb = sb ::: List(_jsFiles mkString sep) | |
sb mkString sep | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment