]> git.r.bdr.sh - rbdr/lyricli/commitdiff
Fix linter warnings
authorBen Beltran <redacted>
Fri, 19 May 2017 04:39:25 +0000 (23:39 -0500)
committerBen Beltran <redacted>
Fri, 19 May 2017 04:39:25 +0000 (23:39 -0500)
Sources/arguments_source.swift
Sources/configuration.swift
Sources/lyricli.swift
Sources/lyrics_engine.swift
Sources/main.swift
Sources/source_manager.swift

index c07c9522102cb8a125c6dfcd35009f50fad094e9..d1bf4c607a992e3ce91ad4357531ba965b7f5d8a 100644 (file)
@@ -1,17 +1,15 @@
 /// Source that deals with command line
 class ArgumentsSource: Source {
     public var currentTrack: Track? {
-        get {
-            if CommandLine.arguments.count >= 3 {
+        if CommandLine.arguments.count >= 3 {
 
-                // expected usage: $ ./lyricli <artist> <name>
+            // expected usage: $ ./lyricli <artist> <name>
 
-                let trackName: String = CommandLine.arguments[2]
-                let trackArtist: String = CommandLine.arguments[1]
+            let trackName: String = CommandLine.arguments[2]
+            let trackArtist: String = CommandLine.arguments[1]
 
-                return Track(withName: trackName, andArtist: trackArtist)
-            }
-            return nil
+            return Track(withName: trackName, andArtist: trackArtist)
         }
+        return nil
     }
 }
index f5e4b0f53b04f52cebf5cc524d247728761b3d4b..bc217694b1ab189a1748bc3b0d7754f7094bdaf3 100644 (file)
@@ -21,14 +21,19 @@ public class Configuration {
         // 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 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
+                            }
+                        }
                     }
                 }
             }
@@ -43,7 +48,10 @@ public class Configuration {
 
         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()
         }
     }
index c44a7f401f4e9d3e20af94f58531764700340e70..5931ca10f4e9ee9303311a5e76a70bb9d0f8d2e0 100644 (file)
@@ -18,13 +18,11 @@ public class Lyricli {
                 }
 
                 print(lyrics)
-            }
-            else {
+            } else {
                 print("Lyrics not found :(")
             }
 
-        }
-        else {
+        } else {
             print("No Artist/Song could be found :(")
         }
     }
index d6b1985908ec8eb8af269b1b3ce0487691da61f1..9741a2c63d65393ebeadc27fd63c5d5829455bd7 100644 (file)
@@ -13,40 +13,37 @@ class LyricsEngine {
     // Fetches the lyrics and returns if found
 
     var lyrics: String? {
-        get {
+        var lyrics: String?
 
-            var lyrics: String?
+        if let artist = track.artist.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
+            if let name: String = track.name.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
+                if let url = URL(string: "\(apiURL)&artist=\(artist)&song=\(name)") {
 
-            if let artist = track.artist.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
-                if let name: String = track.name.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
-                    if let url = URL(string: "\(apiURL)&artist=\(artist)&song=\(name)") {
+                    // We'll lock until the async call is finished
 
-                        // We'll lock until the async call is finished
+                    var requestFinished = false
+                    let asyncLock = NSCondition()
+                    asyncLock.lock()
 
-                        var requestFinished = false
-                        let asyncLock = NSCondition()
-                        asyncLock.lock()
+                    // Call the API and unlock when you're done
 
-                        // Call the API and unlock when you're done
-
-                        fetchLyricsFromAPI(withURL: url, completionHandler: {lyricsResult -> Void in
-                            if let lyricsResult = lyricsResult {
-                                lyrics = lyricsResult
-                                requestFinished = true
-                                asyncLock.signal()
-                            }
-                        })
-
-                        while(!requestFinished) {
-                            asyncLock.wait()
+                    fetchLyricsFromAPI(withURL: url, completionHandler: {lyricsResult -> Void in
+                        if let lyricsResult = lyricsResult {
+                            lyrics = lyricsResult
+                            requestFinished = true
+                            asyncLock.signal()
                         }
-                        asyncLock.unlock()
+                    })
+
+                    while !requestFinished {
+                        asyncLock.wait()
                     }
+                    asyncLock.unlock()
                 }
             }
-
-            return lyrics
         }
+
+        return lyrics
     }
 
     init(withTrack targetTrack: Track) {
@@ -61,20 +58,21 @@ class LyricsEngine {
         var apiRequest = URLRequest(url: url)
         apiRequest.httpMethod = "GET"
 
-        let task = URLSession.shared.dataTask(with: apiRequest, completionHandler: {data, response, error -> Void in
+        let task = URLSession.shared.dataTask(with: apiRequest, completionHandler: {data, _, _ -> Void in
 
             // If the response is parseable JSON, and has a url, we'll look for
             // the lyrics in there
 
             if let data = data {
-                let jsonResponse = try? JSONSerialization.jsonObject(with: data) as! [String: Any]
-                if let jsonResponse = jsonResponse {
-                    if let lyricsUrlString = jsonResponse["url"] as? String {
-                        if let lyricsUrl = URL(string: lyricsUrlString) {
-
-                            // At this point we have a valid wiki url
-                            self.fetchLyricsFromPage(withURL: lyricsUrl, completionHandler: completionHandler)
-                            return
+                if let jsonResponse = try? JSONSerialization.jsonObject(with: data) {
+                    if let jsonResponse = jsonResponse as? [String: Any] {
+                        if let lyricsUrlString = jsonResponse["url"] as? String {
+                            if let lyricsUrl = URL(string: lyricsUrlString) {
+
+                                // At this point we have a valid wiki url
+                                self.fetchLyricsFromPage(withURL: lyricsUrl, completionHandler: completionHandler)
+                                return
+                            }
                         }
                     }
                 }
@@ -92,7 +90,7 @@ class LyricsEngine {
         var pageRequest = URLRequest(url: url)
         pageRequest.httpMethod = "GET"
 
-        let task = URLSession.shared.dataTask(with: pageRequest, completionHandler: {data, response, error -> Void in
+        let task = URLSession.shared.dataTask(with: pageRequest, completionHandler: {data, _, _ -> Void in
 
             // If the response is parseable JSON, and has a url, we'll look for
             // the lyrics in there
index 42d485588fabfcb72c064c0f8748811d9e7fe334..5c39cb5c591a5650f784da6865e5dac99f644c26 100644 (file)
@@ -1,11 +1,43 @@
 import CommandLineKit
 import Foundation
 
+func main() {
+    let (flags, parser) = createParser()
+
+    do {
+        try parser.parse()
+    } catch {
+        parser.printUsage(error)
+        exit(EX_USAGE)
+    }
+
+    // Boolean Options
+
+    checkHelpFlag(flags["help"], withParser: parser)
+    checkVersionFlag(flags["version"], withParser: parser)
+    checkListSourcesFlag(flags["listSources"], withParser: parser)
+    checkTitleFlag(flags["title"], withParser: parser)
+
+    // String Options
+
+    checkEnableSourceFlag(flags["enableSource"], withParser: parser)
+    checkDisableSourceFlag(flags["disableSource"], withParser: parser)
+    checkResetSourceFlag(flags["resetSource"], withParser: parser)
+
+    // Remove any flags so anyone after this gets the unprocessed values
+
+    let programName: [String] = [CommandLine.arguments[0]]
+    CommandLine.arguments = programName + parser.unparsedArguments
+
+    // Run Lyricli
+
+    Lyricli.printLyrics()
+}
+
 /// Sets up and returns a new options parser
 /// 
 /// - Returns: A Dictionary of Options, and a new CommandLineKit instance
-func createParser() -> ([String:Option], CommandLineKit) {
-
+private func createParser() -> ([String:Option], CommandLineKit) {
     let parser = CommandLineKit()
     var flags: [String:Option] = [:]
 
@@ -25,7 +57,7 @@ func createParser() -> ([String:Option], CommandLineKit) {
 
         var formattedString: String
 
-        switch(type) {
+        switch type {
         case .About:
             formattedString = "\(parseString) [<artist_name> <song_name>]"
             break
@@ -39,71 +71,80 @@ func createParser() -> ([String:Option], CommandLineKit) {
     return (flags, parser)
 }
 
-func main() {
-
-    let (flags, parser) = createParser()
+// Handle the Help flag
 
-    do {
-        try parser.parse()
-    }
-    catch {
-        parser.printUsage(error)
-        exit(EX_USAGE)
-    }
-
-    if let helpFlag = flags["help"] as? BoolOption {
-        if helpFlag.value == true {
+private func checkHelpFlag(_ flag: Option?, withParser parser: CommandLineKit) {
+    if let helpFlag = flag as? BoolOption {
+        if helpFlag.value {
             parser.printUsage()
             exit(0)
         }
     }
+}
 
-    if let versionFlag = flags["version"] as? BoolOption {
-        if versionFlag.value == true {
+// Handle the version flag
+
+private func checkVersionFlag(_ flag: Option?, withParser parser: CommandLineKit) {
+    if let versionFlag = flag as? BoolOption {
+        if versionFlag.value {
             print(Lyricli.version)
             exit(0)
         }
     }
+}
 
-    if let listSourcesFlag = flags["listSources"] as? BoolOption {
-        if listSourcesFlag.value == true {
+// Handle the list sources flag
+
+private func checkListSourcesFlag(_ flag: Option?, withParser parser: CommandLineKit) {
+    if let listSourcesFlag = flag as? BoolOption {
+        if listSourcesFlag.value {
             Lyricli.printSources()
             exit(0)
         }
     }
+}
+
+// Handle the title flag
+
+private func checkTitleFlag(_ flag: Option?, withParser parser: CommandLineKit) {
+    if let titleFlag = flag as? BoolOption {
+        if titleFlag.value {
+            Lyricli.showTitle = true
+        }
+    }
+}
+
+// Handle the enable source flag
 
-    if let enableSourceFlag = flags["enableSource"] as? StringOption {
+private func checkEnableSourceFlag(_ flag: Option?, withParser parser: CommandLineKit) {
+    if let enableSourceFlag = flag as? StringOption {
         if let source = enableSourceFlag.value {
             Lyricli.enableSource(source)
             exit(0)
         }
     }
+}
+
+// Handle the disable source flag
 
-    if let disableSourceFlag = flags["disableSource"] as? StringOption {
+private func checkDisableSourceFlag(_ flag: Option?, withParser parser: CommandLineKit) {
+    if let disableSourceFlag = flag as? StringOption {
         if let source = disableSourceFlag.value {
             Lyricli.disableSource(source)
             exit(0)
         }
     }
+}
+
+// Handle the reset source flag
 
-    if let resetSourceFlag = flags["resetSource"] as? StringOption {
+private func checkResetSourceFlag(_ flag: Option?, withParser parser: CommandLineKit) {
+    if let resetSourceFlag = flag as? StringOption {
         if let source = resetSourceFlag.value {
             Lyricli.resetSource(source)
             exit(0)
         }
     }
-
-    if let titleFlag = flags["title"] as? BoolOption {
-        if titleFlag.value == true {
-            Lyricli.showTitle = true
-        }
-    }
-
-    // Remove any flags so anyone after this gets the unprocessed values
-    let programName: [String] = [CommandLine.arguments[0]]
-    CommandLine.arguments = programName + parser.unparsedArguments
-
-    Lyricli.printLyrics()
 }
 
 main()
index dd956ad3ca680418376cc06e1b494d14f511ac3b..e55cc8b18ae19e1acc222b442c6be788b21ea09c 100644 (file)
@@ -6,16 +6,13 @@ class SourceManager {
     ]
 
     var currentTrack: Track? {
-        get {
-
-            for source in enabledSources {
-                if let currentTrack = source.currentTrack {
-                    return currentTrack
-                }
+        for source in enabledSources {
+            if let currentTrack = source.currentTrack {
+                return currentTrack
             }
-
-            return nil
         }
+
+        return nil
     }
 
     var enabledSources: [Source] {
@@ -23,19 +20,17 @@ class SourceManager {
         // Checks the config and returns an array of sources based on the
         // enabled and available ones
 
-        get {
-            var sources = [Source]()
+        var sources = [Source]()
 
-            if let sourceNames = Configuration.sharedInstance["enabled_sources"] as? [String]{
-                for sourceName in sourceNames {
-                    if let source = availableSources[sourceName] {
-                        sources.append(source)
-                    }
+        if let sourceNames = Configuration.sharedInstance["enabled_sources"] as? [String] {
+            for sourceName in sourceNames {
+                if let source = availableSources[sourceName] {
+                    sources.append(source)
                 }
             }
-
-            return sources
         }
+
+        return sources
     }
 
     func enable(sourceName: String) {