Class: Karafka::Cli::Base

Inherits:
Object
  • Object
show all
Includes:
Thor::Shell
Defined in:
lib/karafka/cli/base.rb

Overview

Base class for all the command that we want to define This base class provides a nicer interface to Thor and allows to easier separate single independent commands In order to define a new command you need to:

- specify its desc
- implement call method

Examples:

Create a dummy command

class Dummy < Base
  self.desc = 'Dummy command'

  def call
    puts 'I'm doing nothing!
  end
end

Direct Known Subclasses

Console, Info, Install, Server, Topics

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli) ⇒ Base

Returns a new instance of Base.

Parameters:



27
28
29
# File 'lib/karafka/cli/base.rb', line 27

def initialize(cli)
  @cli = cli
end

Instance Attribute Details

#cliObject (readonly)

We can use it to call other cli methods via this object



24
25
26
# File 'lib/karafka/cli/base.rb', line 24

def cli
  @cli
end

Class Method Details

.bind_to(cli_class) ⇒ Object

This method will bind a given Cli command into Karafka Cli This method is a wrapper to way Thor defines its commands

Parameters:



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/karafka/cli/base.rb', line 74

def bind_to(cli_class)
  cli_class.desc name, @desc

  (@options || []).each { |option| cli_class.option(*option) }

  context = self

  cli_class.send :define_method, name do |*args|
    context.new(self).call(*args)
  end
end

.desc(desc) ⇒ Object

Allows to set description of a given cli command

Parameters:

  • desc (String)

    Description of a given cli command



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

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

.loadObject

Loads proper environment with what is needed to run the CLI



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/karafka/cli/base.rb', line 38

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

.option(*option) ⇒ Object

Allows to set options for Thor cli

Parameters:

  • option

    Single option details

See Also:



60
61
62
63
# File 'lib/karafka/cli/base.rb', line 60

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

Instance Method Details

#callObject

This method should implement proper cli action

Raises:

  • (NotImplementedError)


32
33
34
# File 'lib/karafka/cli/base.rb', line 32

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