Class: Elasticshell::Shell

Inherits:
Object
  • Object
show all
Includes:
HasVerb
Defined in:
lib/elasticshell/shell.rb

Constant Summary

Constants included from HasVerb

HasVerb::VERBS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HasVerb

#verb, #verb=

Constructor Details

#initialize(options = {}) ⇒ Shell

Returns a new instance of Shell.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/elasticshell/shell.rb', line 85

def initialize options={}
  @interactive = false
  self.state  = :init
  self.client = Client.new(options)
  self.cache  = {}
  @initial_servers = (options[:servers] || [])
  self.verb   = (options[:verb] || 'GET')
  self.scope  = scope_from_path(options[:scope] || '/')
  self.only   = options[:only]
  self.ruby_code = options[:eval]
  self.input_stream  = (options[:input]  || $stdin)
  self.output = (options[:output] || $stdout)
  self.line   = 0
  @log_requests = (options[:log_requests] == false ? false : true)
  pretty! if options[:pretty]
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



65
66
67
# File 'lib/elasticshell/shell.rb', line 65

def cache
  @cache
end

#clientObject

Returns the value of attribute client.



65
66
67
# File 'lib/elasticshell/shell.rb', line 65

def client
  @client
end

#inputObject

Returns the value of attribute input.



65
66
67
# File 'lib/elasticshell/shell.rb', line 65

def input
  @input
end

#input_streamObject

Returns the value of attribute input_stream.



65
66
67
# File 'lib/elasticshell/shell.rb', line 65

def input_stream
  @input_stream
end

#lineObject

Returns the value of attribute line.



65
66
67
# File 'lib/elasticshell/shell.rb', line 65

def line
  @line
end

#onlyObject

Returns the value of attribute only.



65
66
67
# File 'lib/elasticshell/shell.rb', line 65

def only
  @only
end

#outputObject

Returns the value of attribute output.



65
66
67
# File 'lib/elasticshell/shell.rb', line 65

def output
  @output
end

#ruby_codeObject

Returns the value of attribute ruby_code.



65
66
67
# File 'lib/elasticshell/shell.rb', line 65

def ruby_code
  @ruby_code
end

#scopeObject

Returns the value of attribute scope.



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

def scope
  @scope
end

#stateObject

Returns the value of attribute state.



65
66
67
# File 'lib/elasticshell/shell.rb', line 65

def state
  @state
end

Instance Method Details

#commandObject

Raises:



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/elasticshell/shell.rb', line 179

def command
  matching_command_class_name = Commands::PRIORITY.detect do |command_class_name|
    Commands.const_get(command_class_name).matches?(input)
  end
  
  # We should never hit the following ArgumentError as there
  # exists a catch-all command: Unknown
  raise ArgumentError.new("Could not parse command: '#{input}'") unless matching_command_class_name
  
  Commands.const_get(matching_command_class_name).new(self, input)
end

#connectObject



154
155
156
# File 'lib/elasticshell/shell.rb', line 154

def connect
  eval_line("connect #{@initial_servers.join(' ')}")
end

#connected?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/elasticshell/shell.rb', line 81

def connected?
  client.connected?
end

#dieObject

Raises:



231
232
233
# File 'lib/elasticshell/shell.rb', line 231

def die
  raise ShellError.new("C-d...quitting")
end

#eval_line(line) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/elasticshell/shell.rb', line 166

def eval_line line
  begin
    self.state = :eval
    self.input = line.strip
    command.evaluate!
  rescue ::Elasticshell::Error => e
    Elasticshell.error e.message
  end
  self.state = :read
  self.line += 1
  self
end

#format_only_part_of(obj) ⇒ Object



212
213
214
215
216
217
218
219
220
# File 'lib/elasticshell/shell.rb', line 212

def format_only_part_of obj
  only_parts = self.only.to_s.split('.')
  obj_to_print = obj
  while obj_to_print && only_parts.size > 0
    this_only = only_parts.shift
    obj_to_print = (obj_to_print || {})[this_only]
  end
  format_output(obj_to_print, true)
end

#format_output(obj, ignore_only = false) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/elasticshell/shell.rb', line 195

def format_output obj, ignore_only=false
  case
  when self.only == true && !ignore_only
    format_output(obj, true)
  when self.only && !ignore_only
    format_only_part_of(obj)
  when obj.nil?
    nil
  when obj.is_a?(String) || obj.is_a?(Fixnum)
    obj
  when pretty?
    JSON.pretty_generate(obj)
  else
    obj.to_json
  end
end

#intObject



222
223
224
225
226
227
228
229
# File 'lib/elasticshell/shell.rb', line 222

def int
  case self.state
  when :read
    $stdout.write("^C\n#{prompt}")
  else
    $stdout.write("^C...aborted\n#{prompt}")
  end
end

#interactive?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/elasticshell/shell.rb', line 128

def interactive?
  @interactive
end

#log_requests?Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/elasticshell/shell.rb', line 235

def log_requests?
  @log_requests
end

#loopObject



158
159
160
161
162
163
164
# File 'lib/elasticshell/shell.rb', line 158

def loop
  self.state = :read
  while line = Readline.readline(prompt, true)
    eval_line(line)
  end
  die
end

#not_pretty!Object



124
125
126
# File 'lib/elasticshell/shell.rb', line 124

def not_pretty!
  @pretty = false
end

#pathObject



77
78
79
# File 'lib/elasticshell/shell.rb', line 77

def path
  scope.path
end

#pretty!Object



120
121
122
# File 'lib/elasticshell/shell.rb', line 120

def pretty!
  @pretty = true
end

#pretty?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/elasticshell/shell.rb', line 116

def pretty?
  @pretty
end


191
192
193
# File 'lib/elasticshell/shell.rb', line 191

def print obj, ignore_only=false
  self.output.puts(format_output(obj, ignore_only))
end

#promptObject



110
111
112
113
114
# File 'lib/elasticshell/shell.rb', line 110

def prompt
  verb_string  = Elasticshell.format((verb.to_s =~ /^(?:G|H)/i ? :passive_http_verb_format : :active_http_verb_format), "%v", verb.to_s.upcase)
  scope_string = Elasticshell.format((scope.exists? ? :existing_scope_format : :missing_scope_format), "%s", scope.path)
  Elasticshell.format((pretty? ? :pretty_prompt_format : :prompt_format), ["%s", "%v"], [scope_string, verb_string])
end

#runObject



148
149
150
151
152
# File 'lib/elasticshell/shell.rb', line 148

def run
  setup
  connect
  loop
end

#scope_from_path(path) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/elasticshell/shell.rb', line 102

def scope_from_path path
  if cache[path]
    cache[path]
  else
    cache[path] = Scopes.from_path(path, :client => self.client)
  end
end

#setupObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/elasticshell/shell.rb', line 132

def setup
  trap("INT") do
    int
  end

  Readline.completer_word_break_characters = " \t\n\"\\'`$><=|&{("
  
  print <<EOF
Elasticshell v. #{Elasticshell.version}
Type "help" for contextual help.
EOF
  @interactive = true

  self.line = 1
end