lib/chronicle/etl/cli/jobs.rb in chronicle-etl-0.5.1 vs lib/chronicle/etl/cli/jobs.rb in chronicle-etl-0.5.2
- old
+ new
@@ -7,12 +7,10 @@
# CLI commands for working with ETL jobs
class Jobs < SubcommandBase
default_task "start"
namespace :jobs
- class_option :name, aliases: '-j', desc: 'Job configuration name'
-
class_option :extractor, aliases: '-e', desc: "Extractor class. Default: stdin", banner: 'NAME'
class_option :'extractor-opts', desc: 'Extractor options', type: :hash, default: {}
class_option :transformer, aliases: '-t', desc: 'Transformer class. Default: null', banner: 'NAME'
class_option :'transformer-opts', desc: 'Transformer options', type: :hash, default: {}
class_option :loader, aliases: '-l', desc: 'Loader class. Default: table', banner: 'NAME'
@@ -42,12 +40,12 @@
3. #{'Loader'.underline}: takes that transformed data and loads it externally. This can be an API, flat files, (or by default), stdout. With the --dry-run option, this step won't be run.
If you do not want to use the command line flags, you can also configure a job with a .yml config file. You can either specify the path to this file or use the filename and place the file in ~/.config/chronicle/etl/jobs/NAME.yml and call it with `--job NAME`
LONG_DESC
# Run an ETL job
- def start
- job_definition = build_job_definition(options)
+ def start(name = nil)
+ job_definition = build_job_definition(name, options)
if job_definition.plugins_missing?
missing_plugins = job_definition.errors[:plugins]
.select { |error| error.is_a?(Chronicle::ETL::PluginNotInstalledError) }
.map(&:name)
@@ -62,25 +60,43 @@
message << "Problem with #{category}:\n - #{errors.map(&:to_s).join("\n - ")}"
end
cli_fail(message: "Error running job.\n#{message}", exception: e)
end
- desc "create", "Create a job"
+ option :'skip-confirmation', aliases: '-y', type: :boolean
+ desc "save", "Save a job"
# Create an ETL job
- def create
- job_definition = build_job_definition(options)
+ def save(name)
+ write_config = true
+ job_definition = build_job_definition(name, options)
job_definition.validate!
- Chronicle::ETL::Config.write("jobs", options[:name], job_definition.definition)
+ if Chronicle::ETL::Config.exists?("jobs", name) && !options[:'skip-confirmation']
+ prompt = TTY::Prompt.new
+ write_config = false
+ message = "Job '#{name}' exists already. Ovewrite it?"
+ begin
+ write_config = prompt.yes?(message)
+ rescue TTY::Reader::InputInterrupt
+ end
+ end
+
+ if write_config
+ Chronicle::ETL::Config.write("jobs", name, job_definition.definition)
+ cli_exit(message: "Job saved. Run it with `$chronicle-etl jobs:run #{name}`")
+ else
+ cli_fail(message: "\nJob not saved")
+ end
+
rescue Chronicle::ETL::JobDefinitionError => e
cli_fail(message: "Job definition error", exception: e)
end
desc "show", "Show details about a job"
# Show an ETL job
- def show
- job_definition = build_job_definition(options)
+ def show(name = nil)
+ job_definition = build_job_definition(name, options)
job_definition.validate!
puts Chronicle::ETL::Job.new(job_definition)
rescue Chronicle::ETL::JobDefinitionError => e
cli_fail(message: "Job definition error", exception: e)
end
@@ -134,12 +150,12 @@
Chronicle::ETL::CLI::Plugins.new.install(*missing_plugins)
end
# Create job definition by reading config file and then overwriting with flag options
- def build_job_definition(options)
+ def build_job_definition(name, options)
definition = Chronicle::ETL::JobDefinition.new
- definition.add_config(load_job_config(options[:name]))
+ definition.add_config(load_job_config(name))
definition.add_config(process_flag_options(options).transform_keys(&:to_sym))
definition
end
def load_job_config name