我使用 picocli,我想构建 cli 从数据库中获取数据并将其发送到另一个服务。所以我配置看起来像
object Student : Table("student") {
val id = integer("student_id").autoIncrement().primaryKey()
val name = varchar("name", 50)
val grade = integer("grade")
}
class App() : Callable<Integer> {
@CommandLine.Option(names = ["-ho", "--host"], description = arrayOf("Host database"), defaultValue = "localhost")
var host: String? = null
@CommandLine.Option(names = ["-p", "--port"], description = arrayOf("port database"), defaultValue = "5432")
var port: String? = null
@CommandLine.Option(names = ["-d", "--database"], description = arrayOf("database"), defaultValue = "postgres")
var database: String? = null
@CommandLine.Option(names = ["-u", "--username"], description = arrayOf("username of database"), defaultValue = "postgres")
var username: String? = null
@CommandLine.Option(names = ["-w", "--password"], description = arrayOf("password off database"), defaultValue = "password")
var password: String? = null
override fun call(): Integer {
connectDB()
createStudent()
return Integer(0)
}
fun createStudent() {
println("Begin create student")
transaction {
create(Student)
}
println("End create student")
}
fun connectDB() {
try {
Database.connect("jdbc:postgresql://${this.host}:${this.port}/${this.database}", "org.postgresql.Driver",this.username.toString(),this.password.toString())
print("Connect db")
} catch (e : Exception) {
println("Connect db fail with error ${e.message}")
}
}
}
fun main(args: Array<String>) {
val existCode = CommandLine(App()).execute(*args)
System.exit(existCode);
}
当我执行时,它连接成功。但我不想从 args 解析值。我想从我的文件属性示例中读取:db.properties
或db.yml
。怎么做?
我搜索了文档 picocli,但找不到任何内容。所以现在我在我的 postgres 中使用 org.jetbrains.exposed:exposed crud。它可以与 picoli 一起使用吗?如果从数据库中获取数据并将其发送到另一个 api,是否有任何建议的框架?非常感谢