Class: Multisync::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/multisync/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#setsObject (readonly)

Given sets to run or empty



12
13
14
# File 'lib/multisync/cli.rb', line 12

def sets
  @sets
end

Class Method Details

.startObject



7
8
9
# File 'lib/multisync/cli.rb', line 7

def self.start
  new.start
end

Instance Method Details

#catalogObject



108
109
110
# File 'lib/multisync/cli.rb', line 108

def catalog
  @_catalog ||= Multisync::Catalog.new options[:file]
end

#list_definitionsObject



60
61
62
63
64
65
# File 'lib/multisync/cli.rb', line 60

def list_definitions
  puts "Catalog: #{options[:file].color(:cyan)}"
  table = Terminal::Table.new(rows: catalog.list, style: table_style)
  puts
  puts table
end

#optionsObject



116
117
118
119
120
121
122
123
124
125
# File 'lib/multisync/cli.rb', line 116

def options
  @_options ||= { 
    list: false,
    print: false,
    dryrun: false,
    quiet: false,
    file: Multisync::Catalog.default_catalog_path,
    timeout: 31536000,
  }
end

#parserObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/multisync/cli.rb', line 14

def parser
  OptionParser.new do |o|
    o.banner = "\nRun rsync jobs defined in the catalog file '#{options[:file]}'.\n\n"+
               "Usage: #{File.basename $0} [options] [SET] [...]\n\n"+
               "       SET selects a section from the catalog (see option -l)\n"+
               "       use / as a follow:\n"+
               "       work/pictures to specify the sync defined in the group work and\n"+
               "       home/pictures to specify the sync defined in the group home\n"+
               "       pictures alone will select both syncs, the one in the group work\n"+
               "       as well as the one in the group home"
    o.separator ''
    o.on('-l', '--list', "List the catalog") do
      options[:list] = true
    end
    o.on('-p', '--print', "Print the commands without executing them") do
      options[:print] = true
    end
    o.on('-q', '--quiet', "Show only rsync summary") do
      options[:quiet] = true
    end
    o.on('--catalog FILE', "Specify a catalog", "Default is #{options[:file]}") do |file|
      options[:file] = file
    end
    o.on('--timeout SECS', Integer, "Timeout for rsync job", "Default is #{options[:timeout]}") do |timeout|
      options[:timeout] = timeout
    end
    o.on('-n', '--dryrun', "Run rsync in dry-run mode") do
      options[:dryrun] = true
    end
    o.separator ''
  end
end

#run_tasksObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/multisync/cli.rb', line 67

def run_tasks
  tasks.each do |task|
    runtime.run task
  end
  return if options[:print]
  table = Terminal::Table.new(headings: summary_headings, rows: summary_data, style: table_style)
  puts
  puts
  puts table
end

#runtimeObject



112
113
114
# File 'lib/multisync/cli.rb', line 112

def runtime
  @_runtime ||= Multisync::Runtime.new(options)
end

#startObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/multisync/cli.rb', line 47

def start
  parser.parse!
  @sets = ARGV
  
  case
  when options[:list]
    list_definitions
  else
    run_tasks
  end
  puts
end

#summary_dataObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/multisync/cli.rb', line 82

def summary_data
  tasks.map do |task|
    result = task.result
    desc = [task.source_description, "--> #{task.destination_description}"]

    case result[:action]
    when :run
      if result[:status].success?
        # successfull run
        stat = Multisync::RsyncStat.new(result[:stdout]).parse
        [*desc, *stat.to_a.map{|e| {value: e.color(:green), alignment: :right} } ]
      else
        # failed run
        [*desc, { value: result[:stderr].strip.color(:red), colspan: 6 } ]
      end
    when :skip
      # skiped sync
      [*desc, { value: result[:skip_message].color(:yellow), colspan: 6 } ]
    end
  end
end

#summary_headingsObject



78
79
80
# File 'lib/multisync/cli.rb', line 78

def summary_headings
  %w( Source Destination Files + - → ∑ ↑ ).zip(i( left left right right right right right right )).map{|v,a| {value: v, alignment: a} }
end

#table_styleObject



127
128
129
# File 'lib/multisync/cli.rb', line 127

def table_style
  { border_top: false,  border_bottom: false, border_x: '–', border_y: '', border_i: '', padding_left: 0, padding_right: 3 }
end

#tasksObject



104
105
106
# File 'lib/multisync/cli.rb', line 104

def tasks
  @_tasks ||= catalog.filter sets
end