Class: Capistrano::Chocopoche::SyncCLI

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano/chocopoche/sync-cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ SyncCLI

Returns a new instance of SyncCLI.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/capistrano/chocopoche/sync-cli.rb', line 36

def initialize(args)
  @args    = args
  @options = {}
  @parser  = OptionParser.new do |opts|
    opts.banner  = "Usage: files-sync [options] DATATYPE SOURCE DEST \n"
    opts.banner += "Sync DATATYPE from the SOURCE stage to the DEST stage.\n"
    opts.on("-y", "--no-confirmation", "Don't prompt for confirmation") do |v|
      @options[:no_confirmation] = v
    end

    opts.on("--no-backup", "Dont backup databases before importing") do |v|
      @options[:no_backup] = v
    end
  end
end

Instance Method Details

#ensure_confirmationObject



22
23
24
25
26
27
28
29
# File 'lib/capistrano/chocopoche/sync-cli.rb', line 22

def ensure_confirmation
  unless @options[:no_confirmation] == false
    confirm = prompt('y', "Sync from #{@source} to #{@dest}? [Y/n] ").downcase
    unless confirm == "y"
      abort("Cancelled")
    end
  end
end

#ensure_stagesObject



12
13
14
15
16
17
18
19
20
# File 'lib/capistrano/chocopoche/sync-cli.rb', line 12

def ensure_stages
  show_error("The DATATYPE is missing.")       if @args[0].nil?
  show_error("The DATATYPE is incorrect.") unless @args[0] =~ /^files|mysql$/
  show_error("The SOURCE is missing.")         if @args[1].nil?
  show_error("The DEST is missing.")           if @args[2].nil?
  @data   = @args[0]
  @source = @args[1]
  @dest   = @args[2]
end

#executeObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/capistrano/chocopoche/sync-cli.rb', line 52

def execute
  @parser.parse!
  ensure_stages
  ensure_confirmation
  if @data == 'mysql'
    Capistrano::CLI.parse([@dest,   "mysql:dump"]).execute! unless @options[:no_backup]
    Capistrano::CLI.parse([@source, "mysql:dump",   "mysql:download"]).execute!
    Capistrano::CLI.parse([@dest,   "mysql:upload", "mysql:import"]).execute!
  elsif @data == 'files'
    Capistrano::CLI.parse([@source, "files:download"]).execute!
    Capistrano::CLI.parse([@dest,   "files:upload"]).execute!
  end
end

#prompt(default, *args) ⇒ Object



6
7
8
9
10
# File 'lib/capistrano/chocopoche/sync-cli.rb', line 6

def prompt(default, *args)
  print(*args)
  result = STDIN.gets.strip
  return result.empty? ? default : result
end

#show_error(msg) ⇒ Object



31
32
33
34
# File 'lib/capistrano/chocopoche/sync-cli.rb', line 31

def show_error(msg)
  puts @parser.help
  abort("\n[ERROR] #{msg}")
end