Class: HastCI::CLI

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

Class Method Summary collapse

Class Method Details

.run(argv:, env:, err:, out:, &runner_block) ⇒ Object



5
6
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
# File 'lib/hastci/cli.rb', line 5

def self.run(argv:, env:, err:, out:, &runner_block)
  config = Config.load_from_env(env)
  configure_logging(config, err)

  runner_exit_code = ExitCodes::SUCCESS
  interrupted = false

  interrupt_cleanup = nil

  begin
    cleanup_result = HastCI::Session.run(config: config) do |session|
      interrupt_cleanup = setup_interrupt_handler(session, err) { interrupted = true }

      session.start!

      runner_exit_code = if session.stopping?
        ExitCodes::SUCCESS
      else
        runner_block.call(session, argv, err, out)
      end
    end
  ensure
    interrupt_cleanup&.call
  end

  print_errors(cleanup_result, err: err)

  final_exit_code(
    runner_exit_code: runner_exit_code,
    cleanup_result: cleanup_result,
    interrupted: interrupted
  )
rescue HastCI::Error => e
  err.puts(e.message)
  exit_code_for_error(e)
rescue Interrupt
  err.puts("\nInterrupted - shutting down...")
  ExitCodes::CANCELLED
end