Skip to content

Instantly share code, notes, and snippets.

@jmazzi
Created June 19, 2013 14:33
Show Gist options
  • Save jmazzi/5814790 to your computer and use it in GitHub Desktop.
Save jmazzi/5814790 to your computer and use it in GitHub Desktop.
require 'securerandom'
class Vhost
attr_reader :content, :vhost_line
def initialize(vhost_line, content)
@vhost_line = vhost_line
@content = content
end
def to_s
entry = "<VirtualHost #{vhost_line}>\n"
content.each do |line|
line.each do |key, value|
if key == :directive
entry << " #{value}\n"
else
entry << " #{key} #{value}\n"
end
end
end
entry << "</VirtualHost>\n\n"
end
end
apache_config = IO.readlines('httpd.conf')
vhosts = {}
in_vhost = false
key = ""
def create_key_values_from_line(content)
content = content.gsub(/^\s+/, '').strip
key, value = content.split(/\s+/)
if content.match(/^</)
{ :directive => content }
else
{ key => value }
end
end
apache_config.each do |line|
if line.match(/\s*<VirtualHost (.*?)>/i)
key = SecureRandom.uuid
vhosts[key] = Hash.new
vhosts[key][:vhost] = $1
vhosts[key][:content] = Array.new
in_vhost = true
elsif line.match(/\s*<\/VirtualHost>/)
in_vhost = false
elsif in_vhost
vhosts[key][:content] << create_key_values_from_line(line)
else
in_vhost = false
next
end
end
vhosts.each do |values|
key, vhost = values
puts Vhost.new(vhost[:vhost], vhost[:content]).to_s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment