Module: PDK::Util::PuppetStrings

Defined in:
lib/pdk/util/puppet_strings.rb

Defined Under Namespace

Classes: NoGeneratorError, NoObjectError, RunError

Class Method Summary collapse

Class Method Details

.all_objectsObject

Generate a list of all objects that PDK has a generator for.



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pdk/util/puppet_strings.rb', line 89

def self.all_objects
  require 'pdk/generate'

  generators = PDK::Generate::GENERATORS.select do |gen|
    gen.respond_to?(:puppet_strings_type) && !gen.puppet_strings_type.nil?
  end

  known_objects = generate_hash

  generators.map { |gen| [gen, known_objects[gen.puppet_strings_type]] }.reject do |_, obj|
    obj.nil? || obj.empty?
  end
end

.find_generator(type) ⇒ Object

Evaluates the mapping of puppet-strings object type to PDK generator class.

Examples:

PDK::Util::PuppetStrings.find_generator('puppet_classes')
=> PDK::Generate::PuppetClass

Parameters:

  • type (String)

    the object type as returned from puppet-strings.



114
115
116
117
118
119
120
# File 'lib/pdk/util/puppet_strings.rb', line 114

def self.find_generator(type)
  require 'pdk/generate'

  PDK::Generate::GENERATORS.find do |gen|
    gen.respond_to?(:puppet_strings_type) && gen.puppet_strings_type == type
  end
end

.find_object(name) ⇒ Object

Searches the puppet-strings result to find the definition of the named object.

Parameters:

  • name (String)

    the name of the object definition to search for. If the object name is not prepended with the module name, “#module_name::#object_name” will also be search for.

Raises:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pdk/util/puppet_strings.rb', line 63

def self.find_object(name)
  module_name = PDK::Util.['name'].rpartition('-').last

  object_names = [name]
  object_names << "#{module_name}::#{name}" unless name.start_with?("#{module_name}::")

  known_objects = generate_hash
  object_type = known_objects.keys.find do |obj_type|
    known_objects[obj_type].any? { |obj| object_names.include?(obj['name']) }
  end

  raise NoObjectError if object_type.nil?

  generator = find_generator(object_type)

  raise NoGeneratorError, object_type if generator.nil?

  [generator, known_objects[object_type].find { |obj| object_names.include?(obj['name']) }]
end

.generate_hashObject

Generates and parses the JSON output from puppet-strings.



37
38
39
40
41
42
43
44
45
46
# File 'lib/pdk/util/puppet_strings.rb', line 37

def self.generate_hash
  result = puppet('strings', 'generate', '--format', 'json')

  raise RunError, result[:stderr] unless result[:exit_code].zero?

  JSON.parse(result[:stdout])
rescue JSON::ParserError => e
  PDK.logger.debug(e)
  raise RunError, _('Unable to parse puppet-strings output')
end

.puppet(*args) ⇒ Hash{Symbol=>Object}

Runs Puppet for the purposes of generating puppet-strings output.

Parameters:

  • *args (String)

    additional parameters to pass to puppet.

Returns:

  • (Hash{Symbol=>Object})

    the result of the command execution.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pdk/util/puppet_strings.rb', line 13

def self.puppet(*args)
  require 'pdk/cli/exec/command'

  PDK::Util::Bundler.ensure_binstubs!('puppet')

  argv = [File.join(PDK::Util.module_root, 'bin', 'puppet')] + args
  argv.unshift(File.join(PDK::Util::RubyVersion.bin_path, 'ruby.exe')) if Gem.win_platform?

  command = PDK::CLI::Exec::Command.new(*argv).tap do |c|
    c.context = :module
    c.add_spinner(_('Examining module contents'))
  end

  command.execute!
end