Class: PuppetMetadata::BaseCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_metadata/base_command.rb

Overview

The base command. All actual commands should inherit from this.

Direct Known Subclasses

OsVersionsCommand, SetfilesCommand

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments, options) ⇒ BaseCommand

Returns a new instance of BaseCommand.



6
7
8
9
# File 'lib/puppet_metadata/base_command.rb', line 6

def initialize(arguments, options)
  @arguments = arguments
  @options = options
end

Class Method Details

.commandObject



28
29
30
31
32
33
34
# File 'lib/puppet_metadata/base_command.rb', line 28

def self.command
  name.gsub(/Command$/, '')
      .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
      .gsub(/([a-z\d])([A-Z])/, '\1_\2')
      .tr('_', '-')
      .downcase
end

.commandsObject



22
23
24
25
26
# File 'lib/puppet_metadata/base_command.rb', line 22

def self.commands
  PuppetMetadata::Command.load_commands

  subclasses.to_h { |subclass| [subclass.command, subclass] }
end

.parser(_options) ⇒ OptionParser

Return a parser. The program name should be overridden with a description

Parameters:

  • _options (Hash)

    The data to store the options into

Returns:

  • (OptionParser)

    The parser for the command

Raises:

  • (NotImplementedError)


41
42
43
# File 'lib/puppet_metadata/base_command.rb', line 41

def self.parser(_options)
  raise NotImplementedError
end

Instance Method Details

#filenameObject



11
12
13
# File 'lib/puppet_metadata/base_command.rb', line 11

def filename
  @filename ||= @options.fetch(:filename, 'metadata.json')
end

#metadataObject



15
16
17
18
19
20
# File 'lib/puppet_metadata/base_command.rb', line 15

def 
  @metadata ||= PuppetMetadata.read(filename)
rescue StandardError => e
  warn "Failed to read #{filename}: #{e}"
  exit 2
end

#runObject

Actual implementation of the command

Raises:

  • (NotImplementedError)


46
47
48
# File 'lib/puppet_metadata/base_command.rb', line 46

def run
  raise NotImplementedError
end