summaryrefslogtreecommitdiff
path: root/bin/leap
blob: 55ffb412f80bf24177030b68c84c14e8b4ef5212 (plain)
1
2
3
4
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
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
101
#!/usr/bin/env ruby

if ARGV.include?('--debug') || ARGV.include?('-d')
  DEBUG=true
  begin
    if RUBY_VERSION =~ /^2/
      require 'byebug'
    else
      require 'debugger'
    end
  rescue LoadError
  end
else
  $VERBOSE=nil
  DEBUG=false
end
TEST = false

LEAP_CLI_BASE_DIR = File.expand_path('..', File.dirname(File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__))

begin
  # First, try to load the leap_cli code that is local to this `leap` command.
  # If that fails, then we try to load leap_cli as a gem.
  require File.join(LEAP_CLI_BASE_DIR, 'lib','leap_cli','load_paths')
  require 'leap_cli'
rescue LoadError
  require 'leap_cli'
end

require 'gli'
require 'leap_cli/lib_ext/gli' # our custom extensions to gli

#
# Typically, GLI and Highline methods are loaded into the global namespace.
# Instead, here we load these into the module LeapCli::Commands in order to
# ensure that the cli logic and code is kept isolated to leap_cli/commands/*.rb
#
# no cheating!
#
module LeapCli::Commands
  extend GLI::App

  # make config manager available as 'manager'
  def self.manager
    @manager ||= begin
      manager = LeapCli::Config::Manager.new
      manager.load
      manager
    end
  end

  # make provider config available as 'provider'
  def self.provider
    manager.provider
  end

  # make leapfile available as 'leapfile'
  def self.leapfile
    LeapCli::leapfile
  end

  # info about leap command line suite
  program_desc       LeapCli::SUMMARY
  program_long_desc  LeapCli::DESCRIPTION

  LeapCli::Bootstrap::setup(ARGV)

  # handle --version ourselves (and not GLI)
  if ARGV.grep(/--version/).any?
    LeapCli::Bootstrap::handle_version(self)
  else
    LeapCli::Bootstrap::load_libraries(self)
  end

  # disable GLI error catching
  ENV['GLI_DEBUG'] = "true"
  def error_message(msg)
  end

  # run command
  begin
    if ARGV.any?
      LeapCli.log_raw(:log, nil, "COMMAND") { 'leap ' + ARGV.join(' ') }
    end
    exit_status = run(ARGV)
    exit(LeapCli::Util.exit_status || exit_status)
  rescue StandardError => exc
    if exc.respond_to? :log
      exc.log
    else
      puts
      LeapCli.log :error, "%s: %s" % [exc.class, exc.message]
      puts
    end
    if DEBUG
      raise exc
    else
      exit(1)
    end
  end
end