lib/zold/commands/pay.rb in zold-0.6.3 vs lib/zold/commands/pay.rb in zold-0.6.4

- old
+ new

@@ -17,10 +17,12 @@ # 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 'rainbow' +require_relative 'args' require_relative '../log' # PAY command. # Author:: Yegor Bugayenko (yegor256@gmail.com) # Copyright:: Copyright (c) 2018 Yegor Bugayenko @@ -33,14 +35,14 @@ @log = log end def run(args = []) opts = Slop.parse(args, help: true, suppress_errors: true) do |o| - o.banner = "Usage: zold pay wallet invoice amount [details] [options] + o.banner = "Usage: zold pay wallet target amount [details] [options] Where: 'wallet' is the sender's wallet ID - 'invoice' is the beneficiary's invoice number, generated by 'zold invoice' + 'target' is the beneficiary (either wallet ID or invoice number)' 'amount' is the amount to pay, in ZLD, for example '14.95' 'details' is the optional text to attach to the payment Available options:" o.string '--private-key', 'The location of RSA private key (default: ~/.ssh/id_rsa)', @@ -49,19 +51,19 @@ o.bool '--force', 'Ignore all validations', default: false o.bool '--help', 'Print instructions' end - if opts.help? - @log.info(opts.to_s) - return - end - mine = opts.arguments[1..-1] + mine = Args.new(opts, @log).take || return raise 'Payer wallet ID is required as the first argument' if mine[0].nil? from = @wallets.find(Zold::Id.new(mine[0])) raise 'Wallet doesn\'t exist, do \'fetch\' first' unless from.exists? - raise 'Recepient\'s invoice is required as the second argument' if mine[1].nil? + raise 'Recepient\'s invoice or wallet ID is required as the second argument' if mine[1].nil? invoice = mine[1] + unless invoice.include?('@') + require_relative 'invoice' + invoice = Invoice.new(wallets: @wallets, log: @log).run(['invoice', invoice]) + end raise 'Amount is required (in ZLD) as the third argument' if mine[2].nil? amount = Zold::Amount.new(zld: mine[2].to_f) details = mine[3] ? mine[3] : '-' pay(from, invoice, amount, details, opts) end