Class: Zeta::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/zeta/runner.rb

Constant Summary collapse

COMMANDS =
{
  'full_check'             => 'Update contracts and validate infrastructure',
  'validate'               => 'Validate the architecture in the contracts cache dir',
  'update_own_contracts'   => 'Update your own contracts in the contracts cache dir',
  'fetch_remote_contracts' => 'Download remote contracts and update your own contracts in the contracts cache dir'
}

Class Method Summary collapse

Class Method Details

.runObject



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/zeta/runner.rb', line 12

def self.run
  options = {}
  parser = OptionParser.new do |opts|
    opts.banner = "#{'Usage:'.red} zeta [options] command"

    opts.separator ""
    opts.separator "Commands:".yellow

    longest_command = COMMANDS.keys.map(&:length).sort.last + 1
    command_list = []
    COMMANDS.each do |cmd, desc|
      padded_cmd = "#{cmd}:".ljust(longest_command, " ")
      command_list << "    #{padded_cmd} #{desc}"
    end
    opts.separator command_list

    opts.separator ""
    opts.separator "Specific options:".yellow

    opts.on("-c CONFIG_FILE", "--config=CONFIG_FILE", "Config file (default: config/zeta.yml)") do |c|
      options[:config_file] = c
    end

    opts.on("-e ENVIRONMENT", "--env=ENVIRONMENT", "Environment (default: RAILS_ENV, if it is set)") do |e|
      options[:env] = e
    end

    opts.on("-s", "--silent", "No output, just an appropriate return code") do |s|
      options[:silent] = s
    end

    opts.on("-t", "--trace", "Print exception stack traces") do |t|
      options[:trace] = t
    end

    opts.separator ""
    opts.separator "Common options:".yellow

    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end

    # Another typical switch to print the version.
    opts.on_tail("-v", "--version", "Show version") do
      puts Zeta::VERSION
      exit
    end
  end
  parser.parse!

  commands = ARGV
  if commands.empty? or !(commands-COMMANDS.keys).empty?
    puts parser
    exit(-1)
  end

  options[:verbose] = !options.delete(:silent)
  zeta = Zeta.new(options)

  begin
    if commands.include?('fetch_remote_contracts') or commands.include?('full_check')
      zeta.update_contracts
      puts "\n" if options[:verbose]
    end

    if commands.include?('update_own_contracts')
      puts "Copying #{zeta.config[:service_name].to_s.camelize} contracts..." if options[:verbose]
      zeta.update_own_contracts
      puts "\n" if options[:verbose]
    end

    if commands.include?('validate') or commands.include?('full_check')
      puts "Validating your infrastructure with #{zeta.infrastructure.publishers.length} publishers and #{zeta.infrastructure.consumers.length} consumers..." if options[:verbose]
      zeta.contracts_fulfilled?
      unless zeta.errors.empty?
        exit(-1)
      end
    end
  rescue => e
    if options[:trace]
      raise
    else
      puts "ERROR: ".red + e.message
      puts "(Pssst: try the "+"--trace".yellow+" option?)"
    end
    exit(-1)
  end
end