comparison src/main/kotlin/name/blackcap/passman/DeleteSubcommand.kt @ 21:ea65ab890f66

More work to support interactive feature.
author David Barts <n5jrn@me.com>
date Tue, 02 Jul 2024 11:27:39 -0700
parents 72619175004e
children 07406c4af4a5
comparison
equal deleted inserted replaced
20:4391afcf6bd0 21:ea65ab890f66
1 package name.blackcap.passman 1 package name.blackcap.passman
2 2
3 import kotlin.system.exitProcess
4
5 class DeleteSubcommand(): Subcommand() { 3 class DeleteSubcommand(): Subcommand() {
6 override fun run(args: Array<String>) { 4 override fun run(args: Array<String>): {
7 if (args.isEmpty()) { 5 if (args.isEmpty()) {
8 die("expecting a site name", 2) 6 throw SubcommandException(message = "expecting a site name", status = 2)
9 } 7 }
10 if (args[0] == "-h" || args[0].startsWith("--h")) { 8 if (args[0] == "-h" || args[0].startsWith("--h")) {
11 println("usage: passman delete name [...]") 9 println("usage: passman delete name [...]")
12 exitProcess(0) 10 return 0
13 } 11 }
14 val db = Database.open() 12 val db = Database.default
15 var errors = 0 13 var errors = 0
16 for (nameIn in args) { 14 for (nameIn in args) {
17 db.connection.prepareStatement("delete from passwords where id = ?").use { 15 db.connection.prepareStatement("delete from passwords where id = ?").use {
18 it.setLong(1, db.makeKey(nameIn)) 16 it.setLong(1, db.makeKey(nameIn))
19 if (it.executeUpdate() == 0) { 17 if (it.executeUpdate() == 0) {
20 error("no record matches ${see(nameIn)}") 18 error("no record matches ${see(nameIn)}")
21 errors++ 19 errors++
22 } 20 }
23 } 21 }
24 } 22 }
25 exitProcess(if (errors > 0) 1 else 0) 23 if (errors > 0) {
24 throw SubcommandException()
25 }
26 } 26 }
27 } 27 }