Sha256: 1a99a1af198e4adbc61be8d15287a1fd061585346794901a96b3c4b35c8a230c

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

require 'fileutils'
require 'yaml'

module Chronicle
  module ETL
    # Utility methods to read, write, and access config files
    module Config
      extend self

      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(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_pathname_for_type("jobs"), "*.yml")).map do |filename|
          File.basename(filename, ".*")
        end
      end

      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 read_job(job_name)
        load('jobs', job_name)
      end

      def config_pathname
        base = Pathname.new(xdg_config.config_home)
        base.join('chronicle', 'etl')
      end

      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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chronicle-etl-0.5.1 lib/chronicle/etl/config.rb
chronicle-etl-0.5.0 lib/chronicle/etl/config.rb