Class: Karafka::Cli::Base

Inherits:
Object
  • Object
show all
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

Direct Known Subclasses

Console, Help, Info, Install, Server, Swarm, Topics

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Colorize

#green, #grey, #red, #yellow

Constructor Details

#initializeBase

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.parse_options
end

Instance Attribute Details

#optionsHash (readonly)

Returns given command cli options.

Returns:

  • (Hash)

    given command cli options



11
12
13
# File 'lib/karafka/cli/base.rb', line 11

def options
  @options
end

Class Method Details

.aliases(*args) ⇒ Object

Allows to set aliases for a given cli command

Parameters:

  • args (Array)

    list of aliases that we can use to run 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

.commandsArray<Class>

Returns available commands.

Returns:

  • (Array<Class>)

    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

Parameters:

  • desc (String) (defaults to: nil)

    Description of a given cli command



72
73
74
# File 'lib/karafka/cli/base.rb', line 72

def desc(desc = nil)
  @desc ||= desc
end

.loadObject

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

.nameString

Returns downcased current class name that we use to define name for given Cli command.

Examples:

for Karafka::Cli::Install

name #=> 'install'

Returns:

  • (String)

    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

.namesArray<String>

Returns names and aliases for command matching.

Returns:

  • (Array<String>)

    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

Parameters:

  • option

    Single option details

See Also:



65
66
67
68
# File 'lib/karafka/cli/base.rb', line 65

def option(*option)
  @options ||= []
  @options << option
end

.parse_optionsHash

Parses the CLI options

Returns:

  • (Hash)

    hash with parsed values



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 parse_options
  options = {}

  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| options[option[0]] = value }
    end
  end.parse!

  options
end

Instance Method Details

#callObject

This method should implement proper cli action

Raises:

  • (NotImplementedError)


20
21
22
# File 'lib/karafka/cli/base.rb', line 20

def call
  raise NotImplementedError, 'Implement this in a subclass'
end