diff src/main/kotlin/name/blackcap/passman/Entry.kt @ 0:a6cfdffcaa94

Initial commit, incomplete but it runs sorta.
author David Barts <n5jrn@me.com>
date Sun, 11 Sep 2022 16:11:37 -0700
parents
children 3c792ad36b3d
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/kotlin/name/blackcap/passman/Entry.kt	Sun Sep 11 16:11:37 2022 -0700
@@ -0,0 +1,68 @@
+package name.blackcap.passman
+
+import java.lang.StringBuilder
+import java.sql.Connection
+import java.sql.PreparedStatement
+import java.util.Date
+
+class Entry(val name: String, val username: String, val password: CharArray, val notes: String,
+            val created: Date? = null, val modified: Date? = null, val accessed: Date? = null) {
+
+    companion object {
+        fun withPromptedPassword() = Entry(
+                name = _getName(),
+                username = _getUsername(),
+                password = _getPassword(),
+                notes = _getNotes()
+            )
+
+        fun withGeneratedPassword(length: Int, allowSymbols: Boolean, verbose: Boolean): Entry {
+            val generated = generate(length, allowSymbols)
+            if (verbose) {
+                println("Generated password: $generated")
+            }
+            return Entry(
+                name = _getName(),
+                username = _getUsername(),
+                password = generated,
+                notes = _getNotes()
+            )
+        }
+
+        private fun _getName() = mustReadLine("Name of site: ")
+
+        private fun _getUsername() = mustReadLine("Username: ")
+
+        private fun _getPassword() = mustGetPassword("Password: ", verify = true)
+
+        private fun _getNotes() = readLine("Notes: ")
+    }
+
+    fun print(redactPassword: String? = null) {
+        println("Name of site: $name")
+        println("Username: $username")
+        if (redactPassword == null) {
+            printPassword(password)
+        } else {
+            println("Password: $redactPassword")
+        }
+    }
+
+    fun printLong(redactPassword: String? = null) {
+        print(redactPassword)
+        println("Notes: $notes")
+        printDate("Created", created)
+        printDate("Modified", modified)
+        printDate("Accessed", accessed)
+    }
+
+    private fun printDate(tag: String, date: Date?) {
+        kotlin.io.print("${tag}: ")
+        if (date == null) {
+            println("never")
+        } else {
+            println(ISO8601.format(date))
+        }
+    }
+
+}
\ No newline at end of file