class Gpr::Commands::Search

Public Class Methods

new(thor) click to toggle source
# File lib/gpr/commands/search.rb, line 12
def initialize(thor)
  initialize_groonga

  thor.class_eval do
    include ::Gpr::Actions::Search

    desc 'search', 'Search the specified keyword in registered repositories'
    option :file, type: :string, aliases: '-f', desc: 'Filter by filename'
    option :host, type: :string, aliases: '-h', desc: 'Filter by host'
    option :repository, type: :string, aliases: '-r', desc: 'Filter by repository'
    def search(string)
      result = Groonga['Files'].select do |record|
        record.body =~ string
      end

      result.each do |record|
        next unless FileTest.exist?(record._key)

        next if options[:file] && !(record.filename =~ /#{options[:file]}/)
        next if options[:host] && !(record.host == options[:host])
        next if options[:repository] && !(record.repository == options[:repository])

        relative_path = record._key.sub(/#{::Gpr::APP_PATH}\/#{record.host}\/#{record.repository}\//, '')
        puts "#{record.host.color(:yellow)} - #{record.repository.color(:blue)} : #{relative_path.color(:red)}"
      end
    end

    desc 'update', 'Update the database to be used for search'
    def update
      puts 'Updating...'

      Groonga['Files'].truncate

      # Remove unregistered directories
      Dir.glob("#{::Gpr::APP_PATH}/*/*").each do |path|
        # Unregistered directories returns [".", ".."].
        Dir.rmdir(path) if Dir.entries(path).size == 2
      end

      repositories = repository_list
      repositories.each do |repository|
        add_file(repository)
      end
    end
  end
end

Public Instance Methods

update() click to toggle source
# File lib/gpr/commands/search.rb, line 40
def update
  puts 'Updating...'

  Groonga['Files'].truncate

  # Remove unregistered directories
  Dir.glob("#{::Gpr::APP_PATH}/*/*").each do |path|
    # Unregistered directories returns [".", ".."].
    Dir.rmdir(path) if Dir.entries(path).size == 2
  end

  repositories = repository_list
  repositories.each do |repository|
    add_file(repository)
  end
end