github: GithubWallet uses logger
authorAlexandre Terrasa <alexandre@moz-code.org>
Tue, 9 Jul 2019 00:57:24 +0000 (20:57 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Thu, 11 Jul 2019 02:07:01 +0000 (22:07 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

lib/github/loader.nit
lib/github/wallet.nit

index b5fd29b..fe8e0bc 100644 (file)
@@ -120,9 +120,7 @@ class LoaderConfig
 
        # Github tokens wallet\13
        var wallet: GithubWallet is lazy do
-               var wallet = new GithubWallet(tokens)
-               wallet.no_colors = no_colors
-               return wallet
+               return new GithubWallet(tokens)
        end
 
        # Use colors in console display
@@ -261,7 +259,7 @@ class Loader
        end
 
        # Show wallet status
-       fun show_wallet do config.wallet.show_status
+       fun show_wallet do config.wallet.show_status(config.no_colors)
 
        # Show jobs status
        fun show_jobs do
index 4737410..000d042 100644 (file)
@@ -81,7 +81,7 @@
 module wallet
 
 import github
-import console
+import logger
 
 # Github OAuth tokens wallet
 class GithubWallet
@@ -89,11 +89,8 @@ class GithubWallet
        # Github API tokens
        var tokens = new Array[String] is optional
 
-       # Do not use colors in console output
-       var no_colors = false is writable
-
-       # Display debug information about the token processing
-       var verbose = false is writable
+       # Logger used to display info about tokens state
+       var logger = new Logger is optional, writable
 
        # Add a new token in the wallet
        fun add(token: String) do tokens.add token
@@ -104,14 +101,14 @@ class GithubWallet
        fun api: GithubAPI do
                var token
                if tokens.is_empty then
-                       message "No tokens, using `get_github_oauth`"
+                       logger.warn "No tokens, using `get_github_oauth`"
                        token = get_github_oauth
                else
                        token = get_next_token
                        var tried = 0
                        while not check_token(token) do
                                if tried >= tokens.length - 1 then
-                                       message "Exhausted all tokens, using {token}"
+                                       logger.warn "Exhausted all tokens, using {token}"
                                        break
                                end
                                tried += 1
@@ -148,17 +145,16 @@ class GithubWallet
 
        # Check if a token is valid
        fun check_token(token: String): Bool do
-               message "Try token {token}"
+               logger.debug "Try token {token}"
                var api = new GithubAPI(token)
                api.get_auth_user
                return not api.was_error
        end
 
-       # Print a message depending on `verbose`
-       fun message(message: String) do if verbose then print "[Github Wallet] {message}"
-
        # Show wallet status in console
-       fun show_status do
+       fun show_status(no_color: nullable Bool) do
+               no_color = no_color or else false
+
                if tokens.is_empty then
                        print "Wallet is empty"
                        return
@@ -167,9 +163,9 @@ class GithubWallet
                for token in tokens do
                        var status
                        if check_token(token) then
-                               status = if no_colors then "OK" else "OK".green
+                               status = if no_color then "OK" else "OK".green
                        else
-                               status = if no_colors then "KO" else "KO".red
+                               status = if no_color then "KO" else "KO".red
                        end
                        print " * [{status}] {token}"
                end