view src/main/kotlin/name/blackcap/passman/DeleteSubcommand.kt @ 9:72619175004e

Fix issues found in testing.
author David Barts <n5jrn@me.com>
date Sat, 01 Oct 2022 09:57:23 -0700
parents 698c4a3d758d
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)
    }
}