Module: CouchShell::PluginUtils

Included in:
Plugin
Defined in:
lib/couch-shell/plugin_utils.rb

Overview

Requires an instance method named “shell” that returns a CouchShell::Shell compatible object.

Defined Under Namespace

Classes: ShellError, VarNotSet

Instance Method Summary collapse

Instance Method Details

#continue?(msg) ⇒ Boolean

Displays msg in the same style as the standard couch-shell prompt and raises ShellError unless the user hits ENTER and nothing else.

Usage:

# do some setup logic
continue?("Changes made: foo replaced by bar\n" +
          "Press ENTER to save changes or CTRL+C to cancel")
# save changes

Returns:

  • (Boolean)


47
48
49
50
51
52
# File 'lib/couch-shell/plugin_utils.rb', line 47

def continue?(msg)
  shell.prompt_msg(msg, false)
  unless shell.stdin.gets.chomp.empty?
    raise ShellError, "cancelled"
  end
end

#dbname!Object

Get the first element of pathstack or raise an exception if pathstack is empty.

Raises:



26
27
28
29
# File 'lib/couch-shell/plugin_utils.rb', line 26

def dbname!
  raise ShellError, "must cd into database" if shell.pathstack.empty?
  shell.pathstack[0]
end

#editfile!(path) ⇒ Object

Opens editor with the given file path and returns after the user closes the editor or raises a ShellError if the editor doesn’t exit with an exit status of 0.



65
66
67
68
69
# File 'lib/couch-shell/plugin_utils.rb', line 65

def editfile!(path)
  unless system(shell.editor_bin!, path)
    raise ShellError, "editing command failed with exit status #{$?.exitstatus}"
  end
end

#edittext!(content, tempfile_name_ext = ".js", tempfile_name_part = "cs") ⇒ Object

Writes content to a temporary file, calls editfile! on it and returns the new file content on success after unlinking the temporary file.



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/couch-shell/plugin_utils.rb', line 73

def edittext!(content, tempfile_name_ext = ".js", tempfile_name_part = "cs")
  t = Tempfile.new([tempfile_name_part, tempfile_name_ext])
  t.write(content)
  t.close
  editfile! t.path
  t.open.read
ensure
  if t
    t.close
    t.unlink
  end
end

#ensure_at_databaseObject

Raise an exception unless pathstack size is 1.



32
33
34
35
36
# File 'lib/couch-shell/plugin_utils.rb', line 32

def ensure_at_database
  if shell.pathstack.size != 1
    raise ShellError, "current directory must be database"
  end
end

#request!(*args) ⇒ Object

Like shell.request, but raises a ShellError (“required request failed”) if response.ok? is false.



56
57
58
59
60
# File 'lib/couch-shell/plugin_utils.rb', line 56

def request!(*args)
  shell.request(*args).tap do |res|
    raise ShellError, "required request failed" unless res.ok?
  end
end