Class: Multisync::Cli
- Inherits:
-
Object
- Object
- Multisync::Cli
- Defined in:
- lib/multisync/cli.rb
Instance Attribute Summary collapse
-
#sets ⇒ Object
readonly
Given sets to run or empty.
Class Method Summary collapse
Instance Method Summary collapse
- #catalog ⇒ Object
- #list_definitions ⇒ Object
- #options ⇒ Object
- #parser ⇒ Object
- #run_tasks ⇒ Object
- #runtime ⇒ Object
- #start ⇒ Object
- #summary_data ⇒ Object
- #summary_headings ⇒ Object
- #table_style ⇒ Object
- #tasks ⇒ Object
Instance Attribute Details
#sets ⇒ Object (readonly)
Given sets to run or empty
12 13 14 |
# File 'lib/multisync/cli.rb', line 12 def sets @sets end |
Class Method Details
.start ⇒ Object
7 8 9 |
# File 'lib/multisync/cli.rb', line 7 def self.start new.start end |
Instance Method Details
#catalog ⇒ Object
108 109 110 |
# File 'lib/multisync/cli.rb', line 108 def catalog @_catalog ||= Multisync::Catalog.new [:file] end |
#list_definitions ⇒ Object
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 |
#options ⇒ Object
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/multisync/cli.rb', line 116 def ||= { list: false, print: false, dryrun: false, quiet: false, file: Multisync::Catalog.default_catalog_path, timeout: 31536000, } end |
#parser ⇒ Object
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. = "\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 [:list] = true end o.on('-p', '--print', "Print the commands without executing them") do [:print] = true end o.on('-q', '--quiet', "Show only rsync summary") do [:quiet] = true end o.on('--catalog FILE', "Specify a catalog", "Default is #{options[:file]}") do |file| [:file] = file end o.on('--timeout SECS', Integer, "Timeout for rsync job", "Default is #{options[:timeout]}") do |timeout| [:timeout] = timeout end o.on('-n', '--dryrun', "Run rsync in dry-run mode") do [:dryrun] = true end o.separator '' end end |
#run_tasks ⇒ Object
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 [:print] table = Terminal::Table.new(headings: summary_headings, rows: summary_data, style: table_style) puts puts puts table end |
#runtime ⇒ Object
112 113 114 |
# File 'lib/multisync/cli.rb', line 112 def runtime @_runtime ||= Multisync::Runtime.new() end |
#start ⇒ Object
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 [:list] list_definitions else run_tasks end puts end |
#summary_data ⇒ Object
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_headings ⇒ Object
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_style ⇒ Object
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 |
#tasks ⇒ Object
104 105 106 |
# File 'lib/multisync/cli.rb', line 104 def tasks @_tasks ||= catalog.filter sets end |