From c7d5c2798f47ed0a06f0ef57908c556580f72f9c Mon Sep 17 00:00:00 2001
From: elijah <elijah@riseup.net>
Date: Mon, 2 Feb 2015 16:41:12 -0800
Subject: more verbose --version (closes #4428)

---
 bin/leap                     | 18 +++++++++---
 lib/leap_cli/commands/pre.rb | 69 +++++++++++++++++++++++++-------------------
 lib/leap_cli/leapfile.rb     |  7 ++++-
 3 files changed, 59 insertions(+), 35 deletions(-)

diff --git a/bin/leap b/bin/leap
index 47dfdf9..59e4ee8 100755
--- a/bin/leap
+++ b/bin/leap
@@ -10,6 +10,8 @@ else
   DEBUG=false
 end
 
+LEAP_CLI_BASE_DIR = File.expand_path('..', File.dirname(File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__))
+
 begin
   require 'leap_cli'
 rescue LoadError
@@ -24,8 +26,7 @@ rescue LoadError
   # This allows you to run the command directly while developing the gem, and also lets you
   # run from anywhere (I like to link 'bin/leap' to /usr/local/bin/leap).
   #
-  base_dir = File.expand_path('..', File.dirname(File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__))
-  require File.join(base_dir, 'lib','leap_cli','load_paths')
+  require File.join(LEAP_CLI_BASE_DIR, 'lib','leap_cli','load_paths')
   require 'leap_cli'
 end
 
@@ -77,9 +78,16 @@ module LeapCli::Commands
   program_desc       LeapCli::SUMMARY
   program_long_desc  LeapCli::DESCRIPTION
 
-  # handle --version ourselves
+  # handle --version ourselves (and not GLI)
   if ARGV.grep(/--version/).any?
     puts "leap #{LeapCli::VERSION}, ruby #{RUBY_VERSION}"
+    begin
+      commands_from('leap_cli/commands')
+      initialize_leap_cli(false, {:verbose => 2})
+    rescue StandardError => exc
+      puts exc.to_s
+      raise exc if DEBUG
+    end
     exit(0)
   end
 
@@ -88,8 +96,10 @@ module LeapCli::Commands
   def error_message(msg)
   end
 
-  # load commands and run
+  # load commands
   commands_from('leap_cli/commands')
+
+  # run command
   ORIGINAL_ARGV = ARGV.dup
   begin
     exit_status = run(ARGV)
diff --git a/lib/leap_cli/commands/pre.rb b/lib/leap_cli/commands/pre.rb
index 055f3a1..74d7e62 100644
--- a/lib/leap_cli/commands/pre.rb
+++ b/lib/leap_cli/commands/pre.rb
@@ -28,57 +28,66 @@ module LeapCli; module Commands
   switch 'color', :negatable => true
 
   pre do |global,command,options,args|
-    #
+    initialize_leap_cli(true, global)
+    true
+  end
+
+  protected
+
+  #
+  # available options:
+  #  :verbose -- integer log verbosity level
+  #  :log     -- log file path
+  #  :color   -- true or false, to log in color or not.
+  #
+  def initialize_leap_cli(require_provider, options={})
     # set verbosity
-    #
-    LeapCli.set_log_level(global[:verbose].to_i)
+    options[:verbose] ||= 1
+    LeapCli.set_log_level(options[:verbose].to_i)
 
-    #
     # load Leapfile
-    #
-    unless LeapCli.leapfile.load
+    LeapCli.leapfile.load
+    if LeapCli.leapfile.valid?
+      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
+    elsif require_provider
       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.log_file = options[:log] || LeapCli.leapfile.log
     LeapCli::Util.log_raw(:log) { $0 + ' ' + ORIGINAL_ARGV.join(' ')}
     log_version
-    LeapCli.log_in_color = global[:color]
-
-    true
+    LeapCli.log_in_color = options[:color]
   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)]
+      if Util.is_git_directory?(LEAP_CLI_BASE_DIR)
+        str << " (%s %s)" % [Util.current_git_branch(LEAP_CLI_BASE_DIR),
+          Util.current_git_commit(LEAP_CLI_BASE_DIR)]
+      else
+        str << " (%s)" % LEAP_CLI_BASE_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)]
+      if LeapCli.leapfile.valid?
+        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
-      log 2, str
     end
   end
 
-
 end; end
diff --git a/lib/leap_cli/leapfile.rb b/lib/leap_cli/leapfile.rb
index 8895f4d..7aaf10f 100644
--- a/lib/leap_cli/leapfile.rb
+++ b/lib/leap_cli/leapfile.rb
@@ -72,7 +72,8 @@ module LeapCli
         unless @platform_branch.nil?
           Util::log 0, :warning, "in Leapfile: @platform_branch is no longer supported."
         end
-        return true
+        @valid = true
+        return @valid
       end
     end
 
@@ -84,6 +85,10 @@ module LeapCli
       edit_leaprc(property)
     end
 
+    def valid?
+      !!@valid
+    end
+
     private
 
     #
-- 
cgit v1.2.3