Created
October 23, 2012 15:08
-
-
Save mlafeldt/3939296 to your computer and use it in GitHub Desktop.
Parse Cheffile in Ruby
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 | |
class CookbookSource | |
attr_reader :name, :options | |
def initialize(name, options = {}) | |
@name = name | |
@options = options | |
end | |
end | |
class Cheffile | |
def self.from_file(file) | |
content = File.read(file) | |
object = new(file) | |
object.load(content) | |
end | |
attr_reader :file, :site, :cookbooks | |
def initialize(file) | |
@file = file | |
@site = nil | |
@cookbooks = Array.new | |
end | |
def site(site) | |
@site = site | |
end | |
def cookbook(*args) | |
options = args.last.is_a?(Hash) ? args.pop : Hash.new | |
name, constraint = args | |
options[:constraint] = constraint | |
@cookbooks << CookbookSource.new(name, options) | |
end | |
def load(content) | |
instance_eval(content) | |
self | |
rescue => e | |
raise "An error occurred while reading the Cheffile: #{e}" | |
end | |
end | |
filename = ARGV.first || 'Cheffile' | |
cheffile = Cheffile.from_file(filename) | |
cheffile.cookbooks.each do |cookbook| | |
puts "#{cookbook.name.ljust(30)}#{cookbook.options.inspect}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment