diff options
| author | Ben Beltran <ben@nsovocal.com> | 2017-05-18 19:38:15 -0500 |
|---|---|---|
| committer | Ben Beltran <ben@nsovocal.com> | 2017-05-18 19:38:15 -0500 |
| commit | b3320a26c57711ae7167f4549338bd0792dbe570 (patch) | |
| tree | bca17656820ba12db12a63f7d94764a857a5a8c7 /Sources/configuration.swift | |
| parent | 1f7088f6391dfbffcd8f243f8d1509be8a209604 (diff) | |
| parent | a968bff7e21c9372deca5fce5921999b45d5d348 (diff) | |
Merge branch 'feature/read-song-from-command-line' into develop
Diffstat (limited to 'Sources/configuration.swift')
| -rw-r--r-- | Sources/configuration.swift | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/Sources/configuration.swift b/Sources/configuration.swift new file mode 100644 index 0000000..f5e4b0f --- /dev/null +++ b/Sources/configuration.swift @@ -0,0 +1,62 @@ +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) as! [String:Any] { + for (key, value) in parsedConfig { + + if key == "enabled_sources" { + configuration[key] = value as! [String] + } + else { + configuration[key] = value as! String + } + } + } + } + + 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() + } + } +} |