lib/sorbet_progress/cli.rb in sorbet-progress-0.2.5 vs lib/sorbet_progress/cli.rb in sorbet-progress-0.3.0

- old
+ new

@@ -4,80 +4,64 @@ require "sorbet_progress/calculator" require "sorbet_progress/error" require "sorbet_progress/metrics" require "sorbet_progress/parser" +require "sorbet_progress/reporters/bar_chart" +require "sorbet_progress/reporters/base" +require "sorbet_progress/reporters/verbose" module SorbetProgress # Parses the provided metrics file and prints a report. class CLI extend T::Sig USAGE = <<~EOS - Usage: sorbet_progress /path/to/sorbet_metrics.json + Usage: sorbet_progress [--reporter name] /path/to/sorbet_metrics.json + Reporters: bar_chart, verbose EOS sig { params(argv: T::Array[String]).void } def initialize(argv) - unless argv.length == 1 + # TODO: use an actual CLI args parser, like optparse or trollop + case argv.length + when 1 + @path = argv.first + @reporter_name = "verbose" + when 3 + @path = argv.last + @reporter_name = argv[1] + else raise Error.new(1, USAGE) end - @path = argv.first end sig { void } - # rubocop:disable Metrics/AbcSize def run metrics = parse(@path) - puts "Sorbet Progress\n\n" - - stats_calculator = Calculator.new(metrics) - - puts "Progress for sig coverage" - stats_calculator.coverage_metrics.each do |label, value| - puts format_metric(label, value) - end - - puts "\nProgress for file coverage" - - stats_calculator.sigil_percentages.each do |elem| - percentage = - if elem[:percentage] - elem[:percentage] * 100 - else - 0 - end - puts format( - "%-17s\t%d\t%.2f %%", - elem[:label], - elem[:value] || 0, - percentage - ) - end - - puts "---------------------------------------" - puts "Total: \t\t\t#{stats_calculator.total}\t100%" - - puts "Keep up the good work 👍" + calculator = Calculator.new(metrics) + reporter = reporter_class(@reporter_name).new(calculator) + puts reporter.report end - # rubocop:enable Metrics/AbcSize private sig { params(path: String).returns(Metrics) } def parse(path) Parser.new.parse(File.read(path)) rescue Errno::ENOENT => e raise Error.new(2, "Metrics file not found: " + e.message) end - # Format a label and metric value into a presentable String. - sig { params(label: Symbol, value: Integer).returns(String) } - def format_metric(label, value) - if value.nil? - format("%-17s\tunknown", label) + sig { params(name: String).returns(T.class_of(Reporters::Base)) } + def reporter_class(name) + case name + when "verbose" + Reporters::Verbose + when "bar_chart" + Reporters::BarChart else - format("%-17s\t%d", label, value) + raise format("Invalid reporter name: %s", @reporter_name) end end end end