lib/arli/commands/search.rb in arli-0.8.3 vs lib/arli/commands/search.rb in arli-0.9.0

- old
+ new

@@ -2,80 +2,75 @@ require 'fileutils' require 'open3' require 'arli' require 'arli/commands/base' require 'arli/errors' +require 'arli/library/multi_version' require 'arduino/library' + module Arli module Commands class Search < Base - LibraryWithVersion = Struct.new(:name, :versions) - require 'arduino/library/include' attr_accessor :search_string, :search_opts, + :search_method, :results, :limit, :database, :format, - :hash, - :custom_print + :unique_libraries def initialize(*args) super(*args) - self.format = :short - self.hash = Hash.new - self.custom_print = true + self.format = config.search.results.output_format + valid_methods = Arli::Library::MultiVersion.format_methods + raise Arli::Errors::InvalidSearchSyntaxError, + "invalid format #{format}" unless valid_methods.include?(format) end def run - self.search_opts = process_search_options! - self.results = search(database, **search_opts).sort + self.search_opts = process_search_options! + self.results = search(database, **search_opts).sort + self.unique_libraries = Set.new - results.map do |lib| - hash[lib.name] ||= LibraryWithVersion.new(lib.name, []) - hash[lib.name].versions << lib.version + results.map { |lib| add_lib_or_version(lib) } - method = "to_s_#{format}".to_sym - if lib.respond_to?(method) - self.custom_print = false - lib.send(method) - end + unique_libraries.each do |multi_version| + puts multi_version.send("to_s_#{format}".to_sym) end - - if custom_print - previous = nil - results.each do |lib| - print_lib(hash[lib.name]) unless previous == lib.name - previous = lib.name - end - end print_total_with_help rescue Exception => e error e puts e.backtrace.join("\n") if ENV['DEBUG'] end - def print_lib(lib) - $stdout.puts "#{lib.name.bold.magenta} " + - (lib.versions ? - "(#{lib.versions.size} versions: #{lib.versions.reverse[0..5].join(', ').blue})": '') + def add_lib_or_version(lib) + a_version = Arli::Library::MultiVersion.new(lib) + if unique_libraries.include?(a_version) + unique_libraries.find { |l| l.name == a_version.name }&.add_version(library: lib) + else + unique_libraries << a_version + end end def process_search_options! self.search_string = extract_search_argument! raise_error_unless_search_string! self.limit = config.search.results.limit search_opts = {} begin - search_opts = eval("{ #{search_string} }") + params_code = "{ #{search_string} }" + puts "Evaluating: [#{params_code.blue}]\nSearch Method: [#{search_method.to_s.green}]" if config.trace + search_opts = eval(params_code) + rescue => e - handle_error(e) + handle_and_raise_error(e) end unless search_opts.is_a?(::Hash) && search_opts.size > 0 raise Arli::Errors::InvalidSearchSyntaxError, "Search string '#{search_string}' did not eval to Hash.\n" @@ -89,13 +84,23 @@ end def extract_search_argument! search = runtime.argv.first if search =~ /:/ + self.search_method = :ruby search + elsif search.start_with?('/') + self.search_method = :regex_name_and_url + # exact match + "#{config.search.default_field}: #{search}, archiveFileName: #{search}" + elsif search.start_with?('=') + self.search_method = :equals + # exact match + "#{config.search.default_field}: '#{search[1..-1]}'" elsif search - "#{config.search.default_field}: /^#{search}$/" + self.search_method = :regex + "#{config.search.default_field}: /#{search.downcase}/i" end end def raise_error_unless_search_string! unless search_string @@ -116,13 +121,13 @@ end def print_total_with_help puts "———————————————————————" puts " Total Versions : #{results.size.to_s.bold.magenta}\n" - puts "Unique Libraries : #{hash.keys.size.to_s.bold.magenta}\n" + puts "Unique Libraries : #{unique_libraries.size.to_s.bold.magenta}\n" puts "———————————————————————" if results.size == Arli::Configuration::DEFAULT_RESULTS_LIMIT - puts "Hint: use #{'-m 0'.bold.green} to disable the limit, or set it to another value." + puts "Hint: use #{'-m 5'.bold.green} to limit the result set." end end def params search_opts