Class: Zold::Clean

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/commands/clean.rb

Overview

CLEAN command

Instance Method Summary collapse

Constructor Details

#initialize(wallets:, copies:, log: Log::NULL) ⇒ Clean

Returns a new instance of Clean.



46
47
48
49
50
# File 'lib/zold/commands/clean.rb', line 46

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

Instance Method Details

#clean(cps, opts) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/zold/commands/clean.rb', line 71

def clean(cps, opts)
  start = Time.now
  deleted = cps.clean(max: opts['max-age'] * 60 * 60)
  list = cps.all.map do |c|
    wallet = Wallet.new(c[:path])
    "#{c[:name]}: #{c[:score]} #{c[:total]}n #{wallet.mnemo} \
#{Size.new(File.size(c[:path]))}/#{Age.new(File.mtime(c[:path]))}#{c[:master] ? ' master' : ''}"
  end
  @log.debug(
    "#{deleted} expired local copies removed for #{cps} \
in #{Age.new(start, limit: 0.01)}, \
#{list.empty? ? 'nothing left' : "#{list.count} left:\n#{list.join("\n")}"}"
  )
end

#run(args = []) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/zold/commands/clean.rb', line 52

def run(args = [])
  opts = Slop.parse(args, help: true, suppress_errors: true) do |o|
    o.banner = "Usage: zold clean [ID...] [options]
Available options:"
    o.integer '--threads',
      'How many threads to use for cleaning copies (default: 1)',
      default: 1
    o.integer '--max-age',
      'Maximum age for a copy to stay, in hours (default: 24)',
      default: 24
    o.bool '--help', 'Print instructions'
  end
  mine = Args.new(opts, @log).take || return
  list = mine.empty? ? @wallets.all : mine.map { |i| Id.new(i) }
  Hands.exec(opts['threads'], list.uniq) do |id|
    clean(Copies.new(File.join(@copies, id), log: @log), opts)
  end
end