Class: Collie::CLI

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

Overview

Command-line interface

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/collie/cli.rb', line 8

def self.exit_on_failure?
  true
end

Instance Method Details

#fmt(*files) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/collie/cli.rb', line 51

def fmt(*files)
  if files.empty?
    say "No files specified", :red
    exit 1
  end

  config = Config.new(options[:config])
  formatter = Formatter::Formatter.new(Formatter::Options.new(config.formatter_options))

  files.each do |file|
    unless File.exist?(file)
      say "File not found: #{file}", :red
      next
    end

    format_file(file, formatter, check: options[:check], diff: options[:diff])
  end
end

#initObject



97
98
99
100
101
102
# File 'lib/collie/cli.rb', line 97

def init
  return if File.exist?(".collie.yml") && !yes?(".collie.yml already exists. Overwrite? (y/n)")

  Config.generate_default
  say "Generated .collie.yml", :green
end

#lint(*files) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/collie/cli.rb', line 20

def lint(*files)
  if files.empty?
    say "No files specified", :red
    exit 1
  end

  config = Config.new(options[:config])
  Linter::Registry.load_rules

  all_offenses = []

  files.each do |file|
    unless File.exist?(file)
      say "File not found: #{file}", :red
      next
    end

    offenses = lint_file(file, config)
    all_offenses.concat(offenses)
  end

  reporter = create_reporter(options[:format])
  puts reporter.report(all_offenses)

  exit 1 if all_offenses.any? { |o| o.severity == :error }
end

#rulesObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/collie/cli.rb', line 72

def rules
  Linter::Registry.load_rules

  if options[:format] == "json"
    output = Linter::Registry.all.map do |rule|
      {
        name: rule.rule_name,
        description: rule.description,
        severity: rule.severity,
        autocorrectable: rule.autocorrectable
      }
    end
    puts JSON.pretty_generate(output)
  else
    say "Available lint rules:", :bold
    Linter::Registry.all.each do |rule|
      severity_color = severity_color(rule.severity)
      autocorrect = rule.autocorrectable ? " [autocorrectable]" : ""
      say "  #{rule.rule_name} (#{set_color(rule.severity, severity_color)})#{autocorrect}"
      say "    #{rule.description}", :dim
    end
  end
end

#versionObject



105
106
107
# File 'lib/collie/cli.rb', line 105

def version
  puts "Collie version #{Collie::VERSION}"
end