Mercurial > cgi-bin > hgweb.cgi > PassMan
comparison 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 |
comparison
equal
deleted
inserted
replaced
14:4dae7a15ee48 | 15:0fc90892a3ae |
---|---|
1 package name.blackcap.passman | 1 package name.blackcap.passman |
2 | 2 |
3 import java.io.BufferedReader | |
4 import java.io.File | |
5 import java.io.FileInputStream | |
6 import java.io.InputStreamReader | |
7 import java.nio.charset.StandardCharsets | 3 import java.nio.charset.StandardCharsets |
4 import java.nio.file.Files | |
5 import java.nio.file.Path | |
8 import java.util.* | 6 import java.util.* |
9 import kotlin.system.exitProcess | 7 import kotlin.system.exitProcess |
10 | 8 |
11 /* OS Type */ | 9 /* OS Type */ |
12 | 10 |
26 OTHER | 24 OTHER |
27 } | 25 } |
28 } | 26 } |
29 } | 27 } |
30 | 28 |
31 /* joins path name components to java.io.File */ | |
32 | |
33 fun joinPath(base: String, vararg rest: String) = rest.fold(File(base), ::File) | |
34 | |
35 /* file names */ | 29 /* file names */ |
36 | 30 |
37 const val SHORTNAME = "passman" | 31 const val SHORTNAME = "passman" |
38 const val MAIN_PACKAGE = "name.blackcap." + SHORTNAME | 32 const val MAIN_PACKAGE = "name.blackcap." + SHORTNAME |
39 private val HOME = System.getenv("HOME") | 33 private val HOME = System.getenv("HOME") |
40 private val PF_DIR = when (OS.type) { | 34 private val PF_DIR = when (OS.type) { |
41 OS.MAC -> joinPath(HOME, "Library", "Application Support", MAIN_PACKAGE) | 35 OS.MAC -> Path.of(HOME, "Library", "Application Support", MAIN_PACKAGE) |
42 OS.WINDOWS -> joinPath(System.getenv("APPDATA"), MAIN_PACKAGE) | 36 OS.WINDOWS -> Path.of(System.getenv("APPDATA"), MAIN_PACKAGE) |
43 else -> joinPath(HOME, "." + SHORTNAME) | 37 else -> Path.of(HOME, "." + SHORTNAME) |
44 } | 38 } |
45 | 39 |
46 val PROP_FILE = File(PF_DIR, SHORTNAME + ".properties") | 40 val PROP_FILE = PF_DIR.resolve( SHORTNAME + ".properties") |
47 val DB_FILE: String = File(PF_DIR, SHORTNAME + ".db").absolutePath | 41 val DB_FILE: String = PF_DIR.resolve(SHORTNAME + ".db").toAbsolutePath().toString() |
42 val NEW_DB_FILE: String = PF_DIR.resolve("new.db").toAbsolutePath().toString() | |
48 | 43 |
49 /* make some needed directories */ | 44 /* make some needed directories/files */ |
50 | 45 |
51 private fun File.makeIfNeeded() = if (exists()) { true } else { mkdirs() } | 46 private fun makeIfNeeded(p: Path) = if (Files.exists(p)) { p } else { Files.createDirectories(p) } |
47 | |
48 private fun createIfNeeded(p: Path) = if (Files.exists(p)) { p } else { Files.createFile(p) } | |
52 | 49 |
53 /* make some usable objects */ | 50 /* make some usable objects */ |
54 | 51 |
55 val DPROPERTIES = Properties().apply { | 52 val DPROPERTIES = Properties().apply { |
56 OS::class.java.getResourceAsStream("/default.properties").use { load(it) } | 53 OS::class.java.getResourceAsStream("/default.properties").use { load(it) } |
57 } | 54 } |
58 | 55 |
59 val PROPERTIES = Properties(DPROPERTIES).apply { | 56 val PROPERTIES = Properties(DPROPERTIES).apply { |
60 PF_DIR.makeIfNeeded() | 57 makeIfNeeded(PF_DIR) |
61 PROP_FILE.createNewFile() | 58 createIfNeeded(PROP_FILE) |
62 BufferedReader(InputStreamReader(FileInputStream(PROP_FILE), StandardCharsets.UTF_8)).use { | 59 Files.newBufferedReader(PROP_FILE, StandardCharsets.UTF_8).use { |
63 load(it) | 60 load(it) |
64 } | 61 } |
65 } | 62 } |
66 | 63 |
67 /* error messages */ | 64 /* error messages */ |