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
94 95 96 97 |
# File 'lib/karafka/cli/base.rb', line 94 def aliases(*args) @aliases ||= [] @aliases << args.map(&:to_s) end |
.commands ⇒ Array<Class>
Returns available commands.
121 122 123 124 125 126 127 |
# File 'lib/karafka/cli/base.rb', line 121 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
88 89 90 |
# File 'lib/karafka/cli/base.rb', line 88 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/karafka/cli/base.rb', line 43 def load rails_env_rb = File.join(Dir.pwd, 'config/environment.rb') is_rails = Kernel.const_defined?(:Rails) && File.exist?(rails_env_rb) # If the boot file is disabled and this is a Rails app, we assume that user moved the # karafka app configuration to initializers or other Rails loading related place. # It is not recommended but some users tend to do this. In such cases we just try to load # the Rails stuff hoping that it will also load Karafka stuff if Karafka.boot_file.to_s == 'false' && is_rails require rails_env_rb return end # 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) # 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 is_rails require Karafka.boot_file.to_s return end # 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 return if %w[-h install].any? { |cmd| cmd == ARGV[0] } # All other commands except help and install do require an existing boot file if it was # declared raise ::Karafka::Errors::MissingBootFileError, ::Karafka.boot_file end |
.name ⇒ String
Returns downcased current class name that we use to define name for given Cli command.
133 134 135 |
# File 'lib/karafka/cli/base.rb', line 133 def name to_s.split('::').last.downcase end |
.names ⇒ Array<String>
Returns names and aliases for command matching.
138 139 140 |
# File 'lib/karafka/cli/base.rb', line 138 def names ((@aliases || []) << name).flatten.map(&:to_s).uniq end |
.option(*option) ⇒ Object
Allows to set options for Thor cli
81 82 83 84 |
# File 'lib/karafka/cli/base.rb', line 81 def option(*option) @options ||= [] @options << option end |
.parse_options ⇒ Hash
Parses the CLI options
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/karafka/cli/base.rb', line 101 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(ARGV) 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 |