Module: Turbot::Command

Extended by:
Helpers
Defined in:
lib/turbot/command.rb

Defined Under Namespace

Classes: Auth, Base, Bots, Help, Version

Constant Summary

Constants included from Helpers

Helpers::DEFAULT_HOST

Class Method Summary collapse

Methods included from Helpers

api, ask, ask_for_password, ask_for_password_on_windows, delete_netrc_entry, email_address_and_api_key, error, format_error, host, netrc_exists?, netrc_path, open_netrc, save_netrc_entry, styled_error, turbot_api, turbot_api_parameters

Class Method Details

.command_aliasesObject



22
23
24
# File 'lib/turbot/command.rb', line 22

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

.commandsObject



18
19
20
# File 'lib/turbot/command.rb', line 18

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

.current_commandObject



42
43
44
# File 'lib/turbot/command.rb', line 42

def self.current_command
  @current_command
end

.extract_error(body, options = {}) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/turbot/command.rb', line 127

def self.extract_error(body, options = {})
  if block_given?
    default_error = yield
  else
    default_error = 'Internal server error'
  end
  parse_error_json(body) || default_error
end

.filesObject



26
27
28
# File 'lib/turbot/command.rb', line 26

def self.files
  @@files ||= Hash.new {|hash,key| hash[key] = File.readlines(key).map {|line| line.strip}}
end

.loadObject



5
6
7
8
9
10
# File 'lib/turbot/command.rb', line 5

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

.namespacesObject



30
31
32
# File 'lib/turbot/command.rb', line 30

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

.parse(command) ⇒ Object



123
124
125
# File 'lib/turbot/command.rb', line 123

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

.parse_error_json(body) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/turbot/command.rb', line 136

def self.parse_error_json(body)
  begin
    JSON.load(body.to_s)['message']
  rescue JSON::ParserError
    nil
  end
end

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



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
95
96
97
98
99
100
101
102
# File 'lib/turbot/command.rb', line 59

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

  @current_command = cmd

  opts = {}
  invalid_options = []

  parser = OptionParser.new do |parser|
    # remove OptionParsers Officious['version'] to avoid conflicts
    # see: https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L814
    parser.base.long.delete('version')
    (command && command[:options] || []).each do |option|
      parser.on(*option[:args]) do |value|
        opts[option[:name].gsub('-', '_').to_sym] = value
        ARGV.join(' ') =~ /(#{option[:args].map {|arg| arg.split(' ', 2).first}.join('|')})/
      end
    end
  end

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

  args.concat(invalid_options)

  @invalid_arguments = invalid_options

  if command
    command_instance = command[:klass].new(args.dup, opts.dup)
    [ command_instance, command[:method] ]
  else
    error([
      "`#{cmd}` is not a turbot command.",
      suggestion(cmd, commands.keys + command_aliases.keys),
      'See `turbot help` for a list of available commands.'
    ].compact.join("\n"))
  end
end

.register_command(command) ⇒ Object



34
35
36
# File 'lib/turbot/command.rb', line 34

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

.register_namespace(namespace) ⇒ Object



38
39
40
# File 'lib/turbot/command.rb', line 38

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

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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/turbot/command.rb', line 104

def self.run(command, arguments=[])
  begin
    object, method = prepare_run(command, arguments.dup)
    object.send(method)
  rescue Interrupt, StandardError, SystemExit => error
    # load likely error classes, as they may not be loaded yet due to defered loads
    require 'rest_client'
    raise(error)
  end
rescue SocketError => e
  error 'Unable to connect to Turbot API, please check internet connectivity and try again.'
rescue OptionParser::ParseError
  if commands[command]
    run('help', [command])
  else
    run('help')
  end
end

.unregister_commands_made_private_after_the_factObject



12
13
14
15
16
# File 'lib/turbot/command.rb', line 12

def self.unregister_commands_made_private_after_the_fact
  commands.values.
    select { |c| c[:klass].private_method_defined? c[:method] }.
    each { |c| commands.delete(c[:command]) }
end

.validate_arguments!Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/turbot/command.rb', line 46

def self.validate_arguments!
  unless @invalid_arguments.empty?
    arguments = @invalid_arguments.map(&:inspect)
    if arguments.length == 1
      message = "Invalid argument: #{arguments.first}"
    else
      message = "Invalid arguments: #{arguments[0...-1].join(', ')} and #{arguments[-1]}"
    end
    run('help', [current_command])
    error message
  end
end