summaryrefslogtreecommitdiff
path: root/lib/leap_cli/commands/pre.rb
blob: 055f3a1682a674d6ee338539c70df4c1cd75405f (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

#
# check to make sure we can find the root directory of the platform
#
module LeapCli; module Commands

  desc 'Verbosity level 0..5'
  arg_name 'LEVEL'
  default_value '1'
  flag [:v, :verbose]

  desc 'Override default log file'
  arg_name 'FILE'
  default_value nil
  flag :log

  desc 'Display version number and exit'
  switch :version, :negatable => false

  desc 'Skip prompts and assume "yes"'
  switch :yes, :negatable => false

  desc 'Print full stack trace for exceptions and load `debugger` gem if installed.'
  switch [:d, :debug], :negatable => false

  desc 'Disable colors in output'
  default_value true
  switch 'color', :negatable => true

  pre do |global,command,options,args|
    #
    # set verbosity
    #
    LeapCli.set_log_level(global[:verbose].to_i)

    #
    # load Leapfile
    #
    unless LeapCli.leapfile.load
      bail! { log :missing, 'Leapfile in directory tree' }
    end
    Path.set_platform_path(LeapCli.leapfile.platform_directory_path)
    Path.set_provider_path(LeapCli.leapfile.provider_directory_path)
    if !Path.provider || !File.directory?(Path.provider)
      bail! { log :missing, "provider directory '#{Path.provider}'" }
    end
    if !Path.platform || !File.directory?(Path.platform)
      bail! { log :missing, "platform directory '#{Path.platform}'" }
    end

    #
    # set log file
    #
    LeapCli.log_file = global[:log] || LeapCli.leapfile.log
    LeapCli::Util.log_raw(:log) { $0 + ' ' + ORIGINAL_ARGV.join(' ')}
    log_version
    LeapCli.log_in_color = global[:color]

    true
  end

  private

  #
  # add a log entry for the leap command and leap platform versions
  #
  def log_version
    if LeapCli.log_level >= 2
      str = "leap command v#{LeapCli::VERSION}"
      cli_dir = File.dirname(__FILE__)
      if Util.is_git_directory?(cli_dir)
        str << " (%s %s)" % [Util.current_git_branch(cli_dir), Util.current_git_commit(cli_dir)]
      end
      log 2, str
      str = "leap platform v#{Leap::Platform.version}"
      if Util.is_git_directory?(Path.platform)
        str << " (%s %s)" % [Util.current_git_branch(Path.platform), Util.current_git_commit(Path.platform)]
      end
      log 2, str
    end
  end


end; end