Class: HammerCLI::AbstractCommand

Inherits:
Clamp::Command
  • Object
show all
Defined in:
lib/hammer_cli/abstract.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ AbstractCommand

Returns a new instance of AbstractCommand.



55
56
57
58
59
# File 'lib/hammer_cli/abstract.rb', line 55

def initialize(*args)
  super
  context[:path] ||= []
  context[:path] << self
end

Class Attribute Details

.validation_blockObject

Returns the value of attribute validation_block.



14
15
16
# File 'lib/hammer_cli/abstract.rb', line 14

def validation_block
  @validation_block
end

Class Method Details

.inherited_output_definitionObject



105
106
107
108
109
110
111
112
# File 'lib/hammer_cli/abstract.rb', line 105

def self.inherited_output_definition
  od = nil
  if superclass.respond_to? :output_definition
    od_super = superclass.output_definition
    od = od_super.dup unless od_super.nil?
  end
  od
end

.output(definition = nil, &block) ⇒ Object



90
91
92
93
94
95
# File 'lib/hammer_cli/abstract.rb', line 90

def self.output(definition=nil, &block)
  dsl = HammerCLI::Output::Dsl.new
  dsl.build &block if block_given?
  output_definition.append definition.fields unless definition.nil?
  output_definition.append dsl.fields
end

.output_definitionObject



114
115
116
117
# File 'lib/hammer_cli/abstract.rb', line 114

def self.output_definition
  @output_definition = @output_definition || inherited_output_definition || HammerCLI::Output::Definition.new
  @output_definition
end

.remove_subcommand(name) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/hammer_cli/abstract.rb', line 65

def self.remove_subcommand(name)
  self.recognised_subcommands.delete_if do |sc|
    if sc.is_called?(name)
      logger.info "subcommand #{name} (#{sc.subcommand_class}) was removed."
      true
    else
      false
    end
  end
end

.subcommand(name, description, subcommand_class = self, &block) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/hammer_cli/abstract.rb', line 82

def self.subcommand(name, description, subcommand_class = self, &block)
  existing = find_subcommand(name)
  if existing
    raise HammerCLI::CommandConflict, "can't replace subcommand #{name} (#{existing.subcommand_class}) with #{name} (#{subcommand_class})"
  end
  super
end

.subcommand!(name, description, subcommand_class = self, &block) ⇒ Object



76
77
78
79
80
# File 'lib/hammer_cli/abstract.rb', line 76

def self.subcommand!(name, description, subcommand_class = self, &block)
  remove_subcommand(name)
  self.subcommand(name, description, subcommand_class, &block)
  logger.info "subcommand #{name} (#{subcommand_class}) was created."
end

.validate_options(&block) ⇒ Object



43
44
45
# File 'lib/hammer_cli/abstract.rb', line 43

def self.validate_options(&block)
  self.validation_block = block
end

Instance Method Details

#adapterObject



17
18
19
# File 'lib/hammer_cli/abstract.rb', line 17

def adapter
  :base
end

#exception_handlerObject



51
52
53
# File 'lib/hammer_cli/abstract.rb', line 51

def exception_handler
  @exception_handler ||= exception_handler_class.new(:output => output)
end

#executeObject



39
40
41
# File 'lib/hammer_cli/abstract.rb', line 39

def execute
  HammerCLI::EX_OK
end

#outputObject



97
98
99
# File 'lib/hammer_cli/abstract.rb', line 97

def output
  @output ||= HammerCLI::Output::Output.new(context, :default_adapter => adapter)
end

#output_definitionObject



101
102
103
# File 'lib/hammer_cli/abstract.rb', line 101

def output_definition
  self.class.output_definition
end

#parent_commandObject



61
62
63
# File 'lib/hammer_cli/abstract.rb', line 61

def parent_command
  context[:path][-2]
end

#parse(arguments) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/hammer_cli/abstract.rb', line 29

def parse(arguments)
  super
  validate_options
  safe_options = options.dup
  safe_options.keys.each { |k| safe_options[k] = '***' if k.end_with?('password') }
  logger.info "Called with options: %s" % safe_options.inspect
rescue HammerCLI::Validator::ValidationError => e
  signal_usage_error e.message
end

#run(arguments) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/hammer_cli/abstract.rb', line 21

def run(arguments)
  exit_code = super
  raise "exit code must be integer" unless exit_code.is_a? Integer
  return exit_code
rescue => e
  handle_exception e
end

#validate_optionsObject



47
48
49
# File 'lib/hammer_cli/abstract.rb', line 47

def validate_options
  validator.run &self.class.validation_block if self.class.validation_block
end