Class: Karafka::Cli::Base
- Inherits:
 - 
      Object
      
        
- Object
 - Karafka::Cli::Base
 
 
- Includes:
 - Helpers::Colorize
 
- Defined in:
 - lib/karafka/cli/base.rb
 
Overview
Base class for all the command that we want to define This base class provides an interface to easier separate single independent commands
Instance Attribute Summary collapse
- 
  
    
      #options  ⇒ Hash 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    
Given command cli options.
 
Class Method Summary collapse
- 
  
    
      .aliases(*args)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Allows to set aliases for a given cli command.
 - 
  
    
      .commands  ⇒ Array<Class> 
    
    
  
  
  
  
  
  
  
  
  
    
Available commands.
 - 
  
    
      .desc(desc = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Allows to set description of a given cli command.
 - 
  
    
      .load  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Loads proper environment with what is needed to run the CLI.
 - 
  
    
      .name  ⇒ String 
    
    
  
  
  
  
  
  
  
  
  
    
Downcased current class name that we use to define name for given Cli command.
 - 
  
    
      .names  ⇒ Array<String> 
    
    
  
  
  
  
  
  
  
  
  
    
Names and aliases for command matching.
 - 
  
    
      .option(*option)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Allows to set options for Thor cli.
 - 
  
    
      .parse_options  ⇒ Hash 
    
    
  
  
  
  
  
  
  
  
  
    
Parses the CLI options.
 
Instance Method Summary collapse
- 
  
    
      #call  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
This method should implement proper cli action.
 - 
  
    
      #initialize  ⇒ Base 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    
Creates new CLI command instance.
 
Methods included from Helpers::Colorize
Constructor Details
#initialize ⇒ Base
Creates new CLI command instance
      14 15 16 17  | 
    
      # File 'lib/karafka/cli/base.rb', line 14 def initialize # Parses the given command CLI options @options = self.class. end  | 
  
Instance Attribute Details
#options ⇒ Hash (readonly)
Returns given command cli options.
      11 12 13  | 
    
      # File 'lib/karafka/cli/base.rb', line 11 def @options end  | 
  
Class Method Details
.aliases(*args) ⇒ Object
Allows to set aliases for a given cli command
      78 79 80 81  | 
    
      # File 'lib/karafka/cli/base.rb', line 78 def aliases(*args) @aliases ||= [] @aliases << args.map(&:to_s) end  | 
  
.commands ⇒ Array<Class>
Returns available commands.
      105 106 107 108 109 110 111  | 
    
      # File 'lib/karafka/cli/base.rb', line 105 def commands ObjectSpace .each_object(Class) .select { |klass| klass.superclass == Karafka::Cli::Base } .reject { |klass| klass.to_s.end_with?('::Base') } .sort_by(&:name) end  | 
  
.desc(desc = nil) ⇒ Object
Allows to set description of a given cli command
      72 73 74  | 
    
      # File 'lib/karafka/cli/base.rb', line 72 def desc(desc = nil) @desc ||= desc end  | 
  
.load ⇒ Object
Loads proper environment with what is needed to run the CLI
      43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60  | 
    
      # File 'lib/karafka/cli/base.rb', line 43 def load # If there is a boot file, we need to require it as we expect it to contain # Karafka app setup, routes, etc if File.exist?(::Karafka.boot_file) rails_env_rb = File.join(Dir.pwd, 'config/environment.rb') # Load Rails environment file that starts Rails, so we can reference consumers and # other things from `karafka.rb` file. This will work only for Rails, for non-rails # a manual setup is needed require rails_env_rb if Kernel.const_defined?(:Rails) && File.exist?(rails_env_rb) require Karafka.boot_file.to_s # However when it is unavailable, we still want to be able to run help command # and install command as they don't require configured app itself to run elsif %w[-h install].none? { |cmd| cmd == ARGV[0] } raise ::Karafka::Errors::MissingBootFileError, ::Karafka.boot_file end end  | 
  
.name ⇒ String
Returns downcased current class name that we use to define name for given Cli command.
      117 118 119  | 
    
      # File 'lib/karafka/cli/base.rb', line 117 def name to_s.split('::').last.downcase end  | 
  
.names ⇒ Array<String>
Returns names and aliases for command matching.
      122 123 124  | 
    
      # File 'lib/karafka/cli/base.rb', line 122 def names ((@aliases || []) << name).flatten.map(&:to_s).uniq end  | 
  
.option(*option) ⇒ Object
Allows to set options for Thor cli
      65 66 67 68  | 
    
      # File 'lib/karafka/cli/base.rb', line 65 def option(*option) @options ||= [] @options << option end  | 
  
.parse_options ⇒ Hash
Parses the CLI options
      85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102  | 
    
      # File 'lib/karafka/cli/base.rb', line 85 def = {} OptionParser.new do |opts| (@options || []).each do |option| # Creates aliases for backwards compatibility names = option[3].flat_map { |name| [name, name.tr('_', '-')] } names.map! { |name| "#{name} value1,value2,valueN" } if option[2] == Array names.uniq! opts.on( *[names, option[2], option[1]].flatten ) { |value| [option[0]] = value } end end.parse! end  | 
  
Instance Method Details
#call ⇒ Object
This method should implement proper cli action
      20 21 22  | 
    
      # File 'lib/karafka/cli/base.rb', line 20 def call raise NotImplementedError, 'Implement this in a subclass' end  |