Module: Elasticshell

Defined in:
lib/elasticshell.rb,
lib/elasticshell/shell.rb,
lib/elasticshell/utils.rb,
lib/elasticshell/client.rb,
lib/elasticshell/runner.rb,
lib/elasticshell/scopes.rb,
lib/elasticshell/command.rb,
lib/elasticshell/utils/log.rb,
lib/elasticshell/commands/cd.rb,
lib/elasticshell/commands/df.rb,
lib/elasticshell/commands/ls.rb,
lib/elasticshell/utils/error.rb,
lib/elasticshell/commands/pwd.rb,
lib/elasticshell/scopes/index.rb,
lib/elasticshell/scopes/nodes.rb,
lib/elasticshell/commands/exit.rb,
lib/elasticshell/commands/help.rb,
lib/elasticshell/scopes/global.rb,
lib/elasticshell/commands/blank.rb,
lib/elasticshell/scopes/cluster.rb,
lib/elasticshell/scopes/mapping.rb,
lib/elasticshell/utils/has_name.rb,
lib/elasticshell/utils/has_verb.rb,
lib/elasticshell/commands/pretty.rb,
lib/elasticshell/utils/es_config.rb,
lib/elasticshell/commands/connect.rb,
lib/elasticshell/commands/request.rb,
lib/elasticshell/commands/unknown.rb,
lib/elasticshell/commands/set_verb.rb,
lib/elasticshell/utils/recognizes_verb.rb,
lib/elasticshell/commands/request_parser.rb

Defined Under Namespace

Modules: Commands, HasName, HasVerb, RecognizesVerb, Scopes Classes: Client, Command, ElasticsearchConfigFile, Scope, Shell

Constant Summary collapse

ORIG_ARGV =
ARGV.dup.freeze
Error =
Class.new(StandardError)
ArgumentError =
Class.new(Error)
NotImplementedError =
Class.new(Error)
ClientError =
Class.new(Error)
ShellError =
Class.new(Error)

Class Method Summary collapse

Class Method Details

.debug(msg) ⇒ Object



67
68
69
# File 'lib/elasticshell/utils/log.rb', line 67

def self.debug msg
  log.debug(format(:debug_format, "%m", msg))
end

.default_loggerObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/elasticshell/utils/log.rb', line 37

def self.default_logger
  Logger.new(Settings[:log]).tap do |l|
    begin
      l.level = Logger::Severity.const_get(Settings[:log_level].to_s.upcase)
    rescue NameError => e
      STDERR.puts "WARN: Log severity must be one of #{Logger::Severity.map(&:to_s).join(', ')}.  Setting severity to \"info\""
      l.level = Logger::Severity::INFO
    end
    l.formatter = proc do |severity, datetime, progname, msg|
      msg + "\n"
    end
  end
end

.error(msg) ⇒ Object



79
80
81
# File 'lib/elasticshell/utils/log.rb', line 79

def self.error msg
  log.error(format(:error_format, "%m", msg))
end

.find_servers_from_config_file!(path = nil) ⇒ Object



27
28
29
30
31
# File 'lib/elasticshell/runner.rb', line 27

def self.find_servers_from_config_file! path=nil
  return if overrode_servers_on_command_line?
  file = ElasticsearchConfigFile.new(path || Settings[:config])
  Settings[:servers] = file.hosts if file.readable? && (!file.hosts.empty?)
end

.format(name, codes, values) ⇒ Object

Raises:



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

def self.format name, codes, values
  cs = [codes].flatten
  vs = [values].flatten
  raise ArgumentError.new("Must provide the same number of format codes as value strings.") unless cs.length == vs.length
  Settings[name].dup.tap do |s|
    cs.each_with_index do |c, index|
      v = vs[index]
      s.gsub!(c, v)
    end
  end
end

.info(msg) ⇒ Object



71
72
73
# File 'lib/elasticshell/utils/log.rb', line 71

def self.info msg
  log.info(format(:info_format, "%m", msg))
end

.logObject



33
34
35
# File 'lib/elasticshell/utils/log.rb', line 33

def self.log
  @log ||= default_logger
end

.overrode_servers_on_command_line?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/elasticshell/runner.rb', line 23

def self.overrode_servers_on_command_line?
  ORIG_ARGV.any? { |arg| arg =~ /--servers/ }
end

.request(verb, host, path) ⇒ Object



63
64
65
# File 'lib/elasticshell/utils/log.rb', line 63

def self.request verb, host, path
  log.info(format(:http_request_format, ["%v", "%h", "%p"], [verb.to_s.upcase, host, path]))
end

.start(*args) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/elasticshell/runner.rb', line 33

def self.start *args
  begin
    Settings.resolve!
    find_servers_from_config_file!
    case
    when Settings[:version]
      puts version
      exit()
    when (Settings[:only] || Settings[:eval]) && Settings.rest.empty?
      raise ArgumentError.new("Starting with the --only or --eval options requires a request argument (like `/_cluster/health')")
      exit(1)
    when (! Settings.rest.empty?)
      es = Shell.new(Settings.merge(:log_requests => false))
      es.connect
      es.eval_line(Settings.rest.first)
      exit()
    else
      Shell.new(Settings).run
    end
  rescue Elasticshell::Error => e
    Elasticshell.error(e.message)
    exit(2)
  end
end

.versionObject



16
17
18
19
20
21
22
# File 'lib/elasticshell.rb', line 16

def self.version
  @version ||= begin
    File.read(File.expand_path('../../VERSION', __FILE__)).chomp
  rescue => e
    'unknown'
  end
end

.warn(msg) ⇒ Object



75
76
77
# File 'lib/elasticshell/utils/log.rb', line 75

def self.warn msg
  log.warn(format(:warn_format, "%m", msg))
end