Skip to content

Instantly share code, notes, and snippets.

@mgagne
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgagne/11069487 to your computer and use it in GitHub Desktop.
Save mgagne/11069487 to your computer and use it in GitHub Desktop.
#
# puppet_module_version.rb
#
require 'puppet/parser/functions'
Puppet::Parser::Functions.newfunction(:puppet_module_version,
:type => :rvalue,
:doc => <<-EOS
This function parses the module version from Modulefile or metadata.json.
EOS
) do |arguments|
if (arguments.size != 1) then
raise(Puppet::ParseError, "puppet_module_version(): Wrong number of arguments " +
"given (#{arguments.size} for 1)")
end
environment = Puppet::Node::Environment.new
puppet_module = environment.module(arguments[0])
return '0.0.0' if puppet_module.nil?
metadata = Puppet::ModuleTool::Metadata.new
modulefile_path = File.join(puppet_module.path, 'Modulefile')
if File.file?(modulefile_path)
Puppet::ModuleTool::ModulefileReader.evaluate(metadata, modulefile_path)
end
puppet_module.version ||= metadata.version ||= '0.0.0'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment