1 // The main class, handles all the actions that the executable will call
4 // Version of the application
5 static var version = "1.0.0"
7 // Flag that controls whether we should show the track artist and name before
9 static var showTitle = false
11 // Obtains the name of the current track from a source, fetches the lyrics
12 // from an engine and prints them
13 static func printLyrics() {
15 let sourceManager = SourceManager()
17 if let currentTrack = sourceManager.currentTrack {
18 printLyrics(currentTrack)
20 print("No Artist/Song could be found :(")
24 // fetches the lyrics from an engine and prints them
25 static func printLyrics(_ currentTrack: Track) {
26 let engine = LyricsEngine(withTrack: currentTrack)
29 printTitle(currentTrack)
31 if let lyrics = engine.lyrics {
34 print("Lyrics not found :(")
38 // Print the currently available sources
39 static func printSources() {
40 let sourceManager = SourceManager()
41 for (sourceName, _) in sourceManager.availableSources {
42 if (Configuration.shared["enabled_sources"] as? [String] ?? []).contains(sourceName) {
43 print("\(sourceName) (enabled)")
50 // Runs the enable method of a source and writes the configuration to set it
52 static func enableSource(_ sourceName: String) throws {
53 let sourceManager = SourceManager()
54 if let source = sourceManager.availableSources[sourceName] {
55 if let enabledSources = Configuration.shared["enabled_sources"] as? [String] {
56 if source.enable() == false {
57 throw SourceCouldNotBeEnabled()
59 if !enabledSources.contains(sourceName) {
60 Configuration.shared["enabled_sources"] = enabledSources + [sourceName]
64 throw ConfigurationCouldNotBeRead()
66 throw SourceNotAvailable()
69 // Remove a source from the enabled sources configuration
70 static func disableSource(_ sourceName: String) throws {
71 let sourceManager = SourceManager()
72 if let source = sourceManager.availableSources[sourceName] {
73 if let enabledSources = Configuration.shared["enabled_sources"] as? [String] {
74 if source.disable() == false {
75 throw SourceCouldNotBeDisabled()
77 Configuration.shared["enabled_sources"] = enabledSources.filter { $0 != sourceName }
80 throw ConfigurationCouldNotBeRead()
82 throw SourceNotAvailable()
85 // Removes any configuration for a source, and disables it
86 static func resetSource(_ sourceName: String) throws {
87 let sourceManager = SourceManager()
88 if let source = sourceManager.availableSources[sourceName] {
89 if source.reset() == false {
90 throw SourceCouldNotBeReset()
92 try disableSource(sourceName)
95 throw SourceNotAvailable()
98 // Prints the track artist and name
99 private static func printTitle(_ track: Track) {
100 print("\(track.artist) - \(track.name)")