blob: bc217694b1ab189a1748bc3b0d7754f7094bdaf3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
import Foundation
/// Handles reading and writing the configuration
public class Configuration {
let configurationPath = NSString(string: "~/.lyricli.conf").expandingTildeInPath
// The defaults are added here
private var configuration: [String: Any] = [
"enabled_sources": ["arguments"],
"default": true
]
static let sharedInstance: Configuration = Configuration()
private init() {
// Read the config file and attempt toset any of the values. Otherwise
// Don't do anything.
// IMPROVEMENT: Enable a debug mode
if let data = try? NSData(contentsOfFile: configurationPath) as Data {
if let parsedConfig = try? JSONSerialization.jsonObject(with: data) {
if let parsedConfig = parsedConfig as? [String: Any] {
for (key, value) in parsedConfig {
if key == "enabled_sources" {
if let value = value as? [String] {
configuration[key] = value
}
} else {
if let value = value as? String {
configuration[key] = value
}
}
}
}
}
}
writeConfiguration()
}
private func writeConfiguration() {
var error: NSError?
if let outputStream = OutputStream(toFileAtPath: configurationPath, append: false) {
outputStream.open()
JSONSerialization.writeJSONObject(configuration,
to: outputStream,
options: [JSONSerialization.WritingOptions.prettyPrinted],
error: &error)
outputStream.close()
}
}
// Allow access as an object
subscript(index: String) -> Any? {
get {
return configuration[index]
}
set(newValue) {
configuration[index] = newValue
writeConfiguration()
}
}
}
|