Class: Constancy::CLI::PushCommand

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

Class Method Summary collapse

Class Method Details

.run(args) ⇒ Object



7
8
9
10
11
12
13
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
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
# File 'lib/constancy/cli/push_command.rb', line 7

def run(args)
  Constancy::CLI.configure
  STDOUT.sync = true

  Constancy.config.sync_targets.each do |target|
    diff = target.diff(:push)

    diff.print_report

    if not diff.any_changes?
      puts
      puts "Everything is in sync. No changes need to be made to this sync target."
      next
    end

    puts
    puts "Do you want to push these changes?"
    print "  Enter '" + "yes".bold + "' to continue: "
    answer = args.include?('--yes') ? 'yes' : gets.chomp

    if answer.downcase != "yes"
      puts
      puts "Push cancelled. No changes will be made to this sync target."
      next
    end

    puts
    diff.items_to_change.each do |item|
      case item.op
      when :create
        print "CREATE".bold.green + " " + item.consul_key
        resp = target.consul.put(item.consul_key, item.local_content, dc: target.datacenter)
        if resp.success?
          puts "   OK".bold
        else
          puts "   ERROR".bold.red
        end

      when :update
        print "UPDATE".bold.blue + " " + item.consul_key
        resp = target.consul.put(item.consul_key, item.local_content, dc: target.datacenter)
        if resp.success?
          puts "   OK".bold
        else
          puts "   ERROR".bold.red
        end

      when :delete
        print "DELETE".bold.red + " " + item.consul_key
        resp = target.consul.delete(item.consul_key, dc: target.datacenter)
        if resp.success?
          puts "   OK".bold
        else
          puts "   ERROR".bold.red
        end

      else
        if Constancy.config.verbose?
          STDERR.puts "constancy: WARNING: unexpected operation '#{item.op}' for #{item.consul_key}"
          next
        end

      end
    end
  end
end