Mercurial > cgi-bin > hgweb.cgi > PassMan
view src/main/kotlin/name/blackcap/passman/Files.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 | 711cc42e96d7 |
line wrap: on
line source
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.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 } } } /* joins path name components to java.io.File */ fun joinPath(base: String, vararg rest: String) = rest.fold(File(base), ::File) /* file names */ private 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) } val PROP_FILE = File(PF_DIR, SHORTNAME + ".properties") val DB_FILE: String = File(PF_DIR, SHORTNAME + ".db").absolutePath /* make some needed directories */ private fun File.makeIfNeeded() = if (exists()) { true } else { mkdirs() } /* make some usable objects */ val DPROPERTIES = Properties().apply { OS::class.java.getResourceAsStream("/default.properties").use { load(it) } } val PROPERTIES = Properties(DPROPERTIES).apply { PF_DIR.makeIfNeeded() PROP_FILE.createNewFile() BufferedReader(InputStreamReader(FileInputStream(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) }