lib/oct/app.rb in oct-0.2.0 vs lib/oct/app.rb in oct-0.3.1
- old
+ new
@@ -1,6 +1,5 @@
-require 'configatron'
require 'term/ansicolor'
class String
include Term::ANSIColor
end
@@ -9,94 +8,84 @@
AVAILABLE_ACTIONS = %w[]
class App
- def initialize(base_dir, options={})
- @base_dir = base_dir
+ def initialize(working_dir, argv=[], options={})
+ @working_dir = working_dir
@options = options
+ @argv = argv
if @options[:verbose]
- puts "base_dir: #{@base_dir}".cyan
+ puts "working_dir: #{@working_dir}".cyan
puts "options: #{@options.inspect}".cyan
+ puts "base_dir: #{@options[:base_dir]}".cyan if @options[:base_dir]
+ puts "config file: #{@options[:config]}".cyan if @options[:config]
end
- configure(options)
+ $stdout.sync = true
end
- def run
+ def execute
begin
if action_argument_required?
- action = ARGV.shift
+ action = @argv.shift
+ args = @argv
+
unless AVAILABLE_ACTIONS.include?(action)
if action.nil?
puts "oct action required"
else
puts "oct invalid action: #{action}"
end
puts "oct --help for more information"
exit 1
end
- puts "oct run action: #{action}".cyan if @options[:verbose]
+ puts "oct run action: #{action} #{args.join(' ')}".cyan if @options[:verbose]
raise "action #{action} not implemented" unless respond_to?(action)
- result = send(action)
+ result = send(action, args)
else
#
# default action if action_argument_required? is false
#
files = ARGV.empty? ? Dir.glob('*') : ARGV
result = Oct::FileStat.new.mode(files, @options)
end
- exit(result ? 0 : 1)
+ if result.is_a?(Numeric)
+ exit(result)
+ else
+ # handle all other return types
+ exit(result ? 0 : 1)
+ end
rescue SystemExit => e
# This is the normal exit point, exit code from the send result
# or exit from another point in the system
puts "oct run system exit: #{e}, status code: #{e.status}".green if @options[:verbose]
exit(e.status)
rescue Exception => e
STDERR.puts("oct command failed, error(s) follow:")
STDERR.puts("#{e.message}".red)
+ STDERR.puts("Use '--verbose' for backtrace.") unless @options[:verbose]
STDERR.puts(e.backtrace.join("\n")) if @options[:verbose]
exit(1)
end
end
#
# app commands start
#
-
+
#
# app commands end
#
private
# true if application requires an action to be specified on the command line
def action_argument_required?
!AVAILABLE_ACTIONS.empty?
- end
-
- # read options for YAML config with ERB processing and initialize configatron
- def configure(options)
- config = @options[:config]
- config = File.join(@base_dir, 'oct.conf') unless config
- if File.exists?(config)
- # load configatron options from the config file
- puts "loading config file: #{config}".cyan if @options[:verbose]
- configatron.configure_from_yaml(config)
- else
- # user specified a config file?
- raise "config file not found" if @options[:config]
- # no error if user did not specify config file
- puts "#{config} not found".yellow if @options[:verbose]
- end
-
- #
- # set defaults, these will NOT override setting read from YAML
- #
-
end
end
end