Class: HammerCLI::AbstractCommand

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Subcommand

included

Constructor Details

#initialize(*args) ⇒ AbstractCommand

Returns a new instance of AbstractCommand.



120
121
122
123
124
125
126
127
# File 'lib/hammer_cli/abstract.rb', line 120

def initialize(*args)
  super
  context[:path] ||= []
  context[:path] << self
  self.class.command_extensions.each do |extension|
    extension.command_object(self)
  end
end

Class Attribute Details

.validation_blocksObject

Returns the value of attribute validation_blocks.



26
27
28
# File 'lib/hammer_cli/abstract.rb', line 26

def validation_blocks
  @validation_blocks
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



23
24
25
# File 'lib/hammer_cli/abstract.rb', line 23

def context
  @context
end

Class Method Details

.add_sets_help(help) ⇒ Object



65
66
67
68
69
# File 'lib/hammer_cli/abstract.rb', line 65

def add_sets_help(help)
  sets_details = HammerCLI::Help::Section.new(_('Predefined field sets'), nil, id: :s_sets_details, richtext: true)
  sets_details.definition << HammerCLI::Help::Text.new(output_definition.sets_table)
  help.definition.unshift(sets_details)
end

.build_options(builder_params = {}) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/hammer_cli/abstract.rb', line 197

def self.build_options(builder_params={})
  builder_params = yield(builder_params) if block_given?

  option_builder.build(builder_params).each do |option|
    # skip switches that are already defined
    next if option.nil? || option.switches.any? { |s| find_option(s) }

    adjust_family(option) if option.respond_to?(:family)
    declared_options << option
    block ||= option.default_conversion_block
    define_accessors_for(option, &block)
    extend_options_help(option) if option.value_formatter.is_a?(HammerCLI::Options::Normalizers::ListNested)
    completion_type_for(option)
  end
end

.command_extensionsObject



32
33
34
35
# File 'lib/hammer_cli/abstract.rb', line 32

def command_extensions
  @command_extensions = @command_extensions || inherited_command_extensions || []
  @command_extensions
end

.extend_help(&block) ⇒ Object



156
157
158
159
# File 'lib/hammer_cli/abstract.rb', line 156

def self.extend_help(&block)
  # We save the block for execution on object level, where we can access command's context and check :is_tty? flag
  self.help_extension_blocks << block
end

.extend_options_help(option) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hammer_cli/abstract.rb', line 46

def extend_options_help(option)
  extend_help do |h|
    begin
      h.find_item(:s_option_details)
    rescue ArgumentError
      option_details = HammerCLI::Help::Section.new(_('Option details'), nil, id: :s_option_details, richtext: true)
      option_details.definition << HammerCLI::Help::Text.new(
        _('Following parameters accept format defined by its schema ' \
          '(bold are required; <> contain acceptable type; [] contain acceptable value):')
      )
      h.definition.unshift(option_details)
    ensure
      h.find_item(:s_option_details).definition << HammerCLI::Help::List.new([
        [option.switches.last, option.value_formatter.schema.description]
      ])
    end
  end
end

.extend_output_definition(&block) ⇒ Object



161
162
163
164
165
166
# File 'lib/hammer_cli/abstract.rb', line 161

def self.extend_output_definition(&block)
  block.call(output_definition)
rescue ArgumentError => e
  handler = HammerCLI::ExceptionHandler.new
  handler.handle_exception(e)
end

.extend_with(*extensions) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/hammer_cli/abstract.rb', line 213

def self.extend_with(*extensions)
  extensions.each do |extension|
    unless extension.is_a?(HammerCLI::CommandExtensions)
      raise ArgumentError, _('Command extensions should be inherited from %s.') % HammerCLI::CommandExtensions
    end
    extension.delegatee(self)
    extension.command_class(self)
    extension.extend_predefined_options
    extension.extend_options
    extension.extend_option_family
    extension.extend_output
    extension.extend_help
    logger('Extensions').info "Applied #{extension.details} on #{self}."
    command_extensions << extension
  end
end

.help(invocation_path, builder = HammerCLI::Help::Builder.new) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/hammer_cli/abstract.rb', line 137

def self.help(invocation_path, builder = HammerCLI::Help::Builder.new)
  super(invocation_path, builder)
  help_extension = HammerCLI::Help::TextBuilder.new(builder.richtext)
  fields_switch = HammerCLI::Options::Predefined::OPTIONS[:fields].first[0]
  add_sets_help(help_extension) if find_option(fields_switch)
  unless help_extension_blocks.empty?
    help_extension_blocks.each do |extension_block|
      begin
        extension_block.call(help_extension)
      rescue ArgumentError => e
        handler = HammerCLI::ExceptionHandler.new
        handler.handle_exception(e)
      end
    end
  end
  builder.add_text(help_extension.string)
  builder.string
end

.help_extension_blocksObject



28
29
30
# File 'lib/hammer_cli/abstract.rb', line 28

def help_extension_blocks
  @help_extension_blocks ||= []
end

.inherited_command_extensionsObject



37
38
39
40
41
42
43
44
# File 'lib/hammer_cli/abstract.rb', line 37

def inherited_command_extensions
  extensions = nil
  if superclass.respond_to?(:command_extensions)
    parent_extensions = superclass.command_extensions.select(&:inheritable?)
    extensions = parent_extensions.dup unless parent_extensions.empty?
  end
  extensions
end

.option_builderObject



192
193
194
195
# File 'lib/hammer_cli/abstract.rb', line 192

def self.option_builder
  @option_builder ||= create_option_builder
  @option_builder
end

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



168
169
170
171
172
173
# File 'lib/hammer_cli/abstract.rb', line 168

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



183
184
185
186
# File 'lib/hammer_cli/abstract.rb', line 183

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

.use_option(*names) ⇒ Object



230
231
232
233
234
# File 'lib/hammer_cli/abstract.rb', line 230

def self.use_option(*names)
  names.each do |name|
    HammerCLI::Options::Predefined.use(name, self)
  end
end

.validate_options(mode = :append, target_name = nil, validator: nil, &block) ⇒ Object



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

def self.validate_options(mode=:append, target_name=nil, validator: nil, &block)
  validator ||= HammerCLI::Options::Validators::DSLBlockValidator.new(&block)
  self.validation_blocks ||= []
  self.validation_blocks << [mode, target_name, validator]
end

Instance Method Details

#adapterObject



72
73
74
# File 'lib/hammer_cli/abstract.rb', line 72

def adapter
  :base
end

#clean_up_contextObject



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

def clean_up_context
  context.delete(:fields)
end

#exception_handlerObject



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

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

#executeObject



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

def execute
  HammerCLI::EX_OK
end

#helpObject



133
134
135
# File 'lib/hammer_cli/abstract.rb', line 133

def help
  self.class.help(invocation_path, HammerCLI::Help::Builder.new(context[:is_tty?]))
end

#interactive?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/hammer_cli/abstract.rb', line 188

def interactive?
  HammerCLI.interactive?
end

#outputObject



175
176
177
# File 'lib/hammer_cli/abstract.rb', line 175

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

#output_definitionObject



179
180
181
# File 'lib/hammer_cli/abstract.rb', line 179

def output_definition
  self.class.output_definition
end

#parent_commandObject



129
130
131
# File 'lib/hammer_cli/abstract.rb', line 129

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

#parse(arguments) ⇒ Object



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

def parse(arguments)
  super
  validate_options
  logger.info "Called with options: %s" % options.inspect
rescue HammerCLI::Options::Validators::ValidationError => e
  signal_usage_error e.message
end

#run(arguments) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/hammer_cli/abstract.rb', line 76

def run(arguments)
  begin
    begin
      exit_code = super
      clean_up_context
      raise "exit code must be integer" unless exit_code.is_a? Integer
    rescue => e
      exit_code = handle_exception(e)
    end
    logger.debug 'Retrying the command' if (exit_code == HammerCLI::EX_RETRY)
  end while (exit_code == HammerCLI::EX_RETRY)
  return exit_code
end

#validate_optionsObject



112
113
114
# File 'lib/hammer_cli/abstract.rb', line 112

def validate_options
  # keep the method for legacy reasons
end