aboutsummaryrefslogtreecommitdiff
path: root/Sources/configuration.swift
diff options
context:
space:
mode:
authorBen Beltran <ben@nsovocal.com>2017-05-20 09:40:30 -0500
committerBen Beltran <ben@nsovocal.com>2017-05-20 09:40:30 -0500
commit29a566fada656091bf92456f6fb0f1bea5b6dca3 (patch)
treeca3abc17d3b5077db6b6a1e6d70e4702613eb427 /Sources/configuration.swift
parent85d0536f2e9a3d4596c01b263d76b2b8d9ba70ae (diff)
parentd852b84edc24f1f1bbf4c34aa359447970e732b0 (diff)
Merge branch 'feature/rbdr-document-and-cleanup' into develop
Diffstat (limited to 'Sources/configuration.swift')
-rw-r--r--Sources/configuration.swift50
1 files changed, 29 insertions, 21 deletions
diff --git a/Sources/configuration.swift b/Sources/configuration.swift
index f5e4b0f..f1a1ee1 100644
--- a/Sources/configuration.swift
+++ b/Sources/configuration.swift
@@ -1,34 +1,38 @@
import Foundation
-/// Handles reading and writing the configuration
-public class Configuration {
-
- let configurationPath = NSString(string: "~/.lyricli.conf").expandingTildeInPath
-
- // The defaults are added here
+// Reads and writes the configuration. Config keys are accessed as a dictionary.
+class Configuration {
+ // Location of the global configuration file
+ private let configurationPath = NSString(string: "~/.lyricli.conf").expandingTildeInPath
+ // Default options, will be automatically written to the global config if
+ // not found.
private var configuration: [String: Any] = [
- "enabled_sources": ["arguments"],
- "default": true
+ "enabled_sources": ["arguments"]
]
- static let sharedInstance: Configuration = Configuration()
+ // The shared instance of the object
+ static let shared: 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
+ // Read the config file and attempt to set any of the values. Otherwise
+ // don't do anything.
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 let parsedConfig = try? JSONSerialization.jsonObject(with: data) {
+ if let parsedConfig = parsedConfig as? [String: Any] {
+ for (key, value) in parsedConfig {
- if key == "enabled_sources" {
- configuration[key] = value as! [String]
- }
- else {
- configuration[key] = value as! String
+ if key == "enabled_sources" {
+ if let value = value as? [String] {
+ configuration[key] = value
+ }
+ } else {
+ if let value = value as? String {
+ configuration[key] = value
+ }
+ }
}
}
}
@@ -37,18 +41,22 @@ public class Configuration {
writeConfiguration()
}
+ // Write the configuration back to the file
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)
+ JSONSerialization.writeJSONObject(configuration,
+ to: outputStream,
+ options: [JSONSerialization.WritingOptions.prettyPrinted],
+ error: &error)
outputStream.close()
}
}
- // Allow access as an object
+ // Allow access to the config properties as a dictionary
subscript(index: String) -> Any? {
get {
return configuration[index]