class Matt::Command
Attributes
exitcode[RW]
output_format[RW]
to[RW]
Public Class Methods
new() { |self| ... }
click to toggle source
# File lib/matt/command.rb, line 40 def initialize @output_format = :csv @to = nil yield(self) if block_given? end
Public Instance Methods
call(argv)
click to toggle source
# File lib/matt/command.rb, line 69 def call(argv) ok = false catch :abort do parse_argv(argv) _call(argv) ok = true end self.exitcode = ok ? 0 : 1 end
configuration()
click to toggle source
# File lib/matt/command.rb, line 51 def configuration return @configuration if @configuration self.configuration = Configuration.new(Path.pwd) end
configuration=(c)
click to toggle source
# File lib/matt/command.rb, line 56 def configuration=(c) @on_configuration.call(c) if @on_configuration @configuration = c end
has_configuration?()
click to toggle source
# File lib/matt/command.rb, line 61 def has_configuration? !!@configuration end
on_configuration(&bl)
click to toggle source
# File lib/matt/command.rb, line 46 def on_configuration(&bl) @on_configuration = bl self end
parse_argv(argv)
click to toggle source
# File lib/matt/command.rb, line 65 def parse_argv(argv) opt_parser.parse!(argv) end
Protected Instance Methods
_call(argv)
click to toggle source
# File lib/matt/command.rb, line 81 def _call(argv) if argv.empty? puts_out opt_parser abort end meth = :"do_#{argv.first}" if self.respond_to?(meth, true) send(meth, argv[1..-1]) else puts_err "No such command #{argv.first}" abort end end
abort()
click to toggle source
# File lib/matt/command.rb, line 231 def abort throw :abort end
argv_count!(argv, n)
click to toggle source
# File lib/matt/command.rb, line 198 def argv_count!(argv, n) return if argv.size == n puts_err "#{n} arguments expected, got #{argv.size}" abort end
argv_to_xs(argv, xs)
click to toggle source
# File lib/matt/command.rb, line 223 def argv_to_xs(argv, xs) if argv.empty? configuration.send(xs).to_h.values else argv.map{|arg| x_exists!(arg, xs) } end end
datasource_exists!(name)
click to toggle source
# File lib/matt/command.rb, line 219 def datasource_exists!(name) x_exists!(name, :datasources) end
do_export(argv)
click to toggle source
# File lib/matt/command.rb, line 118 def do_export(argv) which_ones = argv_to_xs(argv, :measures) which_ones.each do |m| data = m.data_at(configuration.at_predicate) debug("export -- #{data.inspect}") (@to || m.exporters).each do |e| exporter = exporter_exists!(e) exporter.export(m, data) end end end
do_help(*args)
click to toggle source
# File lib/matt/command.rb, line 130 def do_help(*args) file = __FILE__ exec "grep ^#/<'#{file}'|cut -c4-|sed s/VERSION/#{Matt::VERSION}/g" end
do_ping(argv)
click to toggle source
# File lib/matt/command.rb, line 111 def do_ping(argv) which_ones = argv_to_xs(argv, :datasources) which_ones.each do |d| d.ping end end
do_show(argv)
click to toggle source
# File lib/matt/command.rb, line 96 def do_show(argv) argv_count!(argv, 1) m = measure_exists!(argv.first) data = m.data_at(configuration.at_predicate) debug("show -- #{data.inspect}") case of = output_format when :json puts_out JSON.pretty_generate(data) when :csv puts_out data.to_csv(configuration.csv_options) else puts_err "Unknown format #{of}" end end
exporter_exists!(name)
click to toggle source
# File lib/matt/command.rb, line 215 def exporter_exists!(name) x_exists!(name, :exporters) end
measure_exists!(name)
click to toggle source
# File lib/matt/command.rb, line 211 def measure_exists!(name) x_exists!(name, :measures) end
opt_parser()
click to toggle source
# File lib/matt/command.rb, line 137 def opt_parser OptionParser.new do |opts| opts.banner = "Usage: matt [options] COMMAND [args]" opts.on("-f FOLDER") do |folder| p = Path(folder) if has_configuration? puts_err "-f must be used before other configuration options" abort elsif p.exists? && p.directory? self.configuration = Configuration.new(p) else puts_err "No such folder: #{folder}" abort end end opts.on("--all-time") do self.configuration.at_predicate = Matt.alltime_predicate end opts.on("--yesterday") do self.configuration.at_predicate = Matt.yesterday_predicate end opts.on("--today") do self.configuration.at_predicate = Matt.today_predicate end opts.on("--last=X") do |x| self.configuration.at_predicate = Matt.last_predicate(x) end opts.on("--since=X") do |x| self.configuration.at_predicate = Matt.since_predicate(x) end opts.on("--between=X,Y") do |arg| self.configuration.at_predicate = Matt.between_predicate(*arg.split(',')) end opts.on("--to=EXPORTERS") do |exporters| @to = (@to || []) + exporters.split(/\s*,\s*/).map{|e| exporter_exists!(e.to_sym).name } end opts.on("--json") do self.output_format = :json end opts.on("--csv") do self.output_format = :csv end opts.on('--version', "Show version number") do puts_out "Matt v#{VERSION} - (c) Enspirit SRL" abort end opts.on("--silent") do self.configuration.debug_level = Configuration::DEBUG_SILENT end opts.on("--verbose") do self.configuration.debug_level = Configuration::DEBUG_VERBOSE end opts.on("-h", "--help", "Prints this help") do do_help abort end end end
x_exists!(name, xs)
click to toggle source
# File lib/matt/command.rb, line 204 def x_exists!(name, xs) x = configuration.send(xs).send(name.to_sym) return x if x puts_err "No such #{xs[0...-1]} #{name}" abort end