lib/zold/commands/create.rb in zold-0.26.12 vs lib/zold/commands/create.rb in zold-0.26.13

- old
+ new

@@ -35,12 +35,13 @@ module Zold # Create command class Create prepend ThreadBadge - def initialize(wallets:, log: Log::NULL) + def initialize(wallets:, remotes:, log: Log::NULL) @wallets = wallets + @remotes = remotes @log = log end def run(args = []) opts = Slop.parse(args, help: true, suppress_errors: true) do |o| @@ -48,20 +49,37 @@ Available options:" o.string '--public-key', 'The location of RSA public key (default: ~/.ssh/id_rsa.pub)', require: true, default: File.expand_path('~/.ssh/id_rsa.pub') + o.bool '--skip-test', + 'Don\'t check whether this wallet ID is available', + default: false o.string '--network', "The name of the network (default: #{Wallet::MAINET}", require: true, default: Wallet::MAINET o.bool '--help', 'Print instructions' end mine = Args.new(opts, @log).take || return - create(mine.empty? ? Id.new : Id.new(mine[0]), opts) + create(mine.empty? ? create_id(opts) : Id.new(mine[0]), opts) end private + + def create_id(opts) + loop do + id = Id.new + return id if opts['skip-test'] + found = false + @remotes.iterate(@log) do |r| + head = r.http("/wallet/#{id}/digest").get + found = true if head.status == 200 + end + return id unless found + @log.info("Wallet ID #{id} is already occupied, will try another one...") + end + end def create(id, opts) key = Zold::Key.new(file: opts['public-key']) @wallets.acq(id, exclusive: true) do |wallet| wallet.init(id, key, network: opts['network'])