class Brick::CLI

Public Class Methods

category(new_category) click to toggle source

Add catergory

# File lib/brick/cli.rb, line 139
 def self.category(new_category)
  @category = new_category
end
common_optparser() click to toggle source
# File lib/brick/cli.rb, line 53
def self.common_optparser
  @@common_optparser ||= Application.new
end
dependency_loaders() click to toggle source
# File lib/brick/cli.rb, line 147
def self.dependency_loaders
  @dependency_loaders ||= []
end
deps(&block) click to toggle source
# File lib/brick/cli.rb, line 151
def self.deps(&block)
  dependency_loaders << block
end
inherited(subclass) click to toggle source
# File lib/brick/cli.rb, line 22
def self.inherited(subclass)
  unless subclass.unnamed?
    subcommands[subclass.snake_case_name] = subclass
  end
end
list_commands(preferred_category=nil) click to toggle source

is given, only subcommands in that category are shown

# File lib/brick/cli.rb, line 93
def self.list_commands(preferred_category=nil)
  load_commands

  category_desc = preferred_category ? preferred_category + " " : ''
  logger.info "Available #{category_desc}subcommands: (for details, brick SUB-COMMAND --help)\n\n"

  if preferred_category && subcommands_by_category.key?(preferred_category)
    commands_to_show = {preferred_category => subcommands_by_category[preferred_category]}
  else
    commands_to_show = subcommands_by_category
  end

  commands_to_show.sort.each do |category, commands|
    next if category =~ /deprecated/i
    logger.info "** #{category.upcase} COMMANDS **"
    commands.sort.each do |command|
      logger.info subcommands[command].banner if subcommands[command]
    end
    logger.info 
  end
end
list_parameters() click to toggle source
# File lib/brick/cli.rb, line 87
def self.list_parameters
  puts common_optparser.opt_parser.to_s
  puts ""
end
load_commands() click to toggle source
# File lib/brick/cli.rb, line 129
def self.load_commands
  @commands_loaded ||= subcommand_loader.load_commands
end
load_deps() click to toggle source
# File lib/brick/cli.rb, line 155
 def self.load_deps
  dependency_loaders.each do |dep_loader|
    dep_loader.call
  end
end
logger() click to toggle source

include Application

# File lib/brick/cli.rb, line 12
def self.logger
  @@logger ||= Logger.new(STDOUT)
  @@logger.level = Logger::INFO
  @@logger.formatter = proc do |severity, datetime, progname, msg|
      "#{msg}\n"
   end
  @@logger
end
new(argv=[]) click to toggle source
Calls superclass method
# File lib/brick/cli.rb, line 161
    def initialize(argv=[])
      
      super() 
      command_name_words = self.class.snake_case_name.split('_')
      
      # Mixlib::CLI ignores the embedded name_args
      @cmd_args = ARGV.dup - command_name_words
      
      @cmd_args = parse_options @cmd_args
      @cmd_args.delete(command_name_words.join('-'))
      #@name_args.reject! { |name_arg| command_name_words.delete(name_arg) }
      
      
      Brick::Config.merge!(config)
      
      project_name = ::Brick::Config[:project]
      
      config_file = ::Brick::Config[:config_file]
      
      project_dir = File.dirname(config_file)
      
      ::Brick::Config[:project_dir] = project_dir
=begin
      if config[:help]
        logger.info opt_parser
        exit 1
      end
=end
      
    end
run(args, options={}) click to toggle source

Run command line for the given args (ARGV), adding options to the list of CLI options that the subcommand knows how to handle.

Arguments

args:

usually ARGV

options:

A Mixlib::CLI option parser hash. These options are how

subcommands know about global knife CLI options

# File lib/brick/cli.rb, line 38
def self.run(args, options={})
  #configure brick for common attributes
  #common_optparser.configure_brick
  common_optparser.opt_parser.banner="Usage: brick SUBCOMMAND (options)"
  CLI_Validator::validate
  #logger.info "begin to run the comand #{args}"
  load_commands
  subcommand_class = subcommand_class_from(args)
  subcommand_class.options = common_optparser.class.options.merge!(subcommand_class.options)
  subcommand_class.load_deps
  instance = subcommand_class.new(args)
  #instance.configure_brick
  instance.run_with_pretty_exceptions
end
snake_case_name() click to toggle source
# File lib/brick/cli.rb, line 125
def self.snake_case_name
  convert_to_snake_case(name.split('::').last) unless unnamed?
end
subcommand_category() click to toggle source
# File lib/brick/cli.rb, line 143
def self.subcommand_category
  @category || snake_case_name.split('_').first unless unnamed?
end
subcommand_class_from(args) click to toggle source
# File lib/brick/cli.rb, line 57
def self.subcommand_class_from(args)
  command_words = args.select {|arg| arg =~ /^(([[:alnum:]])[[:alnum:]\_\-]+)$/ }

  subcommand_class = nil

  while ( !subcommand_class ) && ( !command_words.empty? )
    snake_case_class_name = command_words.join("_")
    unless subcommand_class = subcommands[snake_case_class_name]
      command_words.pop
    end
  end
  # see if we got the command as e.g., brick model create
  subcommand_class ||= subcommands[args.first.gsub('-', '_')]
  subcommand_class || subcommand_not_found!(args)
end
subcommand_loader() click to toggle source
# File lib/brick/cli.rb, line 134
def self.subcommand_loader
  @subcommand_loader ||= CLI::SubcommandLoader.new
end
subcommand_not_found!(args) click to toggle source
# File lib/brick/cli.rb, line 77
def self.subcommand_not_found!(args)
  logger.fatal("Cannot find sub command for: '#{args.join(' ')}'")

 
  list_commands
  

  exit 10
end
subcommands() click to toggle source
# File lib/brick/cli.rb, line 73
def self.subcommands
  @@subcommands ||= {}
end
subcommands_by_category() click to toggle source
# File lib/brick/cli.rb, line 115
def self.subcommands_by_category
  unless @subcommands_by_category
    @subcommands_by_category = Hash.new { |hash, key| hash[key] = [] }
    subcommands.each do |snake_cased, klass|
      @subcommands_by_category[klass.subcommand_category] << snake_cased
    end
  end
  @subcommands_by_category
end
unnamed?() click to toggle source
# File lib/brick/cli.rb, line 28
def self.unnamed?
  name.nil? || name.empty?
end

Public Instance Methods

humanize_exception(e) click to toggle source
# File lib/brick/cli.rb, line 204
 def humanize_exception(e)
  case e
  when SystemExit
    raise # make sure exit passes through.
  when Errno::ECONNREFUSED, Timeout::Error, Errno::ETIMEDOUT, SocketError
    logger.error "Network Error: #{e.message}"
    logger.info "Check your network settings"
  else
    logger.error "#{e.class.name}: #{e.message}"
  end
end
run_with_pretty_exceptions() click to toggle source
# File lib/brick/cli.rb, line 193
def run_with_pretty_exceptions
  unless self.respond_to?(:run)
    logger.error "You need to add a #run method to your brick command before you can use it"
  end
  run
rescue Exception => e
  raise 
  humanize_exception(e)
  exit 100
end
show_usage() click to toggle source
# File lib/brick/cli.rb, line 216
def show_usage
   puts( self.opt_parser.to_s)
 end