Class: Redsync::CLI

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

Class Method Summary collapse

Class Method Details

.check_config_fileObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/redsync/cli.rb', line 60

def check_config_file
  if !File.exist? @options[:config_file]
    Redsync::CLI.confirm("Config file #{@options[:config_file]} doesn't exist. Create?") do 
      FileUtils.cp("config.yml.dist", @options[:config_file]) 
      puts "Creating config file in #{@options[:config_file]}."
      puts "Edit it and call me again when you're done."
    end
    exit
  end
end

.confirm(question, default_yes = true, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/redsync/cli.rb', line 72

def confirm(question, default_yes = true, &block)
  print question
  print (default_yes ? " [Y/n] " : " [N/y] ")
  c = gets.strip
  result = 
    if c =~ /^$/
      default_yes
    else
      c =~ /^y/i
    end
  (result && block) ? block.call : result
end

.parse_optionsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/redsync/cli.rb', line 28

def parse_options
  @options = {
    :run_mode => :full_sync,
    :config_file => "~/redsync.yml",
  }

  OptionParser.new do |opts|
    opts.banner = "Usage: redsync [options]"
    opts.on("-v", "--verbose", "Output verbose logs") do |v|
      @options[:verbose] = v
    end
    opts.on("-c", "--config FILE", "Use specified config file instead of ~/redsync.yml") do |file|
      @options[:config_file] = file
    end
    opts.on("-s", "--status", "Status check. No uploads or downloads will happen") do |v|
      @options[:run_mode] = :status_check
    end
    opts.on("-i", "--interactive", "Interactive mode (pry)") do |v|
      @options[:run_mode] = :interactive
    end
  end.parse!

  if @options[:debug]
    require 'ruby-debug'
    Debugger.settings[:autoeval] = true
    Debugger.settings[:reload_source_on_change] = true
  end

  @options[:config_file] = File.expand_path(@options[:config_file])
end

.runObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/redsync/cli.rb', line 9

def run
  parse_options
  check_config_file

  redsync = Redsync.new(YAML.load_file(@options.delete(:config_file)).merge(@options))

  case @options[:run_mode]
  when :full_sync
    time do
      redsync.sync_all
    end
  when :interactive
    redsync.interactive
  when :status_check
    redsync.status_check
  end
end

.time(&block) ⇒ Object



86
87
88
89
90
# File 'lib/redsync/cli.rb', line 86

def time(&block)
  start = Time.now
  yield
  puts "Finished in #{Time.now - start} seconds."
end