class Lintron::CLI

Handles setting up flags for CLI runs based on defaults, linty_rc, and command line arguments

Public Class Methods

new() click to toggle source
# File lib/lintron/cli.rb, line 11
def initialize
  @options = {}
  OptionParser.new do |opts|
    opts.banner = 'Usage: linty [options]'

    opts.on('--watch', 'Watch for changes') do |v|
      @options[:watch] = v
    end
  end.parse!
end

Public Instance Methods

base_branch() click to toggle source
# File lib/lintron/cli.rb, line 54
def base_branch
  config[:base_branch]
end
config() click to toggle source
# File lib/lintron/cli.rb, line 58
def config
  defaults
    .merge(validated_config_from_file)
    .merge(@options)
    .merge(
      {
        base_branch: ARGV[0],
      }.compact,
    )
end
config_from_file() click to toggle source
# File lib/lintron/cli.rb, line 89
def config_from_file
  file_path = File.join(`git rev-parse --show-toplevel`.strip, '.linty_rc')

  raise('.linty_rc is missing.') unless File.exist?(file_path)

  begin
    JSON.parse(File.read(file_path)).symbolize_keys
  rescue JSON::ParserError
    raise('Malformed .linty_rc')
  end
end
defaults() click to toggle source
# File lib/lintron/cli.rb, line 69
def defaults
  {
    base_branch: 'origin/develop',
    watch_exclude: [
      '**/*.log',
      'tmp/**/*',
    ],
  }
end
go() click to toggle source
# File lib/lintron/cli.rb, line 22
def go
  if config[:watch]
    go_watch
  else
    go_once
  end
end
go_once() click to toggle source
# File lib/lintron/cli.rb, line 45
def go_once
  violations = Lintron::API.new(config[:base_url]).violations(pr)
  puts Lintron::TerminalReporter.new.format_violations(violations)
end
go_watch() click to toggle source
# File lib/lintron/cli.rb, line 30
def go_watch
  system('clear')
  go_once

  watcher.watch do |filename|
    system('clear')
    puts "Re-linting because of #{filename}\n\n"
    go_once
  end
end
pr() click to toggle source
# File lib/lintron/cli.rb, line 50
def pr
  LocalPrAlike.from_branch(base_branch)
end
validated_config_from_file() click to toggle source
# File lib/lintron/cli.rb, line 79
def validated_config_from_file
  config = config_from_file

  unless config.key?(:base_url)
    raise('.linty_rc missing required key: base_url')
  end

  config
end
watcher() click to toggle source
# File lib/lintron/cli.rb, line 41
def watcher
  FileWatcher.new(['*', '**/*'], exclude: config[:watch_exclude])
end