Class: Zold::Remove

Inherits:
Object
  • Object
show all
Includes:
ThreadBadge
Defined in:
lib/zold/commands/remove.rb

Overview

REMOVE command

Instance Method Summary collapse

Constructor Details

#initialize(wallets:, log: Log::NULL) ⇒ Remove

Returns a new instance of Remove.



38
39
40
41
# File 'lib/zold/commands/remove.rb', line 38

def initialize(wallets:, log: Log::NULL)
  @wallets = wallets
  @log = log
end

Instance Method Details

#remove(id, opts) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/zold/commands/remove.rb', line 59

def remove(id, opts)
  @wallets.acq(id, exclusive: true) do |w|
    if w.exists?
      File.delete(w.path)
    else
      raise "Wallet #{id} doesn't exist in #{w.path}" unless opts['force']
      @log.info("Wallet #{id} file not found in #{w.path}")
    end
  end
  @log.info("Wallet #{id} removed")
end

#run(args = []) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/zold/commands/remove.rb', line 43

def run(args = [])
  @log.debug("Remove.run(#{args.join(' ')})")
  opts = Slop.parse(args, help: true, suppress_errors: true) do |o|
    o.banner = "Usage: zold remove [ID...] [options]
Available options:"
    o.bool '--force',
      'Don\'t report any errors if the wallet doesn\'t exist',
      default: false
    o.bool '--help', 'Print instructions'
  end
  mine = Args.new(opts, @log).take || return
  (mine.empty? ? @wallets.all : mine.map { |i| Id.new(i) }).each do |id|
    remove(id, opts)
  end
end