You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cross-post/lib/cross-post/config.rb

43 lines
873 B

require 'yaml'
class CrossPost
class Config
DEFAULT_CONFIG = File.join Dir.home, '.config/cross-post/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
def [](key)
current = @config
key.split(/\./).each do |k|
current = current[k]
return nil if current.nil?
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_})"
end
current = next_
end
current[last] = value
end
def save
File.write @file, YAML.dump(@config)
end
end
end