lib/chronicle/etl/config.rb in chronicle-etl-0.4.4 vs lib/chronicle/etl/config.rb in chronicle-etl-0.5.0

- old
+ new

@@ -1,56 +1,68 @@ -require 'runcom' +require 'fileutils' +require 'yaml' module Chronicle module ETL # Utility methods to read, write, and access config files module Config - module_function + extend self - # Loads a yml config file - def load(path) - config = Runcom::Config.new(path) - # FIXME: hack to deeply symbolize keys - JSON.parse(config.to_h.to_json, symbolize_names: true) + attr_accessor :xdg_environment + + def load(type, identifier) + base = config_pathname_for_type(type) + path = base.join("#{identifier}.yml") + return {} unless path.exist? + + YAML.safe_load(File.read(path), symbolize_names: true, permitted_classes: [Symbol, Date, Time]) end # Writes a hash as a yml config file - def write(path, data) - config = Runcom::Config.new(path) - filename = config.all[0].to_s + '.yml' - File.open(filename, 'w') do |f| - f << data.to_yaml + def write(type, identifier, data) + base = config_pathname_for_type(type) + path = base.join("#{identifier}.yml") + FileUtils.mkdir_p(File.dirname(path)) + File.open(path, 'w', 0o600) do |f| + # Ruby likes to add --- separators when writing yaml files + f << data.to_yaml.gsub(/^-+\n/, '') end end # Returns all jobs available in ~/.config/chronicle/etl/jobs/*.yml def available_jobs - Dir.glob(File.join(config_directory("jobs"), "*.yml")).map do |filename| + Dir.glob(File.join(config_pathname_for_type("jobs"), "*.yml")).map do |filename| File.basename(filename, ".*") end end - # Returns all available credentials available in ~/.config/chronicle/etl/credentials/*.yml - def available_credentials - Dir.glob(File.join(config_directory("credentials"), "*.yml")).map do |filename| + def available_configs(type) + Dir.glob(File.join(config_pathname_for_type(type), "*.yml")).map do |filename| File.basename(filename, ".*") end end # Load a job definition from job config directory - def load_job_from_config(job_name) - definition = self.load("chronicle/etl/jobs/#{job_name}.yml") - definition[:name] = job_name - definition + def read_job(job_name) + load('jobs', job_name) end - def load_credentials(name) - config = self.load("chronicle/etl/credentials/#{name}.yml") + def config_pathname + base = Pathname.new(xdg_config.config_home) + base.join('chronicle', 'etl') end - def config_directory(type) - path = "chronicle/etl/#{type}" - Runcom::Config.new(path).current || raise(Chronicle::ETL::ConfigError, "Could not access config directory (#{path})") + def config_pathname_for_type(type) + config_pathname.join(type) + end + + def xdg_config + # Only used for overriding ENV['HOME'] for XDG-related specs + if @xdg_environment + XDG::Environment.new(environment: @xdg_environment) + else + XDG::Environment.new + end end end end end