Class: Elasticshell::Commands::Ls

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

Instance Attribute Summary collapse

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

Instance Attribute Details

#scopeObject

Returns the value of attribute scope.



5
6
7
# File 'lib/elasticshell/commands/ls.rb', line 5

def scope
  @scope
end

Class Method Details

.matches?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.matches? input
  input =~ /^l(s|l|a)?(?: .+)?$/i
end

Instance Method Details

#evaluate!Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/elasticshell/commands/ls.rb', line 11

def evaluate!
  be_connected!
  if input =~ /^l(?:s|l|a)? +(.+)$/
    self.scope = shell.scope_from_path($1)
  else
    self.scope = shell.scope
  end
  self.scope.refresh!
  input =~ /^l(?:l|a)/ ? ll! : ls!
end

#ll!Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/elasticshell/commands/ls.rb', line 28

def ll!
  sort(scope.scopes).each do |scope_name|
    case
    when scope.path == '/' && scope.indices.include?(scope_name)
      index = shell.scope_from_path("/#{scope_name}")
      total_shards = index.status["_shards"]["total"]
      succ_shards  = index.status["_shards"]["successful"]
      fail_shards  = index.status["_shards"]["failed"]
      size         = index.status["indices"][scope_name]["index"]["size_in_bytes"]
      human_size   = index.status["indices"][scope_name]["index"]["size"]
      num_docs     = index.status["indices"][scope_name]["docs"]["num_docs"]
      shell.print("i %10s %6s %6s \e[32m%s\e[0m" % ["#{total_shards}/#{succ_shards}/#{fail_shards}", num_docs, human_size, scope_name])
    when scope.class == Scopes::Index && scope.mappings.include?(scope_name)
      shell.print("m                          \e[32m%s\e[0m" % [scope_name])
    else
      shell.print Elasticshell.format(:scope_long_format, "%s", scope_name)
    end
  end
  sort(scope.request_names).each do |request|
    shell.print Elasticshell.format(:request_long_format, "%r", request)
  end
end

#ls!Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/elasticshell/commands/ls.rb', line 51

def ls!
  output = []
  sort(scope.scopes).map do |scope|
    output << Elasticshell.format(:scope_format, "%s", scope)
  end
  sort(scope.request_names).map do |request|
    output << Elasticshell.format(:request_format, "%r", request)
  end
  shell.print output.join(' ')
end

#sort(array) ⇒ Object



22
23
24
25
26
# File 'lib/elasticshell/commands/ls.rb', line 22

def sort array
  with_underscores    = array.find_all { |element| element =~ /^_/    }
  without_underscores = array.find_all { |element| element =~ /^[^_]/ }
  without_underscores.sort + with_underscores.sort
end