Class: OldMaid::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/old-maid/runner.rb

Constant Summary collapse

COMMANDS =
%w{full_check fetch_remote_contracts update_own_contracts validate}

Class Method Summary collapse

Class Method Details

.runObject



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
73
74
75
76
77
78
79
80
81
82
# File 'lib/old-maid/runner.rb', line 7

def self.run
  options = {}
  parser = OptionParser.new do |opts|
    opts.banner = "Usage: old-maid [options] #{COMMANDS.join('|')}"

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

    opts.on("-c CONFIG_FILE", "--config=CONFIG_FILE", "Config file (default: config/old-maid.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.separator ""
    opts.separator "Common options:"

    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 OldMaid::VERSION
      exit
    end
  end
  parser.parse!

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

  options[:verbose] = !options.delete(:silent)
  maid = OldMaid.new(options)

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

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

    if commands.include?('validate') or commands.include?('full_check')
      puts "Validating your infrastructure with #{maid.infrastructure.publishers.length} publishers and #{maid.infrastructure.consumers.length} consumers..." if options[:verbose]
      maid.contracts_fulfilled?
      unless maid.errors.empty?
        puts JSON.pretty_generate(maid.errors)
        puts "#{maid.errors.length} invalid contracts".red
        exit(-1)
      end
      puts "All contracts valid 🙌".green if options[:verbose]
    end
  rescue => e
    if options[:verbose]
      raise
    else
      puts "ERROR: ".red + e.message
    end
    exit(-1)
  end
end