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) {
25 if let parsedConfig = parsedConfig as? [String: Any] {
26 for (key, value) in parsedConfig {
28 if key == "enabled_sources" {
29 if let value = value as? [String] {
30 configuration[key] = value
33 if let value = value as? String {
34 configuration[key] = value
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 as an object
60 subscript(index: String) -> Any? {
62 return configuration[index]
66 configuration[index] = newValue