comparison src/main/kotlin/name/blackcap/passman/PasswordSubcommand.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 0fc90892a3ae
children
comparison
equal deleted inserted replaced
20:4391afcf6bd0 21:ea65ab890f66
1 package name.blackcap.passman 1 package name.blackcap.passman
2 2
3 import java.nio.file.Files 3 import java.nio.file.Files
4 import java.nio.file.Path 4 import java.nio.file.Path
5 import java.nio.file.StandardCopyOption 5 import java.nio.file.StandardCopyOption
6 import kotlin.system.exitProcess
7 6
8 class PasswordSubcommand : Subcommand() { 7 class PasswordSubcommand : Subcommand() {
9 override fun run(args: Array<String>) { 8 override fun run(args: Array<String>) {
10 // Parse arguments 9 // Parse arguments
11 if (args.size > 0 && (args[0] == "-h" || args[0].startsWith("--h"))) { 10 if (args.size > 0 && (args[0] == "-h" || args[0].startsWith("--h"))) {
12 println("usage: passman password") 11 println("usage: passman password")
13 exitProcess(0) 12 return
14 } 13 }
15 if (!args.isEmpty()) { 14 if (!args.isEmpty()) {
16 die("unexpected arguments", 2) 15 throw SubcommandException(message = "unexpected arguments", status = 2)
17 } 16 }
18 17
19 // Open databases 18 // Open databases
20 println("Changing database encryption key...") 19 println("Changing database encryption key...")
21 val oldPath = Path.of(DB_FILE) 20 val oldPath = Path.of(DB_FILE)
22 val oldDb = Database.open(fileName = DB_FILE, passwordPrompt = "Old database key: ") 21 val oldDb = Database.default
23 val newPath = Path.of(NEW_DB_FILE) 22 val newPath = Path.of(NEW_DB_FILE)
24 if (Files.exists(newPath)) { 23 if (Files.exists(newPath)) {
25 println("WARNING: deleting ${see(NEW_DB_FILE)}") 24 println("WARNING: deleting ${see(NEW_DB_FILE)}")
26 Files.delete(newPath) 25 Files.delete(newPath)
27 } 26 }
29 28
30 // Copy old to new 29 // Copy old to new
31 println("WARNING: do not interrupt this process or data may be lost!") 30 println("WARNING: do not interrupt this process or data may be lost!")
32 copyRecords(oldDb, newDb) 31 copyRecords(oldDb, newDb)
33 32
34 // Wrap up 33 // Wrap up. XXX - this closes Database.default, so this subcommand may
34 // only be invoked directly from the shell prompt, never in interactive
35 // mode.
35 oldDb.connection.close() 36 oldDb.connection.close()
36 newDb.connection.close() 37 newDb.connection.close()
37 Files.move(newPath, oldPath, StandardCopyOption.REPLACE_EXISTING) 38 Files.move(newPath, oldPath, StandardCopyOption.REPLACE_EXISTING)
38 println("Database key changed.") 39 println("Database key changed.")
39 } 40 }