view src/main/kotlin/name/blackcap/passman/DeleteSubcommand.kt @ 19:7d80cbcb67bb

add shlex-style splitter and tests
author David Barts <n5jrn@me.com>
date Sun, 30 Jun 2024 20:37:36 -0700
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)
    }
}