// The main class, handles all the actions that the executable will call class Lyricli { // Version of the application static var version = "2.0.1" // Flag that controls whether we should show the track artist and name before // the lyrics static var showTitle = false // Obtains the name of the current track from a source, fetches the lyrics // from an engine and prints them static func printLyrics() { let sourceManager = SourceManager() if let currentTrack = sourceManager.currentTrack { printLyrics(currentTrack) } else { print("No Artist/Song could be found :(") } } // fetches the lyrics from an engine and prints them static func printLyrics(_ currentTrack: Track) { let engine = LyricsEngine(withTrack: currentTrack) if showTitle { printTitle(currentTrack) } if let lyrics = engine.lyrics { print(lyrics) } else { print("Lyrics not found :(") } } // Print the currently available sources static func printSources() { 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) 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) 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) 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 private static func printTitle(_ track: Track) { print("\(track.artist) - \(track.name)") } }