lib/saddler/cli.rb in saddler-0.1.0 vs lib/saddler/cli.rb in saddler-0.1.1
- old
+ new
@@ -17,28 +17,64 @@
option :data
option :file
option :options, type: :hash, default: {}
option :require
option :reporter
+ option :debug, type: :boolean, default: false
+ option :verbose, type: :boolean, default: false
def report
- data = \
+ if options[:debug]
+ logger.level = Logger::DEBUG
+ elsif options[:verbose]
+ logger.level = Logger::INFO
+ end
+ logger.debug(options)
+
+ data = fetch_data(options)
+ Validator.valid?(data)
+ require options[:require] if options[:require]
+ reporter = add_reporter(options)
+
+ reporter.report(data, options[:options])
+ rescue StandardError => e
+ logger.error('options')
+ logger.error(options)
+ logger.error('input data')
+ logger.error(data)
+ logger.error('reporter')
+ logger.error(reporter)
+ raise e
+ end
+
+ no_commands do
+ def logger
+ ::Saddler.logger
+ end
+
+ def fetch_data(options)
+ data = \
if options[:data]
options[:data]
elsif options[:file]
File.read(options[:file])
elsif !$stdin.tty?
ARGV.clear
ARGF.read
end
- abort('no input') if !data || data.empty?
+ logger.info('input data')
+ logger.info(data)
+ fail NoInputError if !data || data.empty?
- require options[:require] if options[:require]
- if options[:reporter]
- reporter = ::Saddler::Reporter.add_reporter(options[:reporter], $stdout)
+ data
end
- abort('no reporter') unless reporter
- reporter.report(data, options[:options])
+ def add_reporter(options)
+ reporter = Reporter.add_reporter(options[:reporter], $stdout) if options[:reporter]
+ fail NoReporterError unless reporter
+ logger.info('use reporter')
+ logger.info(reporter)
+ reporter
+ end
end
end
end