class Magellan::Cli::Base

Public Class Methods

command_help(shell, command_name) click to toggle source

override Thor.command_help

# File lib/magellan/cli/base.rb, line 250
def command_help(shell, command_name)
  meth = normalize_command_name(command_name)
  command = all_commands[meth]
  handle_no_command_error(meth) unless command

  shell.say "Usage:"
  shell.say "  #{banner(command)}"
  shell.say
  class_options_help(shell, nil => command.options.map { |_, o| o })
  if command.long_description
    shell.say "Description:"
    shell.print_wrapped(command.long_description, :indent => 2)
  else
    shell.say command.description
  end
end
help(shell, subcommand = false) click to toggle source

overwrite Thor.help method

# File lib/magellan/cli/base.rb, line 236
def help(shell, subcommand = false)
  update_common_help_message
  if defined?(@package_name) && @package_name
    shell.say "#{@package_name} commands:"
  else
    shell.say "Commands:"
  end

  shell.print_table(sorted_printable_commands(true, subcommand), :indent => 2, :truncate => true)
  shell.say
  class_options_help(shell)
end
log_error(msg) click to toggle source
# File lib/magellan/cli/base.rb, line 181
def log_error(msg)
  puts_with_color(31, msg)
end
log_info(msg) click to toggle source
# File lib/magellan/cli/base.rb, line 172
def log_info(msg)
  puts_with_color(0, msg)
end
log_success(msg) click to toggle source
# File lib/magellan/cli/base.rb, line 178
def log_success(msg)
  puts_with_color(32, msg)
end
log_verbose(msg, flag = true) click to toggle source
# File lib/magellan/cli/base.rb, line 169
def log_verbose(msg, flag = true)
  puts_with_color(34, msg) if flag
end
log_warning(msg) click to toggle source
# File lib/magellan/cli/base.rb, line 175
def log_warning(msg)
  puts_with_color(33, msg)
end
puts_with_color(color_no, msg) click to toggle source
# File lib/magellan/cli/base.rb, line 165
def puts_with_color(color_no, msg)
  $stderr.puts("\e[#{color_no}m#{msg}\e[0m")
end
sorted_commands(all = true) click to toggle source
# File lib/magellan/cli/base.rb, line 185
def sorted_commands(all = true)
  cmd_hash = all_commands.dup
  Thor::Util.thor_classes_in(self).each do |klass|
    cmd_hash.update(klass.commands)
  end
  if order = self.const_get(:COMMAND_ORDER) rescue nil
    result = order.map{|i| cmd_hash[i]}
    result += (cmd_hash.keys - order).map{|i| cmd_hash[i]}
  else
    result = cmd_hash.values
  end
  if idx = result.index{|cmd| cmd.name == "help" }
    h = result.delete_at(idx)
    result << h
  end
  return result
end
sorted_printable_commands(all = true, subcommand = false) click to toggle source
# File lib/magellan/cli/base.rb, line 203
def sorted_printable_commands(all = true, subcommand = false)
  list = printable_commands(all, subcommand)
  Thor::Util.thor_classes_in(self).each do |klass|
    list += klass.printable_commands(false)
  end
  order = self.const_get(:COMMAND_ORDER) rescue nil
  if order
    orig = list
    list = order.map do |ptn|
      idx = orig.index{|t| t.first =~ /\b#{ptn}\b/}
      raise "#{ptn} not found" unless idx
      orig.delete_at(idx)
    end
    list += orig # add items not in COMMAND_ORDER
  end
  # # don't sort in alphabetical order
  # list.sort! { |a, b| a[0] <=> b[0] }

  # move help to the end of list
  if idx = list.index{|t| t.first =~ /\bhelp\b/ }
    h = list.delete_at(idx)
    list << h
  end
  return list
end
update_common_help_message() click to toggle source
# File lib/magellan/cli/base.rb, line 229
def update_common_help_message
  if cmd = all_commands["help"]
    cmd.description = I18n.t(:help, scope: [:base, :cmd])
  end
end

Public Instance Methods

delete(rel_path, &block) click to toggle source

ログインしてDELETEします @param [String] rel_path http.base_url からの相対パス @return nil

# File lib/magellan/cli/base.rb, line 148
def delete(rel_path, &block)
  http_access do |api|
    api.delete(rel_path, &block)
  end
end
dryrun?() click to toggle source
# File lib/magellan/cli/base.rb, line 25
def dryrun?
  opts[:dryrun]
end
fatal(msg) click to toggle source
# File lib/magellan/cli/base.rb, line 50
def fatal(msg)
  log_verbose(caller.join("\n  "))
  raise Cli::Error, msg
end
get_json(rel_path, params = {}, &block) click to toggle source

ログインしてGETします @param [String] rel_path http.base_url からの相対パス @param [Hash] params クエリ文字列 @return [Object] レスポンスのBODYをJSONとしてパースした結果オブジェクト

# File lib/magellan/cli/base.rb, line 99
def get_json(rel_path, params = {}, &block)
  http_access do |api|
    api.get_json(rel_path, params, &block)
  end
end
http_access() { |http_conn| ... } click to toggle source
# File lib/magellan/cli/base.rb, line 85
def http_access
  if block_given?
    http_conn.check_login_auth!
    return yield(http_conn)
  else
    return log_success(I18n.t(:ok, scope: [:http, :access_api]))
  end
end
http_conn() click to toggle source
# File lib/magellan/cli/base.rb, line 61
def http_conn
  @http_conn ||= Cli::Http.new(self)
end
log_error(msg) click to toggle source
# File lib/magellan/cli/base.rb, line 46
def log_error(msg)
  self.class.log_error(msg)
end
log_info(msg) click to toggle source
# File lib/magellan/cli/base.rb, line 37
def log_info(msg)
  self.class.log_info(msg)
end
log_success(msg) click to toggle source
# File lib/magellan/cli/base.rb, line 43
def log_success(msg)
  self.class.log_success(msg)
end
log_verbose(msg) click to toggle source
# File lib/magellan/cli/base.rb, line 33
def log_verbose(msg)
  self.class.log_verbose(msg) if verbose?
end
log_warning(msg) click to toggle source
# File lib/magellan/cli/base.rb, line 40
def log_warning(msg)
  self.class.log_warning(msg)
end
login!(email, password) click to toggle source
# File lib/magellan/cli/base.rb, line 65
def login!(email, password)
  logined = http_conn.api_login!(email, password)
  if logined
    log_success I18n.t(:success, scope: [:http, :login])
  else
    log_error I18n.t(:error, scope: [:http, :login])
    exit(1)
  end
end
login_by_token!(email, token) click to toggle source
# File lib/magellan/cli/base.rb, line 75
def login_by_token!(email, token)
  logined = http_conn.api_login_by_token!(email, token)
  if logined
    log_success I18n.t(:success, scope: [:http, :token])
  else
    log_error I18n.t(:error, scope: [:http, :token])
    exit(1)
  end
end
opts() click to toggle source
# File lib/magellan/cli/base.rb, line 21
def opts
  @opts ||= options || {}
end
post(rel_path, params, &block) click to toggle source

ログインしてPOSTします @param [String] rel_path http.base_url からの相対パス @param [Hash] params POSTで渡されるパラメータ @return nil

# File lib/magellan/cli/base.rb, line 109
def post(rel_path, params, &block)
  http_access do |api|
    api.post(rel_path, params, &block)
  end
end
post_json(rel_path, params, &block) click to toggle source

ログインしてJSON形式のbodyをPOSTします @param [String] rel_path http.base_url からの相対パス @param [Hash] params POSTで渡されるパラメータ @return nil

# File lib/magellan/cli/base.rb, line 119
def post_json(rel_path, params, &block)
  http_access do |api|
    api.post_json(rel_path, params, &block)
  end
end
process_error_response(res) click to toggle source
# File lib/magellan/cli/base.rb, line 154
def process_error_response(res)
  obj = JSON.parse(res.body) rescue nil
  if obj and obj.is_a?(Hash) and msg = obj["message"]
    fatal(msg)
  end
end
put(rel_path, params, &block) click to toggle source

ログインしてPUTします @param [String] rel_path http.base_url からの相対パス @param [Hash] params PUTで渡されるパラメータ @return nil

# File lib/magellan/cli/base.rb, line 129
def put(rel_path, params, &block)
  http_access do |api|
    api.put(rel_path, params, &block)
  end
end
put_json(rel_path, params, &block) click to toggle source

ログインしてJSON形式のbodyをPUTします @param [String] rel_path http.base_url からの相対パス @param [Hash] params PUTで渡されるパラメータ @return nil

# File lib/magellan/cli/base.rb, line 139
def put_json(rel_path, params, &block)
  http_access do |api|
    api.put_json(rel_path, params, &block)
  end
end
show_error_and_exit1(e) click to toggle source
# File lib/magellan/cli/base.rb, line 55
def show_error_and_exit1(e)
  log_error "[#{e.class}] #{e.message}"
  log_verbose("  " << e.backtrace.join("\n  "))
  exit(1)
end
sub(klass) click to toggle source
# File lib/magellan/cli/base.rb, line 15
def sub(klass)
  task = klass.new
  task.options = self.options
  task
end
verbose?() click to toggle source
# File lib/magellan/cli/base.rb, line 29
def verbose?
  opts[:verbose]
end