|
|
|
@ -2,42 +2,80 @@ require 'yaml' |
|
|
|
|
|
|
|
|
|
class CrossPost |
|
|
|
|
class Config |
|
|
|
|
DEFAULT_CONFIG = File.join Dir.home, '.config/cross-post/config.yml' |
|
|
|
|
DEFAULT_CONFIG_FOLDER = File.join Dir.home, '.config/cross-post' |
|
|
|
|
DEFAULT_CONFIG_FILE = 'config.yml' |
|
|
|
|
|
|
|
|
|
def initialize |
|
|
|
|
@file = ENV.fetch 'CROSS_POST_CONFIG', DEFAULT_CONFIG |
|
|
|
|
raise 'Unable to find config file' unless File.readable? @file |
|
|
|
|
File.open(@file) { |f| @config = YAML.safe_load f } |
|
|
|
|
end |
|
|
|
|
class SubConfig |
|
|
|
|
def initialize(file) |
|
|
|
|
@file = file |
|
|
|
|
@config = if File.readable? @file |
|
|
|
|
File.open(@file) { |f| YAML.safe_load f } |
|
|
|
|
else |
|
|
|
|
{} |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def [](key) |
|
|
|
|
current = @config |
|
|
|
|
key.split(/\./).each do |k| |
|
|
|
|
current = current[k] |
|
|
|
|
return nil if current.nil? |
|
|
|
|
end |
|
|
|
|
current |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def [](key) |
|
|
|
|
current = @config |
|
|
|
|
key.split(/\./).each do |k| |
|
|
|
|
current = current[k] |
|
|
|
|
return nil if current.nil? |
|
|
|
|
def fetch(key, default = nil) |
|
|
|
|
self[key] || default |
|
|
|
|
end |
|
|
|
|
current |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def []=(key, value) |
|
|
|
|
*key, last = key.split(/\./) |
|
|
|
|
current = @config |
|
|
|
|
key.each do |k| |
|
|
|
|
next_ = current[k] |
|
|
|
|
case next_ |
|
|
|
|
when nil |
|
|
|
|
next_ = current[k] = {} |
|
|
|
|
when Hash |
|
|
|
|
else |
|
|
|
|
raise "Invalid entry, Hash expected, had #{next_.class} (#{next_})" |
|
|
|
|
def []=(key, value) |
|
|
|
|
*key, last = key.split(/\./) |
|
|
|
|
current = @config |
|
|
|
|
key.each do |k| |
|
|
|
|
next_ = current[k] |
|
|
|
|
case next_ |
|
|
|
|
when nil |
|
|
|
|
next_ = current[k] = {} |
|
|
|
|
when Hash |
|
|
|
|
else |
|
|
|
|
raise "Invalid entry, Hash expected, had #{next_.class} (#{next_})" |
|
|
|
|
end |
|
|
|
|
current = next_ |
|
|
|
|
end |
|
|
|
|
current = next_ |
|
|
|
|
current[last] = value |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def put(key, value, save: false) |
|
|
|
|
self[key] = value |
|
|
|
|
self.save if save |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def save |
|
|
|
|
LOGGER.debug "Saving #{@file}" |
|
|
|
|
yaml = YAML.dump @config |
|
|
|
|
File.write @file, yaml |
|
|
|
|
end |
|
|
|
|
current[last] = value |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def save |
|
|
|
|
File.write @file, YAML.dump(@config) |
|
|
|
|
def initialize |
|
|
|
|
@config = {} |
|
|
|
|
@dir = ENV.fetch 'CONFIG_FOLDER', DEFAULT_CONFIG_FOLDER |
|
|
|
|
file = ENV.fetch 'CONFIG_FILE', DEFAULT_CONFIG_FILE |
|
|
|
|
self.load :settings, file |
|
|
|
|
self.load :posts |
|
|
|
|
self.load :users |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def [](name) |
|
|
|
|
settings = @config[name] |
|
|
|
|
return settings if settings |
|
|
|
|
self.load name |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def load(name, file = nil) |
|
|
|
|
file ||= "#{name}.yml" |
|
|
|
|
file = File.join @dir, file |
|
|
|
|
@config[name] = SubConfig.new file |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|