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.



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

def self.all_objects
  require 'pdk/generate'

  generators = PDK::Generate.generators.select do |gen|
    gen.const_defined?(: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.



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

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

  PDK::Generate.generators.find do |gen|
    gen.const_defined?(:PUPPET_STRINGS_TYPE) && type == gen::PUPPET_STRINGS_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:



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

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.



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

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.



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

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