Class: Kafkat::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/kafkat/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/kafkat/cli.rb', line 3

def config
  @config
end

Class Method Details

.run!Object



5
6
7
# File 'lib/kafkat/cli.rb', line 5

def self.run!
  new.run
end

Instance Method Details

#bad_config_errorObject



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

def bad_config_error
  print "Configuration file failed to parse (~/.kafkatcfg).\n"
  exit 1
end

#load_configObject



14
15
16
17
18
19
20
# File 'lib/kafkat/cli.rb', line 14

def load_config
  @config = Config.load!
rescue Config::NotFoundError
  no_config_error
rescue Config::ParseError
  bad_config_error
end

#no_command_errorObject



65
66
67
68
69
# File 'lib/kafkat/cli.rb', line 65

def no_command_error
  print "This command isn't recognized.\n"
  print_commands
  exit 1
end

#no_config_errorObject



55
56
57
58
# File 'lib/kafkat/cli.rb', line 55

def no_config_error
  print "Configuration file not found (~/.kafkatcfg). See documentation.\n"
  exit 1
end


37
38
39
40
# File 'lib/kafkat/cli.rb', line 37

def print_banner
  print "kafkat #{VERSION}: Simplified command-line administration for Kafka brokers\n"
  print "usage: kafkat [command] [options]\n"
end


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/kafkat/cli.rb', line 42

def print_commands
  print "\nHere's a list of supported commands:\n\n"
  Command.all.values.sort_by(&:command_name).each do |klass|
    klass.usages.each do |usage|
      format, description = usage[0], usage[1]
      padding_length = 68 - format.length
      padding = " " * padding_length unless padding_length < 0
      print "  #{format}#{padding}#{description}\n"
    end
  end
  print "\n"
end

#runObject



9
10
11
12
# File 'lib/kafkat/cli.rb', line 9

def run
  load_config
  run_command
end

#run_commandObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kafkat/cli.rb', line 22

def run_command
  name = ARGV.shift
  if name
    command_klass = Command.get(name)
    command = command_klass.new(config)
    command.run
  else
    print_banner
    print_commands
    exit 0
  end
rescue Command::NotFoundError
  no_command_error
end