3 // Reads and writes the configuration. Config keys are accessed as a dictionary.
5 // Location of the global configuration file
6 private let configurationPath = NSString(string: "~/.lyricli.conf").expandingTildeInPath
8 // Default options, will be automatically written to the global config if
10 private var configuration: [String: Any] = [
11 "enabled_sources": ["apple_music", "spotify"]
14 // The shared instance of the object
15 static let shared: Configuration = Configuration()
19 // Read the config file and attempt to set any of the values. Otherwise
22 if let data = try? NSData(contentsOfFile: configurationPath) as Data {
23 if let parsedConfig = try? JSONSerialization.jsonObject(with: data) {
24 if let parsedConfig = parsedConfig as? [String: Any] {
25 for (key, value) in parsedConfig {
27 if key == "enabled_sources" {
28 if let value = value as? [String] {
29 configuration[key] = value
32 if let value = value as? String {
33 configuration[key] = value
44 // Write the configuration back to the file
45 private func writeConfiguration() {
49 if let outputStream = OutputStream(toFileAtPath: configurationPath, append: false) {
51 JSONSerialization.writeJSONObject(configuration,
53 options: [JSONSerialization.WritingOptions.prettyPrinted],
59 // Allow access to the config properties as a dictionary
60 subscript(index: String) -> Any? {
62 return configuration[index]
66 configuration[index] = newValue