Created
January 10, 2019 16:28
-
-
Save johanek/cda7e2059c5aa123051aae0e64f72386 to your computer and use it in GitHub Desktop.
dependencies from puppetfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'pry' | |
require 'puppet_forge' | |
PuppetForge.user_agent = 'puppetfile/0.9.0' | |
Module_Regex = Regexp.new("mod ['\"]([a-z0-9_]+\/[a-z0-9_]+)['\"](, ['\"](\\d\.\\d\.\\d)['\"])?", Regexp::IGNORECASE) | |
def cleanname(name) | |
name.gsub(/\//, '-') | |
end | |
def read_puppetfile(puppetfile) | |
puppetfile_contents = { | |
modules: [] | |
} | |
File.foreach(puppetfile) do |line| | |
if Module_Regex.match(line) | |
name = cleanname(Regexp.last_match(1)) | |
version = Regexp.last_match(3) | |
puppetfile_contents[:modules].push({name: name, version: version, parents: [ { name: 'Puppetfile', version: version } ] }) | |
end | |
end | |
puppetfile_contents | |
end | |
puppetfile = read_puppetfile('Puppetfile') | |
modules = puppetfile[:modules] | |
modules.each do |mod| | |
modname = cleanname(mod[:name]) | |
modversion = mod[:version] | |
puts "Processing #{modname} #{modversion}" | |
begin | |
forgemod = PuppetForge::Module.find(modname) | |
if modversion.nil? | |
release = forgemod.releases.first | |
else | |
release = forgemod.releases.select { |r| r.version == modversion }.first | |
end | |
release.metadata[:dependencies].each do |dep| | |
name = cleanname(dep[:name]) | |
puts "dep #{name}" | |
# Need to get the correct version here | |
modules.push({name: name, version: nil, parents: []}) unless modules.select { |m| m[:name] == name }.length > 0 | |
modules.select { |m| m[:name] == name }.map { |m| m[:parents] << { name: modname, version: dep[:version_requirement] } } | |
end | |
rescue Faraday::ResourceNotFound | |
puts "#{modname} not found on the forge!" | |
end | |
end | |
modules.each do |mod| | |
puts "#{mod[:name]} has the following version requirements :" | |
mod[:parents].each do |parent| | |
puts " - #{parent[:version]} from #{parent[:name]}" if parent[:name] == 'Puppetfile' or parent.has_key?(:version) | |
end | |
puts "" | |
end | |
binding.pry | |
# This works, but we need to get the correct version of modules resolved, rather than just the latest... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment