Class: Zold::Remote
- Inherits:
-
Object
- Object
- Zold::Remote
- Defined in:
- lib/zold/commands/remote.rb
Overview
Remote command
Instance Method Summary collapse
- #add(host, port) ⇒ Object
- #clean ⇒ Object
-
#initialize(remotes:, log: Log::Quiet.new) ⇒ Remote
constructor
A new instance of Remote.
- #remove(host, port) ⇒ Object
- #reset ⇒ Object
- #run(args = []) ⇒ Object
- #show ⇒ Object
- #update(opts) ⇒ Object
Constructor Details
Instance Method Details
#add(host, port) ⇒ Object
101 102 103 104 105 |
# File 'lib/zold/commands/remote.rb', line 101 def add(host, port) @remotes.add(host, port) @log.info("#{host}:#{port} added to the list") @log.info("There are #{@remotes.all.count} remote nodes in the list") end |
#clean ⇒ Object
91 92 93 94 |
# File 'lib/zold/commands/remote.rb', line 91 def clean @remotes.clean @log.debug('All remote nodes deleted') end |
#remove(host, port) ⇒ Object
107 108 109 110 111 |
# File 'lib/zold/commands/remote.rb', line 107 def remove(host, port) @remotes.remove(host, port) @log.info("#{host}:#{port} removed from the list") @log.info("There are #{@remotes.all.count} remote nodes in the list") end |
#reset ⇒ Object
96 97 98 99 |
# File 'lib/zold/commands/remote.rb', line 96 def reset @remotes.reset @log.debug('Remote nodes set back to default') end |
#run(args = []) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/zold/commands/remote.rb', line 43 def run(args = []) opts = Slop.parse(args, help: true) do |o| o. = "Usage: zold remote <command> [options] Available commands: #{Rainbow('remote show').green} Show all registered remote nodes #{Rainbow('remote clean').green} Remove all registered remote nodes #{Rainbow('remote reset').green} Restore it back to the default list of nodes #{Rainbow('remote add').green} host port Add a new remote node #{Rainbow('remote remove').green} host port Remove the remote node #{Rainbow('remote update').green} Check each registered remote node for availability Available options:" o.bool '--ignore-score-weakness', 'Don\'t complain when their score is too weak', default: false o.bool '--help', 'Print instructions' end command = args[0] case command when 'show' show when 'clean' clean when 'reset' reset when 'add' add(opts.arguments[1], opts.arguments[2].to_i) when 'remove' remove(opts.arguments[1], opts.arguments[2].to_i) when 'update' update(opts) else @log.info(opts.to_s) end end |
#show ⇒ Object
84 85 86 87 88 89 |
# File 'lib/zold/commands/remote.rb', line 84 def show @remotes.all.each do |r| score = Rainbow("/#{r[:score]}").color(r[:score] > 0 ? :green : :red) @log.info(r[:host] + Rainbow(":#{r[:port]}").gray + score) end end |
#update(opts) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/zold/commands/remote.rb', line 113 def update(opts) @remotes.all.each do |r| uri = URI("#{r[:home]}remotes") res = Http.new(uri).get unless res.code == '200' @remotes.remove(r[:host], r[:port]) @log.info( "#{Rainbow(r[:host]).red} #{res.code} \"#{res.}\" #{uri}" ) next end begin json = JSON.parse(res.body) rescue JSON::ParserError => e remove(r[:host], r[:port]) @log.info("#{Rainbow(r[:host]).red} \"#{e.}\": #{res.body}") next end score = Score.new( Time.parse(json['score']['time']), r[:host], r[:port], json['score']['suffixes'] ) unless score.valid? remove(r[:host], r[:port]) @log.info("#{Rainbow(r[:host]).red} invalid score") next end if score.strength < Score::STRENGTH && !opts['ignore-score-weakness'] remove(r[:host], r[:port]) @log.info( "#{Rainbow(r[:host]).red} score too weak: #{score.strength}" ) next end @remotes.rescore(r[:host], r[:port], score.value) json['all'].each do |s| unless @remotes.exists?(s['host'], s['port']) add(s['host'], s['port']) end end @log.info("#{r[:host]}:#{r[:port]}: #{Rainbow(score.value).green} \ (v.#{json['version']})") end total = @remotes.all.size if total.zero? @log.debug("The list of remotes is #{Rainbow('empty').red}!") @log.debug( "Run 'zold remote add b1.zold.io 80` and then `zold update`" ) else @log.debug("There are #{total} known remotes") end end |