class Querly::CLI

Public Class Methods

source_root() click to toggle source
# File lib/querly/cli.rb, line 126
def self.source_root
  File.join(__dir__, "../..")
end

Public Instance Methods

check(*paths) click to toggle source
# File lib/querly/cli.rb, line 16
    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
console(*paths) click to toggle source
# 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?
  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
find(pattern, *paths) click to toggle source
# File lib/querly/cli.rb, line 93
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
init() click to toggle source
# File lib/querly/cli.rb, line 133
def init()
  copy_file("template.yml", "querly.yml")
end
rules(*ids) click to toggle source
# File lib/querly/cli.rb, line 116
def rules(*ids)
  require "querly/cli/rules"
  Rules.new(config_path: config_path, ids: ids).run
end
test() click to toggle source
# File lib/querly/cli.rb, line 109
def test()
  require "querly/cli/test"
  exit Test.new(config_path: config_path).run
end
version() click to toggle source
# File lib/querly/cli.rb, line 122
def version
  puts "Querly #{VERSION}"
end

Private Instance Methods

config(root_option:) click to toggle source
# File lib/querly/cli.rb, line 139
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
config_path() click to toggle source
# File lib/querly/cli.rb, line 146
def config_path
  [Pathname(options[:config]),
   Pathname("querly.yaml")].compact.find(&:file?) || Pathname(options[:config])
end