Mercurial > cgi-bin > hgweb.cgi > PassMan
view 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 source
package name.blackcap.passman import java.nio.charset.StandardCharsets import java.nio.file.Files import java.nio.file.Path import java.util.* import kotlin.system.exitProcess /* OS Type */ enum class OS { MAC, UNIX, WINDOWS, OTHER; companion object { private val rawType = System.getProperty("os.name")?.lowercase() val type = if (rawType == null) { OTHER } else if (rawType.contains("win")) { WINDOWS } else if (rawType.contains("mac")) { MAC } else if (rawType.contains("nix") || rawType.contains("nux") || rawType.contains("aix") || rawType.contains("sunos")) { UNIX } else { OTHER } } } /* 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 -> 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 = 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/files */ 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 */ val DPROPERTIES = Properties().apply { OS::class.java.getResourceAsStream("/default.properties").use { load(it) } } val PROPERTIES = Properties(DPROPERTIES).apply { makeIfNeeded(PF_DIR) createIfNeeded(PROP_FILE) Files.newBufferedReader(PROP_FILE, StandardCharsets.UTF_8).use { load(it) } } /* error messages */ fun error(message: String) { System.err.println("${SHORTNAME}: ${message}") } fun die(message: String, exitStatus: Int = 1) { error(message) exitProcess(exitStatus) }