class Crowbar::Client::App::Base

A base class that provides helper for the wrappers

Public Class Methods

banner(command, namespace = nil, subcommand = true) click to toggle source

A banner that gets displayed on help and error

@param command [String] the command for help output @param namespace [String] the namespace for help @param subcommand [Bool] the flag if it's a subcommand @return [String]

handle_argument_error(command, error, args, arity) click to toggle source

Properly print an error on invalid command

@param command [String] the command that failed @param error [StandardError] the error class @param args [Array] the arguments that failed @param arity [Integer] the number of arguments

# File lib/crowbar/client/app/base.rb, line 129
def handle_argument_error(command, error, args, arity)
  $stderr.puts("Usage: #{banner(command)}")
  exit(2)
end
new(args = [], local_options = {}, config = {}) click to toggle source

Initialize the Thor command

@param args [Array] the arguments @param local_options [Hash] the local options @param config [Hash] the configuration @return [Crowbar::Client::App::Base]

Calls superclass method
# File lib/crowbar/client/app/base.rb, line 35
def initialize(args = [], local_options = {}, config = {})
  super

  Config.configure(
    options.slice(
      :alias,
      :username,
      :password,
      :server,
      :verify_ssl,
      :timeout,
      :anonymous,
      :apiversion,
      :debug
    )
  )
end

Public Instance Methods

catch_errors(error) click to toggle source

General errors to catch properly

@param error [StandardError] the error to catch @raise [StandardError] only raised if uncatchable

# File lib/crowbar/client/app/base.rb, line 106
def catch_errors(error)
  case error
  when SimpleCatchableError
    err error.message, 1
  when Errno::ECONNREFUSED
    err "Connection to server refused", 1
  when SocketError
    err "Unknown server to connect to", 1
  else
    raise error
  end
end
command_params(args = {}) click to toggle source

Standard parameters for commands

@param args [Hash] the arguments to inject @return [Array]

# File lib/crowbar/client/app/base.rb, line 90
def command_params(args = {})
  [
    $stdin,
    $stdout,
    $stderr,
    options,
    args
  ]
end
err(message, exit_code = nil) click to toggle source

Print a message to STDERR

@param message [String] the message to print @param exit_code [Integer] the exit code to use

# File lib/crowbar/client/app/base.rb, line 71
def err(message, exit_code = nil)
  case provide_format
  when :json
    $stderr.puts JSON.pretty_generate(
      error: message
    )
  else
    $stderr.puts message
  end

  exit(exit_code) unless exit_code.nil?
end
say(message) click to toggle source

Print a message to STDOUT

@param message [String] the message to print

# File lib/crowbar/client/app/base.rb, line 61
def say(message)
  $stdout.puts message
end