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



94
95
96
97
# File 'lib/karafka/cli/base.rb', line 94

def aliases(*args)
  @aliases ||= []
  @aliases << args.map(&:to_s)
end

.commandsArray<Class>

Returns available commands.

Returns:

  • (Array<Class>)

    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

Parameters:

  • desc (String) (defaults to: nil)

    Description of a given cli command



88
89
90
# File 'lib/karafka/cli/base.rb', line 88

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
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

.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



133
134
135
# File 'lib/karafka/cli/base.rb', line 133

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



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

Parameters:

  • option

    Single option details

See Also:



81
82
83
84
# File 'lib/karafka/cli/base.rb', line 81

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

.parse_optionsHash

Parses the CLI options

Returns:

  • (Hash)

    hash with parsed values



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 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(ARGV)

  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