class Awssh::Search

Public Class Methods

new(servers, terms) click to toggle source
# File lib/awssh/search.rb, line 3
def initialize(servers, terms)
  @db = db(servers)
  @terms = convert(terms)
end

Public Instance Methods

convert(terms) click to toggle source
# File lib/awssh/search.rb, line 22
def convert(terms)
  terms.inject([]) do |a, e|
    opts = {}
    term = e.downcase
    if term =~ /\:/
      (key, value) = term.split(':')
    else
      key = 'name'
      value = term
      if term =~ /^\^/
        value = term.gsub(/^\^/, '')
        key = "^#{k}"
      end
    end
    if key =~ /^\^/
      opts[:inverse] = true
      key.gsub!(/^\^/)
    end
    a << [key, value, opts]
  end
end
db(servers) click to toggle source
# File lib/awssh/search.rb, line 44
def db(servers)
  servers.inject([]) { |a, s| a << "#{s[:id]}|| #{s[:tags].inject([]) { |a, e| a << e.join(':') }.join(' ')}" }
end
filter() click to toggle source
# File lib/awssh/search.rb, line 8
def filter
  list = @db
  @terms.each do |key, value, opts|
    regex = key == 'name' ? /\sname:[^\s]*#{value}[^\s]*/ : /\s#{key}:#{value}/
    if opts[:inverse]
      found = list.grep(regex)
      list = list - found
    else
      list = list.grep(regex)
    end
  end
  list
end