Skip to content

Commit d94733c

Browse files
committed
Add load_metadata_json function
This function loads the metadata.json into a puppet variable. This enables a number of neat things such as: * Which version of the module am I using? 2.x? 3.x? * Which author of the module am I using? puppetlabs? example42?
1 parent 1282649 commit d94733c

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

README.markdown

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,17 @@ Loads a YAML file containing an array, string, or hash, and returns the data in
449449

450450
*Type*: rvalue.
451451

452+
#### `load_module_metadata`
453+
454+
Loads the metadata.json of a target module. Can be used to determine module version and authorship for dynamic support of modules.
455+
456+
~~~
457+
$metadata = load_module_metadata('archive')
458+
notify { $metadata['author']: }
459+
~~~
460+
461+
*Type*: rvalue.
462+
452463
#### `lstrip`
453464

454465
Strips spaces to the left of a string. *Type*: rvalue.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Puppet::Parser::Functions
2+
newfunction(:load_module_metadata, :type => :rvalue, :doc => <<-EOT
3+
EOT
4+
) do |args|
5+
raise(Puppet::ParseError, "load_module_metadata(): Wrong number of arguments, expects one") unless args.size == 1
6+
mod = args[0]
7+
module_path = function_get_module_path([mod])
8+
metadata_json = File.join(module_path, 'metadata.json')
9+
10+
raise(Puppet::ParseError, "load_module_metadata(): No metadata.json file for module #{mod}") unless File.exists?(metadata_json)
11+
12+
metadata = PSON.load(File.read(metadata_json))
13+
14+
return metadata
15+
end
16+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'spec_helper'
2+
3+
describe 'load_module_metadata' do
4+
it { is_expected.not_to eq(nil) }
5+
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
6+
it { is_expected.to run.with_params("one", "two").and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
7+
end

0 commit comments

Comments
 (0)