Class: Zold::Diff

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

Overview

DIFF pulling command

Instance Method Summary collapse

Constructor Details

#initialize(wallets:, copies:, log: Log::Quiet.new) ⇒ Diff

Returns a new instance of Diff.



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

def initialize(wallets:, copies:, log: Log::Quiet.new)
  @wallets = wallets
  @copies = copies
  @log = log
end

Instance Method Details

#diff(wallet, cps, _) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/zold/commands/diff.rb', line 62

def diff(wallet, cps, _)
  raise 'There are no remote copies, try FETCH first' if cps.all.empty?
  cps = cps.all.sort_by { |c| c[:score] }.reverse
  patch = Patch.new
  patch.start(Wallet.new(cps[0][:path]))
  cps[1..-1].each do |c|
    patch.join(Wallet.new(c[:path]))
  end
  before = File.read(wallet.path)
  after = ''
  Tempfile.open do |f|
    patch.save(f, overwrite: true)
    after = File.read(f)
  end
  diff = Diffy::Diff.new(before, after, context: 0).to_s(:color)
  @log.info(diff)
  diff
end

#run(args = []) ⇒ Object



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

def run(args = [])
  opts = Slop.parse(args, help: true, suppress_errors: true) do |o|
    o.banner = "Usage: zold diff [ID...] [options]
Available options:"
    o.bool '--help', 'Print instructions'
  end
  mine = Args.new(opts, @log).take || return
  raise 'At least one wallet ID is required' if mine.empty?
  stdout = ''
  mine.each do |id|
    stdout += diff(
      @wallets.find(Id.new(id)),
      Copies.new(File.join(@copies, id)),
      opts
    )
  end
  stdout
end