lib/metacrunch/cli.rb in metacrunch-2.2.3 vs lib/metacrunch/cli.rb in metacrunch-3.0.0

- old
+ new

@@ -1,26 +1,74 @@ module Metacrunch class Cli - require_relative "./cli/main" - require_relative "./cli/base" - require_relative "./cli/command_registry" - require_relative "./cli/command_definition" + ARGS_SEPERATOR = "@@" - def self.start(argv) - Main.start(argv) + def run + init_commander! + init_run_command! + run_commander! end - def self.setup(namespace, description, &block) - klass = Class.new(Base) - klass.namespace(namespace) + private + def commander + @commander ||= Commander::Runner.new(metacrunch_args) + end - registry = CommandRegistry.new - yield(registry) + def init_commander! + commander.program :name, "metacrunch" + commander.program :version, Metacrunch::VERSION + commander.program :description, "Data processing and ETL toolkit for Ruby." + commander.default_command :help + end - registry.commands.each do |c| - klass.register_thor_command(c) + def run_commander! + commander.run! + end + + def init_run_command! + commander.command :run do |c| + c.syntax = "metacrunch run [options] FILE [@@ job_options]" + c.description = "Runs a metacrunch job description." + + c.action do |filenames, program_options| + if filenames.empty? + say "You need to provide a job description file." + exit(1) + elsif filenames.count > 1 + say "You must provide exactly one job description file." + else + filename = File.expand_path(filenames.first) + dir = File.dirname(filename) + + setup_bundler(dir) + + Dir.chdir(dir) do + contents = File.read(filename) + context = Metacrunch::Job.define(contents, filename: filename, args: job_args) + context.run + end + end + end end + end - Main.register(klass, namespace, "#{namespace} [COMMAND]", description) + def metacrunch_args + index = ARGV.index(ARGS_SEPERATOR) + @metacrunch_args ||= index ? ARGV[0..index-1] : ARGV end + + def job_args + index = ARGV.index(ARGS_SEPERATOR) + @job_args ||= index ? ARGV[index+1..-1] : nil + end + + def setup_bundler(dir) + ENV['BUNDLE_GEMFILE'] ||= File.join(dir, "Gemfile") + if File.exists?(ENV['BUNDLE_GEMFILE']) + puts "Using Gemfile `#{ENV['BUNDLE_GEMFILE']}`." + Bundler.setup + Bundler.require + end + end + end end