3 /// Handles reading and writing the configuration
4 public class Configuration {
6 let configurationPath = NSString(string: "~/.lyricli.conf").expandingTildeInPath
8 // The defaults are added here
10 private var configuration: [String: Any] = [
11 "enabled_sources": ["arguments"],
15 static let sharedInstance: Configuration = Configuration()
19 // Read the config file and attempt toset any of the values. Otherwise
21 // IMPROVEMENT: Enable a debug mode
23 if let data = try? NSData(contentsOfFile: configurationPath) as Data {
24 if let parsedConfig = try? JSONSerialization.jsonObject(with: data) as! [String:Any] {
25 for (key, value) in parsedConfig {
27 if key == "enabled_sources" {
28 configuration[key] = value as! [String]
31 configuration[key] = value as! String
40 private func writeConfiguration() {
44 if let outputStream = OutputStream(toFileAtPath: configurationPath, append: false) {
46 JSONSerialization.writeJSONObject(configuration, to: outputStream, options: [JSONSerialization.WritingOptions.prettyPrinted], error: &error)
51 // Allow access as an object
52 subscript(index: String) -> Any? {
54 return configuration[index]
58 configuration[index] = newValue