Class: ConfigmonkeyCli::Application::ManifestAction::Rsync

Inherits:
Base
  • Object
show all
Defined in:
lib/configmonkey_cli/application/manifest_actions/rsync.rb

Instance Attribute Summary

Attributes inherited from Base

#app, #args, #manifest, #opts, #thor

Instance Method Summary collapse

Methods inherited from Base

#args_and_opts, #exists?, #expand_dst, #expand_src, #initialize, #rel, #to_s

Constructor Details

This class inherits a constructor from ConfigmonkeyCli::Application::ManifestAction::Base

Instance Method Details

#destructiveObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/configmonkey_cli/application/manifest_actions/rsync.rb', line 26

def destructive
  if @opts[:preview] && !(app.opts[:default_yes] || app.opts[:default_accept])
    preview = do_sync(true)
    render_sync(preview)
    if preview.any?
      if manifest.yes?("Apply changes?", default: @opts[:preview] == true || [:y, :yes].include?(@opts[:preview].to_sym))
        render_sync do_sync
      end
    end
  else
    render_sync do_sync
  end
end

#do_sync(force_dry = false) ⇒ Object



92
93
94
95
96
97
# File 'lib/configmonkey_cli/application/manifest_actions/rsync.rb', line 92

def do_sync force_dry = false
  cmd = rsync_command(@source, @destination, force_dry)
  code, res = exec(cmd)
  raise "rsync exited with status #{code}: #{res}" unless code.exitstatus == 0
  res.split("\n").map{|l| l.split(" ", 2) }
end

#exec(cmd, chomp = true) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/configmonkey_cli/application/manifest_actions/rsync.rb', line 99

def exec cmd, chomp = true
  app.debug "§invoking:#{cmd}"
  _stdin, _stdouterr, _thread = Open3.popen2e(cmd)
  _thread.join
  res = _stdouterr.read
  [_thread.value, chomp ? res.chomp : res]
ensure
  _stdin.close rescue false
  _stdouterr.close rescue false
end

#init(hargs_and_opts = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/configmonkey_cli/application/manifest_actions/rsync.rb', line 5

def init hargs_and_opts = {}
  @args, @opts = args_and_opts(hargs_and_opts)
  @opts = @opts.reverse_merge({
    binary: "rsync",
    delete: true,
    delay: true,
    preview: false,
    flags: [],
  })
end

#prepareObject



16
17
18
19
20
# File 'lib/configmonkey_cli/application/manifest_actions/rsync.rb', line 16

def prepare
  @source = @args[0]
  @destination = @args[1]
  status :synchronize, :green, "#{@source} => #{@destination}"
end

#render_sync(ary) ⇒ Object



86
87
88
89
90
# File 'lib/configmonkey_cli/application/manifest_actions/rsync.rb', line 86

def render_sync ary
  ary.each do |mode, file|
    status mode, status_color_for_mode(mode), file, status_color_from_mode(mode)
  end
end

#rsync_command(src, dst, force_dry = false) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/configmonkey_cli/application/manifest_actions/rsync.rb', line 55

def rsync_command src, dst, force_dry = false
  [
    @opts[:binary],
    str_flags(force_dry),
    Shellwords.escape(expand_src(src)),
    Shellwords.escape(expand_dst(dst))
  ].join(" ")
end

#simulateObject



22
23
24
# File 'lib/configmonkey_cli/application/manifest_actions/rsync.rb', line 22

def simulate
  render_sync(do_sync(true))
end

#status_color_for_mode(mode) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/configmonkey_cli/application/manifest_actions/rsync.rb', line 76

def status_color_for_mode mode
  if mode[0] == ">" || mode[0..1] == "cd"
    :green
  elsif mode == "*deleting"
    :red
  else
    :yellow
  end
end

#status_color_from_mode(mode) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/configmonkey_cli/application/manifest_actions/rsync.rb', line 64

def status_color_from_mode mode
  if mode[1] == "f"
    :white
  elsif mode == "*deleting"
    :red
  elsif mode[1] == "d"
    :blue
  else
    :white
  end
end

#str_flags(force_dry = false) ⇒ Object




42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/configmonkey_cli/application/manifest_actions/rsync.rb', line 42

def str_flags force_dry = false
  ([].tap{|f|
    f << "--archive"
    f << "--whole-file" # better I guess?
    f << "--dry-run" if force_dry || app.opts[:simulation]
    f << "--itemize-changes" # for parsing and display
    f << "--delay-updates" if @opts[:delay]
    if @opts[:delete]
      f << (@opts[:delay] ? "--delete-#{@opts[:delay].is_a?(String) ? @opts[:delay] : "delay"}" : "--delete")
    end
  }.compact + @opts[:flags]).join(" ")
end