view src/main/kotlin/name/blackcap/passman/DeleteSubcommand.kt @ 28:287eadf5ab30 default tip

Check for timeouts inside subcommands while in interactive mode as well.
author David Barts <n5jrn@me.com>
date Wed, 31 Jul 2024 11:21:18 -0700
parents 07406c4af4a5
children
line wrap: on
line source

package name.blackcap.passman

class DeleteSubcommand(): Subcommand() {
    override fun run(args: Array<String>) {
        if (args.isEmpty()) {
            throw SubcommandException(message = "expecting a site name", status = 2)
        }
        if (args[0] == "-h" || args[0].startsWith("--h")) {
            println("usage: passman delete name [...]")
            return
        }
        val db = Database.default
        var errors = 0
        for (nameIn in args) {
            db.connection.prepareStatement("delete from passwords where id = ?").use {
                it.setLong(1, db.makeKey(nameIn))
                if (it.executeUpdate() == 0) {
                    error("no record matches ${see(nameIn)}")
                    errors++
                }
            }
        }
        if (errors > 0) {
            throw SubcommandException()
        }
    }
}