lib/zold/commands/push.rb in zold-0.2 vs lib/zold/commands/push.rb in zold-0.3

- old
+ new

@@ -16,34 +16,52 @@ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +require 'slop' require 'net/http' -require_relative '../log.rb' -require_relative '../http.rb' +require_relative '../log' +require_relative '../id' +require_relative '../http' # PUSH command. # Author:: Yegor Bugayenko (yegor256@gmail.com) # Copyright:: Copyright (c) 2018 Yegor Bugayenko # License:: MIT module Zold # Wallet pushing command class Push - def initialize(wallet:, remotes:, log: Log::Quiet.new) - @wallet = wallet + def initialize(wallets:, remotes:, log: Log::Quiet.new) + @wallets = wallets @remotes = remotes @log = log end - def run(_ = []) - raise 'The wallet is absent' unless @wallet.exists? + def run(args = []) + opts = Slop.parse(args, help: true) do |o| + o.banner = "Usage: zold push [ID...] [options] +Available options:" + o.bool '--help', 'Print instructions' + end + if opts.help? + @log.info(opts.to_s) + return + end + raise 'At least one wallet ID is required' if opts.arguments.empty? + opts.arguments.each do |id| + push(@wallets.find(Id.new(id)), opts) + end + end + + def push(wallet, _) + raise 'The wallet is absent' unless wallet.exists? remote = @remotes.all[0] - uri = URI("#{remote[:home]}/wallet/#{@wallet.id}") - response = Http.new(uri).put(File.read(@wallet.path)) + uri = URI("#{remote[:home]}/wallet/#{wallet.id}") + response = Http.new(uri).put(File.read(wallet.path)) unless response.code == '200' raise "Failed to push to #{uri}: #{response.code}/#{response.message}" end - @log.info("The #{@wallet.id} pushed to #{uri}") + @log.info("The #{wallet.id} pushed to #{uri}") end end end