Skip to content

Commit

Permalink
Merge pull request #67 from pocke/console-with-config
Browse files Browse the repository at this point in the history
`console` and `find` with configuration file
  • Loading branch information
soutaro authored Jul 19, 2019
2 parents 55b2a6d + 14f2b7f commit bf024ac
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
24 changes: 16 additions & 8 deletions lib/querly/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,10 @@ def check(*paths)
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)
begin
config = config(root_option: options[:root])
rescue => exn
formatter.config_error config_path, exn
exit 1
end

analyzer = Analyzer.new(config: config, rule: options[:rule])
Expand All @@ -67,6 +62,7 @@ def check(*paths)
end

desc "console [paths]", "Start console for given paths"
option :config, default: "querly.yml"
def console(*paths)
require 'querly/cli/console'
home_path = if (path = ENV["QUERLY_HOME"])
Expand All @@ -75,21 +71,26 @@ def console(*paths)
Pathname(Dir.home) + ".querly"
end
home_path.mkdir unless home_path.exist?
config = config_path.file? ? config(root_option: nil) : nil

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
history_size: ENV["QUERLY_HISTORY_SIZE"]&.to_i || 1_000_000,
config: config,
).start
end

desc "find pattern [paths]", "Find for the pattern in given paths"
option :config, default: "querly.yml"
def find(pattern, *paths)
require 'querly/cli/find'

config = config_path.file? ? config(root_option: nil) : nil
Find.new(
pattern: pattern,
paths: paths.empty? ? [Pathname.pwd] : paths.map {|path| Pathname(path) },
config: config,
).start
end

Expand Down Expand Up @@ -125,6 +126,13 @@ def init()

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])
Expand Down
8 changes: 5 additions & 3 deletions lib/querly/cli/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ class Console
attr_reader :paths
attr_reader :history_path
attr_reader :history_size
attr_reader :config
attr_reader :history

def initialize(paths:, history_path:, history_size:)
def initialize(paths:, history_path:, history_size:, config: nil)
@paths = paths
@history_path = history_path
@history_size = history_size
@config = config
@history = []
end

Expand Down Expand Up @@ -42,9 +44,9 @@ def reload!
def analyzer
return @analyzer if @analyzer

@analyzer = Analyzer.new(config: nil, rule: nil)
@analyzer = Analyzer.new(config: config, rule: nil)

ScriptEnumerator.new(paths: paths, config: nil).each do |path, script|
ScriptEnumerator.new(paths: paths, config: config).each do |path, script|
case script
when Script
@analyzer.scripts << script
Expand Down
8 changes: 5 additions & 3 deletions lib/querly/cli/find.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ class Find

attr_reader :pattern_str
attr_reader :paths
attr_reader :config

def initialize(pattern:, paths:)
def initialize(pattern:, paths:, config: nil)
@pattern_str = pattern
@paths = paths
@config = config
end

def start
Expand Down Expand Up @@ -48,9 +50,9 @@ def pattern
def analyzer
return @analyzer if @analyzer

@analyzer = Analyzer.new(config: nil, rule: nil)
@analyzer = Analyzer.new(config: config, rule: nil)

ScriptEnumerator.new(paths: paths, config: nil).each do |path, script|
ScriptEnumerator.new(paths: paths, config: config).each do |path, script|
case script
when Script
@analyzer.scripts << script
Expand Down

0 comments on commit bf024ac

Please sign in to comment.