class ConfGen
Public Class Methods
new(path)
click to toggle source
# File lib/confgen.rb, line 10 def initialize(path) @path = path end
Public Instance Methods
choice(var)
click to toggle source
# File lib/confgen.rb, line 77 def choice(var) return choose(*var['items']) do |menu| menu.header = var['question'] if (var['default']) menu.default = var['default'] menu.prompt = "(defaults to '#{menu.default}')" end end end
gather(configSet)
click to toggle source
# File lib/confgen.rb, line 57 def gather(configSet) data = Hash.new() configSet['variables'].each do |var| data[var['name']] = self.send(var['type'], var) end return data end
generate(sets)
click to toggle source
# File lib/confgen.rb, line 14 def generate(sets) config = locate() sets.each() do |setName| configSet = config[setName] data = gather(configSet) say('Generation will continue with the following data') print(configSet, data) if (agree('Continue [y/n]?', true)) b = binding configSet['templates'].each() do |template| src = Pathname.new(ERB.new(@confPath.join(template['src']).to_path()).result(b)) dst = Pathname.new(ERB.new(@confPath.join(template['dst']).to_path()).result(b)) tpl = ERB.new(File.read(src.to_path())) if (!dst.dirname.directory?) FileUtils.mkdir_p(dst.dirname.to_path()) end File.open(dst.to_path(), 'w') do |f| f.write(tpl.result(b)) end end end end end
locate()
click to toggle source
# File lib/confgen.rb, line 42 def locate() current = Pathname.new(@path) while (!current.root?()) [current.join('variables.confgen'), current.join('confgen', 'variables.confgen')].each() do |f| if (f.file?()) @confPath = f.dirname return YAML::load(f.open()) end end current = current.dirname end end
password(var)
click to toggle source
# File lib/confgen.rb, line 91 def password(var) return ask(var['question']) { |q| q.echo = false } end
print(configSet, data)
click to toggle source
# File lib/confgen.rb, line 67 def print(configSet, data) configSet['variables'].each do |var| if (var['type'] == 'password') say(" #{var['name']}: ****") else say(" #{var['name']}: #{data[var['name']]}") end end end
string(var)
click to toggle source
# File lib/confgen.rb, line 87 def string(var) return ask(var['question']) { |q| q.default = var['default'] } end