view src/main/kotlin/name/blackcap/passman/DeleteSubcommand.kt @ 14:4dae7a15ee48

Fix bugs found in additional round of testing.
author David Barts <n5jrn@me.com>
date Tue, 31 Jan 2023 19:07:46 -0800
parents 72619175004e
children ea65ab890f66
line wrap: on
line source

package name.blackcap.passman

import kotlin.system.exitProcess

class DeleteSubcommand(): Subcommand() {
    override fun run(args: Array<String>) {
        if (args.isEmpty()) {
            die("expecting a site name", 2)
        }
        if (args[0] == "-h" || args[0].startsWith("--h")) {
            println("usage: passman delete name [...]")
            exitProcess(0)
        }
        val db = Database.open()
        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++
                }
            }
        }
        exitProcess(if (errors > 0) 1 else 0)
    }
}