Class: Gpr::Commands::Search

Inherits:
Base
  • Object
show all
Includes:
Actions::Search
Defined in:
lib/gpr/commands/search.rb

Constant Summary

Constants included from Actions::Search

Actions::Search::DB_PATH

Constants included from Gpr

APP_PATH, VERSION

Instance Method Summary collapse

Methods included from Actions::Search

#add_file, #initialize_groonga

Methods inherited from Base

inherited

Constructor Details

#initialize(thor) ⇒ Search

Returns a new instance of Search.



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
# 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