Skip to content

Instantly share code, notes, and snippets.

@mlafeldt
Created October 23, 2012 15:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlafeldt/3939296 to your computer and use it in GitHub Desktop.
Save mlafeldt/3939296 to your computer and use it in GitHub Desktop.
Parse Cheffile in Ruby
#!/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