]> git.r.bdr.sh - rbdr/lyricli/blobdiff - Sources/lyricli/lyricli.swift
Update code, add source management config
[rbdr/lyricli] / Sources / lyricli / lyricli.swift
index 043c0049de8b86e98000f749f07f5c9e54e88c74..ad4bb86c17d8c843b266e566a38b9c0ca5ad2a83 100644 (file)
@@ -25,11 +25,10 @@ class Lyricli {
     static func printLyrics(_ currentTrack: Track) {
         let engine = LyricsEngine(withTrack: currentTrack)
 
+        if showTitle {
+            printTitle(currentTrack)
+        }
         if let lyrics = engine.lyrics {
-            if showTitle {
-                printTitle(currentTrack)
-            }
-
             print(lyrics)
         } else {
             print("Lyrics not found :(")
@@ -38,23 +37,62 @@ class Lyricli {
 
     // Print the currently available sources
     static func printSources() {
-        print("Listing Sources: Not yet implemented")
+        let sourceManager = SourceManager()
+        for (sourceName, _) in sourceManager.availableSources {
+            if (Configuration.shared["enabled_sources"] as? [String] ?? []).contains(sourceName) {
+                print("\(sourceName) (enabled)")
+            } else {
+                print(sourceName)
+            }
+        }
     }
 
     // Runs the enable method of a source and writes the configuration to set it
     // as enabled
-    static func enableSource(_ sourceName: String) {
-        print("Enable source \(sourceName): Not yet implemented")
+    static func enableSource(_ sourceName: String) throws {
+        let sourceManager = SourceManager()
+        if let source = sourceManager.availableSources[sourceName] {
+            if let enabledSources = Configuration.shared["enabled_sources"] as? [String] {
+                if source.enable() == false {
+                    throw SourceCouldNotBeEnabled()
+                }
+                if !enabledSources.contains(sourceName) {
+                    Configuration.shared["enabled_sources"] = enabledSources + [sourceName]
+                }
+                return
+            }
+            throw ConfigurationCouldNotBeRead()
+        }
+        throw SourceNotAvailable()
     }
 
     // Remove a source from the enabled sources configuration
-    static func disableSource(_ sourceName: String) {
-        print("Disable source \(sourceName): Not yet implemented")
+    static func disableSource(_ sourceName: String) throws {
+        let sourceManager = SourceManager()
+        if let source = sourceManager.availableSources[sourceName] {
+            if let enabledSources = Configuration.shared["enabled_sources"] as? [String] {
+                if source.disable() == false {
+                    throw SourceCouldNotBeDisabled()
+                }
+                Configuration.shared["enabled_sources"] = enabledSources.filter { $0 != sourceName }
+                return
+            }
+            throw ConfigurationCouldNotBeRead()
+        }
+        throw SourceNotAvailable()
     }
 
     // Removes any configuration for a source, and disables it
-    static func resetSource(_ sourceName: String) {
-        print("Reset source \(sourceName): Not yet implemented")
+    static func resetSource(_ sourceName: String) throws {
+        let sourceManager = SourceManager()
+        if let source = sourceManager.availableSources[sourceName] {
+            if source.reset() == false {
+                throw SourceCouldNotBeReset()
+            }
+            try disableSource(sourceName)
+            return
+        }
+        throw SourceNotAvailable()
     }
 
     // Prints the track artist and name