diff src/main/kotlin/name/blackcap/passman/Files.kt @ 15:0fc90892a3ae

Add password subcommand.
author David Barts <n5jrn@me.com>
date Fri, 03 Feb 2023 18:48:13 -0800
parents 711cc42e96d7
children
line wrap: on
line diff
--- a/src/main/kotlin/name/blackcap/passman/Files.kt	Tue Jan 31 19:07:46 2023 -0800
+++ b/src/main/kotlin/name/blackcap/passman/Files.kt	Fri Feb 03 18:48:13 2023 -0800
@@ -1,10 +1,8 @@
 package name.blackcap.passman
 
-import java.io.BufferedReader
-import java.io.File
-import java.io.FileInputStream
-import java.io.InputStreamReader
 import java.nio.charset.StandardCharsets
+import java.nio.file.Files
+import java.nio.file.Path
 import java.util.*
 import kotlin.system.exitProcess
 
@@ -28,27 +26,26 @@
     }
 }
 
-/* joins path name components to java.io.File */
-
-fun joinPath(base: String, vararg rest: String) = rest.fold(File(base), ::File)
-
 /* file names */
 
 const val SHORTNAME = "passman"
 const val MAIN_PACKAGE = "name.blackcap." + SHORTNAME
 private val HOME = System.getenv("HOME")
 private val PF_DIR = when (OS.type) {
-    OS.MAC -> joinPath(HOME, "Library", "Application Support", MAIN_PACKAGE)
-    OS.WINDOWS -> joinPath(System.getenv("APPDATA"), MAIN_PACKAGE)
-    else -> joinPath(HOME, "." + SHORTNAME)
+    OS.MAC -> Path.of(HOME, "Library", "Application Support", MAIN_PACKAGE)
+    OS.WINDOWS -> Path.of(System.getenv("APPDATA"), MAIN_PACKAGE)
+    else -> Path.of(HOME, "." + SHORTNAME)
 }
 
-val PROP_FILE = File(PF_DIR, SHORTNAME + ".properties")
-val DB_FILE: String = File(PF_DIR, SHORTNAME + ".db").absolutePath
+val PROP_FILE = PF_DIR.resolve( SHORTNAME + ".properties")
+val DB_FILE: String = PF_DIR.resolve(SHORTNAME + ".db").toAbsolutePath().toString()
+val NEW_DB_FILE: String = PF_DIR.resolve("new.db").toAbsolutePath().toString()
 
-/* make some needed directories */
+/* make some needed directories/files */
 
-private fun File.makeIfNeeded() = if (exists()) { true } else { mkdirs() }
+private fun makeIfNeeded(p: Path) = if (Files.exists(p)) { p } else { Files.createDirectories(p) }
+
+private fun createIfNeeded(p: Path) = if (Files.exists(p)) { p } else { Files.createFile(p) }
 
 /* make some usable objects */
 
@@ -57,9 +54,9 @@
 }
 
 val PROPERTIES = Properties(DPROPERTIES).apply {
-    PF_DIR.makeIfNeeded()
-    PROP_FILE.createNewFile()
-    BufferedReader(InputStreamReader(FileInputStream(PROP_FILE), StandardCharsets.UTF_8)).use  {
+    makeIfNeeded(PF_DIR)
+    createIfNeeded(PROP_FILE)
+    Files.newBufferedReader(PROP_FILE, StandardCharsets.UTF_8).use  {
         load(it)
     }
 }