Class: Lintron::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/lintron/cli.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



11
12
13
14
15
16
17
18
19
20
# 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

Instance Method Details

#base_branchObject



54
55
56
# File 'lib/lintron/cli.rb', line 54

def base_branch
  config[:base_branch]
end

#configObject



58
59
60
61
62
63
64
65
66
67
# 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_fileObject



89
90
91
92
93
94
95
96
97
98
99
# 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

#defaultsObject



69
70
71
72
73
74
75
76
77
# File 'lib/lintron/cli.rb', line 69

def defaults
  {
    base_branch: 'origin/develop',
    watch_exclude: [
      '**/*.log',
      'tmp/**/*',
    ],
  }
end

#goObject



22
23
24
25
26
27
28
# File 'lib/lintron/cli.rb', line 22

def go
  if config[:watch]
    go_watch
  else
    go_once
  end
end

#go_onceObject



45
46
47
48
# 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_watchObject



30
31
32
33
34
35
36
37
38
39
# 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

#prObject



50
51
52
# File 'lib/lintron/cli.rb', line 50

def pr
  LocalPrAlike.from_branch(base_branch)
end

#validated_config_from_fileObject



79
80
81
82
83
84
85
86
87
# 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

#watcherObject



41
42
43
# File 'lib/lintron/cli.rb', line 41

def watcher
  FileWatcher.new(['*', '**/*'], exclude: config[:watch_exclude])
end