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, Flow, Info, Install, Missingno, Server

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:



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/karafka/cli/base.rb', line 54

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(*args) ⇒ Object

Allows to set description of a given cli command

Parameters:

  • args (Array)

    All the arguments that Thor desc method accepts



47
48
49
# File 'lib/karafka/cli/base.rb', line 47

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

.option(*option) ⇒ Object

Allows to set options for Thor cli

Parameters:

  • option

    Single option details

See Also:



40
41
42
43
# File 'lib/karafka/cli/base.rb', line 40

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