Class: Elasticshell::Commands::Connect

Inherits:
Elasticshell::Command show all
Defined in:
lib/elasticshell/commands/connect.rb

Instance Attribute Summary

Attributes inherited from Elasticshell::Command

#input, #shell

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Elasticshell::Command

#be_connected!, #initialize

Constructor Details

This class inherits a constructor from Elasticshell::Command

Class Method Details

.matches?(input) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/elasticshell/commands/connect.rb', line 7

def self.matches? input
  input =~ /^connect(?: |$)/i
end

Instance Method Details

#evaluate!Object



11
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
# File 'lib/elasticshell/commands/connect.rb', line 11

def evaluate!
  servers = (input.split(/\s+/, 2)[1] || '').split(/\s+/)
  if servers.empty?
    shell.client.safely_connect()
  else
    uris = servers.map do |raw|
      has_port = raw =~ /:\d+/
      cooked  = (raw =~ /^http:\/\// ? raw : 'http://' + raw)
      cooked += '/' unless cooked =~ /\/$/
      begin
        uri = URI.parse(cooked)
        if uri.path == '/'
          uri.port = 9200 unless has_port
          uri.to_s
        else
          Elasticshell.warn("#{raw} is not a valid URI for an ElasticSearch server")
          nil
        end
      rescue => e
        Elasticshell.warn("#{raw} is not a valid URI")
        nil
      end
    end.compact
    shell.client.safely_connect(:servers => uris)
  end
end