Module: Spacialdb::Command

Extended by:
Helpers
Defined in:
lib/spacialdb/command/version.rb,
lib/spacialdb/command.rb

Overview

display version

Defined Under Namespace

Classes: Auth, Base, CommandFailed, Db, Help, InvalidCommand, Layers, Version

Class Method Summary collapse

Methods included from Helpers

ask, confirm_command, display, echo_off, echo_on, error, home_directory, json_decode, json_encode, longest, redisplay

Class Method Details

.command_aliasesObject



21
22
23
# File 'lib/spacialdb/command.rb', line 21

def self.command_aliases
  @@command_aliases ||= {}
end

.commandsObject



17
18
19
# File 'lib/spacialdb/command.rb', line 17

def self.commands
  @@commands ||= {}
end

.confirmation_required(db, message) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/spacialdb/command.rb', line 148

def self.confirmation_required(db, message)
  display
  display
  display message
  display " !    To proceed, type \"#{db}\" or re-run this command with --confirm #{db}"
  display
  display "> ", false
  if ask.downcase != app
    display " !    Input did not match #{db}. Aborted."
    false
  else
    true
  end
end

.current_commandObject



37
38
39
# File 'lib/spacialdb/command.rb', line 37

def self.current_command
  @current_command
end

.extract_error(body) ⇒ Object



132
133
134
135
136
# File 'lib/spacialdb/command.rb', line 132

def self.extract_error(body)
  default_error = block_given? ? yield : "Internal server error"
  msg = parse_error_json(body) || parse_error_plain(body) || default_error
  msg.split("\n").map { |line| ' !   ' + line }.join("\n")
end

.global_option(name, *args) ⇒ Object



45
46
47
# File 'lib/spacialdb/command.rb', line 45

def self.global_option(name, *args)
  global_options << { :name => name, :args => args }
end

.global_optionsObject



41
42
43
# File 'lib/spacialdb/command.rb', line 41

def self.global_options
  @global_options ||= []
end

.loadObject



11
12
13
14
15
# File 'lib/spacialdb/command.rb', line 11

def self.load
  Dir[File.join(File.dirname(__FILE__), "command", "*.rb")].each do |file|
    require file
  end
end

.namespacesObject



25
26
27
# File 'lib/spacialdb/command.rb', line 25

def self.namespaces
  @namespaces ||= {}
end

.parse(cmd) ⇒ Object



128
129
130
# File 'lib/spacialdb/command.rb', line 128

def self.parse(cmd)
  commands[cmd] || commands[command_aliases[cmd]]
end

.parse_error_json(body) ⇒ Object



138
139
140
141
# File 'lib/spacialdb/command.rb', line 138

def self.parse_error_json(body)
  json = json_decode(body.to_s)
  json ? json['error'] : nil
end

.parse_error_plain(body) ⇒ Object



143
144
145
146
# File 'lib/spacialdb/command.rb', line 143

def self.parse_error_plain(body)
  return unless body.respond_to?(:headers) && body.headers[:content_type].to_s.include?("text/plain")
  body.to_s
end

.prepare_run(cmd, args = []) ⇒ Object

Raises:

  • (OptionParser::ParseError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/spacialdb/command.rb', line 53

def self.prepare_run(cmd, args=[])

  command = parse(cmd)

  unless command
    error " !   #{cmd} is not a spacialdb command. See 'spacialdb help'."
    return
  end

  @current_command = cmd

  opts = {}
  invalid_options = []

  parser = OptionParser.new do |parser|
    global_options.each do |global_option|
      parser.on(*global_option[:args]) do |value|
        opts[global_option[:name]] = value
      end
    end
    command[:options].each do |name, option|
      parser.on("-#{option[:short]}", "--#{option[:long]}", option[:desc]) do |value|
        opts[name.gsub("-", "_").to_sym] = value
      end
    end
  end

  begin
    parser.order!(args) do |nonopt|
      invalid_options << nonopt
    end
  rescue OptionParser::InvalidOption => ex
    invalid_options << ex.args.first
    retry
  end

  raise OptionParser::ParseError if opts[:help]

  args.concat(invalid_options)

  [ command[:klass].new(args.dup, opts.dup), command[:method] ]
end

.register_command(command) ⇒ Object



29
30
31
# File 'lib/spacialdb/command.rb', line 29

def self.register_command(command)
  commands[command[:command]] = command
end

.register_namespace(namespace) ⇒ Object



33
34
35
# File 'lib/spacialdb/command.rb', line 33

def self.register_namespace(namespace)
  namespaces[namespace[:name]] = namespace
end

.run(cmd, arguments = []) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/spacialdb/command.rb', line 96

def self.run(cmd, arguments=[])
  object, method = prepare_run(cmd, arguments)
  object.send(method)
rescue InvalidCommand
  error "Unknown command. Run 'spacialdb help' for usage information."
rescue RestClient::Unauthorized
  puts "Authentication failure"
  run "login"
  retry
rescue RestClient::ResourceNotFound => e
  error extract_error(e.http_body) {
    e.http_body =~ /^[\w\s]+ not found$/ ? e.http_body : "Resource not found"
  }
rescue RestClient::Locked => e
  db = e.response.headers[:x_confirmation_required]
  message = extract_error(e.response.body)
  if confirmation_required(db, message)
    opts[:confirm] = db
    retry
  end
rescue RestClient::RequestFailed => e
  error extract_error(e.http_body)
rescue RestClient::RequestTimeout
  error "API request timed out. Please try again, or contact [email protected] if this issue persists."
rescue CommandFailed => e
  error e.message
rescue OptionParser::ParseError => ex
  commands[cmd] ? run("help", [cmd]) : run("help")
rescue Interrupt => e
  error "\n[canceled]"
end