pleroma-docker/config_parser/parser.rb

60 lines
1.2 KiB
Ruby
Raw Normal View History

2018-12-24 17:20:17 -05:00
#!/usr/bin/env ruby
2018-12-27 19:54:12 -05:00
#
# TODO: Write a better translator.
# This is just rough text replacement right now
# but it's already way better than the exs stuff
# we had durin the .env files.
#
2018-12-24 17:20:17 -05:00
require 'yaml'
require 'json'
2018-12-27 19:44:33 -05:00
def getval(val)
if val.is_a?(String)
val.start_with?('<D>') ? val.delete('<D>') : val.to_json()
else
val.to_json()
end
end
2018-12-24 17:20:17 -05:00
config = YAML.load_file(ARGV[0])
if config["version"] != 1
raise "Incompatible config version (#{config["version"]} != 1)"
end
buf = "use Mix.Config\n\n"
config["app"].each do |atom, content|
content.each do |sub, settings|
buf += "config :#{atom}, #{sub.is_a?(Symbol) ? ":#{sub}" : sub}"
2018-12-27 19:44:33 -05:00
if !settings.is_a?(Hash)
buf += ": #{getval(settings)}\n"
2018-12-24 17:20:17 -05:00
next
end
settings.each do |name, value|
if value.is_a?(Hash) && value["<T>"] == "Array"
value.delete("<T>")
buf += ", #{name}: ["
value.each do |k, v|
2018-12-27 19:44:33 -05:00
buf += "#{k}: #{getval(v)},"
2018-12-24 17:20:17 -05:00
end
2018-12-27 19:44:33 -05:00
buf.chop!()
2018-12-24 17:20:17 -05:00
buf += "]"
else
2018-12-27 19:44:33 -05:00
buf += ", #{name}: #{getval(value)}"
2018-12-24 17:20:17 -05:00
end
end
buf += "\n"
end
end
puts buf