-
Notifications
You must be signed in to change notification settings - Fork 20
/
cli.rb
151 lines (127 loc) · 4.28 KB
/
cli.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require "thor"
require "json"
if ENV["NO_COLOR"]
Rainbow.enabled = false
end
module Querly
class CLI < Thor
desc "check [paths]", "Check paths based on configuration"
option :config, default: "querly.yml"
option :root
option :format, default: "text", type: :string, enum: %w(text json)
option :rule, type: :string
option :threads, default: Parallel.processor_count, type: :numeric
def check(*paths)
require 'querly/cli/formatter'
formatter = case options[:format]
when "text"
Formatter::Text.new
when "json"
Formatter::JSON.new
end
formatter.start
threads = Integer(options[:threads])
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
begin
config = config(root_option: options[:root])
rescue => exn
formatter.config_error config_path, exn
end
analyzer = Analyzer.new(config: config, rule: options[:rule])
ScriptEnumerator.new(paths: paths.empty? ? [Pathname.pwd] : paths.map {|path| Pathname(path) }, config: config, threads: threads).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
desc "console [paths]", "Start console for given paths"
option :config, default: "querly.yml"
option :threads, default: Parallel.processor_count, type: :numeric
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?
config = config_path.file? ? config(root_option: nil) : nil
threads = Integer(options[:threads])
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,
config: config,
threads: threads
).start
end
desc "find pattern [paths]", "Find for the pattern in given paths"
option :config, default: "querly.yml"
option :threads, default: Parallel.processor_count, type: :numeric
def find(pattern, *paths)
require 'querly/cli/find'
config = config_path.file? ? config(root_option: nil) : nil
threads = Integer(options[:threads])
Find.new(
pattern: pattern,
paths: paths.empty? ? [Pathname.pwd] : paths.map {|path| Pathname(path) },
config: config,
threads: threads
).start
end
desc "test", "Check configuration"
option :config, default: "querly.yml"
def test()
require "querly/cli/test"
exit Test.new(config_path: config_path).run
end
desc "rules", "Print loaded rules"
option :config, default: "querly.yml"
def rules(*ids)
require "querly/cli/rules"
Rules.new(config_path: config_path, ids: ids).run
end
desc "version", "Print version"
def version
puts "Querly #{VERSION}"
end
def self.source_root
File.join(__dir__, "../..")
end
include Thor::Actions
desc "init", "Generate Querly config file (querly.yml)"
def init()
copy_file("template.yml", "querly.yml")
end
private
def config(root_option:)
root_path = root_option ? Pathname(root_option).realpath : config_path.parent.realpath
yaml = YAML.load(config_path.read)
Config.load(yaml, config_path: config_path, root_dir: root_path, stderr: STDERR)
end
def config_path
[Pathname(options[:config]),
Pathname("querly.yaml")].compact.find(&:file?) || Pathname(options[:config])
end
end
end