Skip to content

Commit e815da5

Browse files
committed
Merge pull request #483 from nibalizer/load_metadata_json
Add load_metadata_json function
2 parents d1f6c5c + f411ee7 commit e815da5

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

README.markdown

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

451451
*Type*: rvalue.
452452

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

455466
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
8+
it "should json parse the file" do
9+
allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/')
10+
allow(File).to receive(:exists?).with(/metadata.json/).and_return(true)
11+
allow(File).to receive(:read).with(/metadata.json/).and_return('{"name": "spencer-science"}')
12+
13+
result = subject.call(['science'])
14+
expect(result['name']).to eq('spencer-science')
15+
end
16+
end

0 commit comments

Comments
 (0)