Class: Lintron::CLI
- Inherits:
-
Object
- Object
- Lintron::CLI
- 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
- #base_branch ⇒ Object
- #config ⇒ Object
- #config_from_file ⇒ Object
- #defaults ⇒ Object
- #go ⇒ Object
- #go_once ⇒ Object
- #go_watch ⇒ Object
-
#initialize ⇒ CLI
constructor
A new instance of CLI.
- #pr ⇒ Object
- #validated_config_from_file ⇒ Object
- #watcher ⇒ Object
Constructor Details
#initialize ⇒ CLI
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. = 'Usage: linty [options]' opts.on('--watch', 'Watch for changes') do |v| @options[:watch] = v end end.parse! end |
Instance Method Details
#base_branch ⇒ Object
54 55 56 |
# File 'lib/lintron/cli.rb', line 54 def base_branch config[:base_branch] end |
#config ⇒ Object
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_file ⇒ Object
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 |
#defaults ⇒ Object
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 |
#go ⇒ Object
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_once ⇒ Object
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_watch ⇒ Object
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 |
#pr ⇒ Object
50 51 52 |
# File 'lib/lintron/cli.rb', line 50 def pr LocalPrAlike.from_branch(base_branch) end |
#validated_config_from_file ⇒ Object
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 |
#watcher ⇒ Object
41 42 43 |
# File 'lib/lintron/cli.rb', line 41 def watcher FileWatcher.new(['*', '**/*'], exclude: config[:watch_exclude]) end |