Mercurial > cgi-bin > hgweb.cgi > PassMan
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a6cfdffcaa94 |
---|---|
1 package name.blackcap.passman | |
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 | |
8 import java.util.* | |
9 import kotlin.system.exitProcess | |
10 | |
11 /* OS Type */ | |
12 | |
13 enum class OS { | |
14 MAC, UNIX, WINDOWS, OTHER; | |
15 companion object { | |
16 private val rawType = System.getProperty("os.name")?.lowercase() | |
17 val type = if (rawType == null) { | |
18 OTHER | |
19 } else if (rawType.contains("win")) { | |
20 WINDOWS | |
21 } else if (rawType.contains("mac")) { | |
22 MAC | |
23 } else if (rawType.contains("nix") || rawType.contains("nux") || rawType.contains("aix") || rawType.contains("sunos")) { | |
24 UNIX | |
25 } else { | |
26 OTHER | |
27 } | |
28 } | |
29 } | |
30 | |
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 */ | |
36 | |
37 private const val SHORTNAME = "passman" | |
38 const val MAIN_PACKAGE = "name.blackcap." + SHORTNAME | |
39 private val HOME = System.getenv("HOME") | |
40 private val PF_DIR = when (OS.type) { | |
41 OS.MAC -> joinPath(HOME, "Library", "Application Support", MAIN_PACKAGE) | |
42 OS.WINDOWS -> joinPath(System.getenv("APPDATA"), MAIN_PACKAGE) | |
43 else -> joinPath(HOME, "." + SHORTNAME) | |
44 } | |
45 | |
46 val PROP_FILE = File(PF_DIR, SHORTNAME + ".properties") | |
47 val DB_FILE: String = File(PF_DIR, SHORTNAME + ".db").absolutePath | |
48 | |
49 /* make some needed directories */ | |
50 | |
51 private fun File.makeIfNeeded() = if (exists()) { true } else { mkdirs() } | |
52 | |
53 /* make some usable objects */ | |
54 | |
55 val DPROPERTIES = Properties().apply { | |
56 OS::class.java.getResourceAsStream("/default.properties").use { load(it) } | |
57 } | |
58 | |
59 val PROPERTIES = Properties(DPROPERTIES).apply { | |
60 PF_DIR.makeIfNeeded() | |
61 PROP_FILE.createNewFile() | |
62 BufferedReader(InputStreamReader(FileInputStream(PROP_FILE), StandardCharsets.UTF_8)).use { | |
63 load(it) | |
64 } | |
65 } | |
66 | |
67 /* error messages */ | |
68 | |
69 fun error(message: String) { | |
70 System.err.println("${SHORTNAME}: ${message}") | |
71 } | |
72 | |
73 fun die(message: String, exitStatus: Int = 1) { | |
74 error(message) | |
75 exitProcess(exitStatus) | |
76 } | |
77 |