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



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

def sets
  @sets
end

Class Method Details

.startObject



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

def self.start
  new.start
end

Instance Method Details

#catalogObject



82
83
84
# File 'lib/multisync/cli.rb', line 82

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

#optionsObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/multisync/cli.rb', line 90

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

#parserObject



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
# File 'lib/multisync/cli.rb', line 15

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 group/task separator.\n" +
               "       e.g. #{File.basename $0} nas/userdata"
    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

#runtimeObject



86
87
88
# File 'lib/multisync/cli.rb', line 86

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

#startObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/multisync/cli.rb', line 45

def start
  parser.parse!
  options[:quiet] = false if options[:print]
  
  @sets = ARGV

  if options[:list]
    # List tasks
    puts "Catalog: #{options[:file].color(:cyan)}"
    puts
    puts Multisync::List.new catalog

  else
    # Run tasks
    return if tasks.empty?
    begin
      tasks.each do |task|
        runtime.run task
      end
    rescue Interrupt => e
      $stderr.puts "\nAborted!".color(:red)
    end
    unless options[:print]
      puts
      puts
      puts Multisync::Summary.new tasks
    end
  end

  puts
end

#tasksObject



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

def tasks
  @_tasks ||= Multisync::Selector.new(catalog, sets).tasks
  # @_tasks ||= catalog.filter sets
end