Skip to content

Instantly share code, notes, and snippets.

@hellectronic
Created December 17, 2010 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hellectronic/745812 to your computer and use it in GitHub Desktop.
Save hellectronic/745812 to your computer and use it in GitHub Desktop.
sbt tasks for mongodb
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