Class: Querly::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/querly/cli.rb,
lib/querly/cli/find.rb,
lib/querly/cli/test.rb,
lib/querly/cli/rules.rb,
lib/querly/cli/console.rb,
lib/querly/cli/formatter.rb

Defined Under Namespace

Modules: Formatter Classes: Console, Find, Rules, Test

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



115
116
117
# File 'lib/querly/cli.rb', line 115

def self.source_root
  File.join(__dir__, "../..")
end

Instance Method Details

#check(*paths) ⇒ Object



15
16
17
18
19
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/querly/cli.rb', line 15

def check(*paths)
  require 'querly/cli/formatter'

  formatter = case options[:format]
              when "text"
                Formatter::Text.new
              when "json"
                Formatter::JSON.new
              end
  formatter.start

  begin
    unless config_path.file?
      STDERR.puts <<-Message
Configuration file #{config_path} does not look a file.
Specify configuration file by --config option.
      Message
      exit 1
    end

    root_option = options[:root]
    root_path = root_option ? Pathname(root_option).realpath : config_path.parent.realpath

    config = begin
      yaml = YAML.load(config_path.read)
      Config.load(yaml, config_path: config_path, root_dir: root_path, stderr: STDERR)
    rescue => exn
      formatter.config_error config_path, exn
      exit 1
    end

    analyzer = Analyzer.new(config: config, rule: options[:rule])

    ScriptEnumerator.new(paths: paths.empty? ? [Pathname.pwd] : paths.map {|path| Pathname(path) }, config: config).each do |path, script|
      case script
      when Script
        analyzer.scripts << script
        formatter.script_load script
      when StandardError, LoadError
        formatter.script_error path, script
      end
    end

    analyzer.run do |script, rule, pair|
      formatter.issue_found script, rule, pair
    end
  rescue => exn
    formatter.fatal_error exn
    exit 1
  ensure
    formatter.finish
  end
end

#console(*paths) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/querly/cli.rb', line 70

def console(*paths)
  require 'querly/cli/console'
  home_path = if (path = ENV["QUERLY_HOME"])
                   Pathname(path)
                 else
                   Pathname(Dir.home) + ".querly"
                 end
  home_path.mkdir unless home_path.exist?

  Console.new(
    paths: paths.empty? ? [Pathname.pwd] : paths.map {|path| Pathname(path) },
    history_path: home_path + "history",
    history_size: ENV["QUERLY_HISTORY_SIZE"]&.to_i || 1_000_000
  ).start
end

#find(pattern, *paths) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/querly/cli.rb', line 87

def find(pattern, *paths)
  require 'querly/cli/find'

  Find.new(
    pattern: pattern,
    paths: paths.empty? ? [Pathname.pwd] : paths.map {|path| Pathname(path) },
  ).start
end

#initObject



122
123
124
# File 'lib/querly/cli.rb', line 122

def init()
  copy_file("template.yml", "querly.yml")
end

#rules(*ids) ⇒ Object



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

def rules(*ids)
  require "querly/cli/rules"
  Rules.new(config_path: config_path, ids: ids).run
end

#testObject



98
99
100
101
# File 'lib/querly/cli.rb', line 98

def test()
  require "querly/cli/test"
  exit Test.new(config_path: config_path).run
end

#versionObject



111
112
113
# File 'lib/querly/cli.rb', line 111

def version
  puts "Querly #{VERSION}"
end