Class: CouchShell::CorePlugin

Inherits:
Plugin
  • Object
show all
Defined in:
lib/couch-shell-plugin/core.rb

Instance Method Summary collapse

Methods inherited from Plugin

inherited, #initialize, #plugin_info, #plugin_initialization, #plugin_name, #shell, #variables_object

Methods included from PluginUtils

#continue?, #dbname!, #editfile!, #edittext!, #ensure_at_database, #request!

Constructor Details

This class inherits a constructor from CouchShell::Plugin

Instance Method Details

#execute_cd(argstr) ⇒ Object



119
120
121
# File 'lib/couch-shell-plugin/core.rb', line 119

def execute_cd(argstr)
  shell.cd shell.interpolate(argstr), false
end

#execute_cg(argstr) ⇒ Object



125
126
127
# File 'lib/couch-shell-plugin/core.rb', line 125

def execute_cg(argstr)
  shell.cd shell.interpolate(argstr), true
end

#execute_cput(argstr) ⇒ Object



101
102
103
104
# File 'lib/couch-shell-plugin/core.rb', line 101

def execute_cput(argstr)
  url = request_command_with_body("PUT", argstr)
  cd url if shell.responses.current(&:ok?)
end

#execute_delete(argstr) ⇒ Object



113
114
115
# File 'lib/couch-shell-plugin/core.rb', line 113

def execute_delete(argstr)
  shell.request "DELETE", shell.interpolate(argstr)
end

#execute_echo(argstr) ⇒ Object



148
149
150
151
152
# File 'lib/couch-shell-plugin/core.rb', line 148

def execute_echo(argstr)
  if argstr
    shell.stdout.puts shell.interpolate(argstr)
  end
end

#execute_exit(argstr) ⇒ Object

Raises:



130
131
132
# File 'lib/couch-shell-plugin/core.rb', line 130

def execute_exit(argstr)
  raise Quit
end

#execute_expand(argstr) ⇒ Object



181
182
183
# File 'lib/couch-shell-plugin/core.rb', line 181

def execute_expand(argstr)
  shell.stdout.puts shell.expand(shell.interpolate(argstr))
end

#execute_format(argstr) ⇒ Object

Raises:



163
164
165
166
167
168
169
170
171
# File 'lib/couch-shell-plugin/core.rb', line 163

def execute_format(argstr)
  raise ShellError, "expression required" unless argstr
  val = shell.eval_expr(argstr)
  if val.respond_to?(:couch_shell_format_string!)
    shell.stdout.puts val.couch_shell_format_string!
  else
    shell.stdout.puts val
  end
end

#execute_get(argstr) ⇒ Object



90
91
92
# File 'lib/couch-shell-plugin/core.rb', line 90

def execute_get(argstr)
  shell.request "GET", shell.interpolate(argstr)
end

#execute_member(argstr) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/couch-shell-plugin/core.rb', line 196

def execute_member(argstr)
  id, rev = nil, nil
  json = shell.responses.current(&:json_value)
  unless json && (id = json.attr_or_nil!("_id")) &&
      (rev = json.attr_or_nil!("_rev")) &&
      (shell.pathstack.size > 0) &&
      (shell.pathstack.last == id.to_s)
    raise ShellError,
      "`cg' the desired document first, e.g.: `cg /my_db/my_doc_id'"
  end
  # TODO: read json string as attribute name if argstr starts with double
  # quote
  attr_name, new_valstr = argstr.split(/\s+/, 2)
  unless attr_name && new_valstr
    raise ShellError,
      "attribute name and new value argument required"
  end
  if new_valstr == "remove"
    json.delete_attr!(attr_name)
  else
    new_val = JsonValue.parse(new_valstr)
    json.set_attr!(attr_name, new_val)
  end
  shell.request "PUT", "?rev=#{rev}", json.to_s
end

#execute_plugin(argstr) ⇒ Object



235
236
237
# File 'lib/couch-shell-plugin/core.rb', line 235

def execute_plugin(argstr)
  shell.plugin argstr
end

#execute_post(argstr) ⇒ Object



108
109
110
# File 'lib/couch-shell-plugin/core.rb', line 108

def execute_post(argstr)
  request_command_with_body("POST", argstr)
end

#execute_print(argstr) ⇒ Object

Raises:



156
157
158
159
# File 'lib/couch-shell-plugin/core.rb', line 156

def execute_print(argstr)
  raise ShellError, "expression required" unless argstr
  shell.stdout.puts shell.eval_expr(argstr)
end

#execute_put(argstr) ⇒ Object



96
97
98
# File 'lib/couch-shell-plugin/core.rb', line 96

def execute_put(argstr)
  request_command_with_body("PUT", argstr)
end

#execute_quit(argstr) ⇒ Object

Raises:



135
136
137
# File 'lib/couch-shell-plugin/core.rb', line 135

def execute_quit(argstr)
  raise Quit
end

#execute_server(argstr) ⇒ Object



175
176
177
# File 'lib/couch-shell-plugin/core.rb', line 175

def execute_server(argstr)
  shell.server = argstr
end

#execute_sh(argstr) ⇒ Object

Raises:



187
188
189
190
191
192
# File 'lib/couch-shell-plugin/core.rb', line 187

def execute_sh(argstr)
  raise ShellError, "argument required" unless argstr
  unless system(argstr)
    shell.errmsg "command exited with status #{$?.exitstatus}"
  end
end

#execute_user(argstr) ⇒ Object



225
226
227
228
229
230
231
# File 'lib/couch-shell-plugin/core.rb', line 225

def execute_user(argstr)
  shell.prompt_msg("Password:", false)
  shell.password = shell.read_secret
  # we save the username only after the password was entered
  # to allow cancellation during password input
  shell.username = argstr
end

#execute_uuids(argstr) ⇒ Object



141
142
143
144
# File 'lib/couch-shell-plugin/core.rb', line 141

def execute_uuids(argstr)
  count = argstr ? argstr.to_i : 1
  shell.request "GET", "/_uuids?count=#{count}"
end

#lookup_content_typeObject



40
41
42
# File 'lib/couch-shell-plugin/core.rb', line 40

def lookup_content_type
  shell.responses.current(&:content_type)
end

#lookup_idObject



25
26
27
# File 'lib/couch-shell-plugin/core.rb', line 25

def lookup_id
  shell.responses.current { |r| r.attr "id", "_id" } or raise VarNotSet
end

#lookup_idrObject



35
36
37
# File 'lib/couch-shell-plugin/core.rb', line 35

def lookup_idr
  shell.interpolate "$(id)?rev=$(rev)"
end

#lookup_prefix_j(name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/couch-shell-plugin/core.rb', line 59

def lookup_prefix_j(name)
  i = name.to_i
  if shell.responses.readable_index?(i)
    if shell.responses[i].json_value
      shell.responses[i].json_value
    else
      raise ShellError, "no json in response #{i}"
    end
  else
    raise ShellError, "no response index #{i}"
  end
end

#lookup_prefix_r(name) ⇒ Object

Raises:



52
53
54
55
56
# File 'lib/couch-shell-plugin/core.rb', line 52

def lookup_prefix_r(name)
  i = name.to_i
  raise VarNotSet unless shell.responses.readable_index?(i)
  shell.responses[i]
end

#lookup_revObject



30
31
32
# File 'lib/couch-shell-plugin/core.rb', line 30

def lookup_rev
  @responses.current { |r| r.attr "rev", "_rev" } or raise VarNotSet
end

#lookup_serverObject

Raises:



45
46
47
48
49
# File 'lib/couch-shell-plugin/core.rb', line 45

def lookup_server
  raise VarNotSet unless shell.server_url
  u = shell.server_url
  "#{u.scheme}://#{u.host}:#{u.port}#{u.path}"
end

#lookup_uuidObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/couch-shell-plugin/core.rb', line 10

def lookup_uuid
  shell.execute "uuids"
  if shell.responses.current(&:ok?)
    json = shell.responses.current.json_value
    if json && (uuids = json["uuids"]) && uuids.array? && uuids.length > 0
      uuids[0]
    else
      raise ShellError, "unkown json structure"
    end
  else
    raise ShellError, "uuids request failed"
  end
end

#request_command_with_body(method, argstr) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/couch-shell-plugin/core.rb', line 72

def request_command_with_body(method, argstr)
  if argstr =~ CouchShell::JSON_DOC_START_RX
    url, bodyarg = nil, argstr
  else
    url, bodyarg= argstr.split(/\s+/, 2)
  end
  if bodyarg && bodyarg.start_with?("@")
    filename, content_type = bodyarg[1..-1].split(/\s+/, 2)
    body = CouchShell::FileToUpload.new(filename, content_type)
  else
    body = bodyarg
  end
  real_url = shell.interpolate(url)
  shell.request method, real_url, body
  real_url
end