class Etcdist::Reader

Reads data from file system into directories, keys and values.

Public Class Methods

new(dir) click to toggle source

@param [String] dir The path to the data directory

# File lib/etcdist/reader.rb, line 8
def initialize(dir)
  @dir = dir
end

Public Instance Methods

all_dirs() click to toggle source
# File lib/etcdist/reader.rb, line 27
def all_dirs
  @dir = Pathname.new File.expand_path(@dir)
  root = Pathname.new '/'

  descendant_dirs = Dir[File.join(@dir, '**', '*')].select { |p| File.directory? p }
  relative_descendant_dirs = descendant_dirs.map { |d| Pathname.new(d).relative_path_from @dir }
  dirs = relative_descendant_dirs.map { |d| root.join(d).to_s }

  dirs.push '/'
end
read() click to toggle source

Returns a hash of type { directory => { key => val } }

# File lib/etcdist/reader.rb, line 13
def read
  @dir = File.expand_path(@dir)
  files = Dir[File.join(@dir, '**', '*')].reject { |p| File.directory? p }
  Log.info("found #{files.length} files in #{@dir}")

  files.reduce(Hash.new { |h, k| h[k] = {} }) do |h, f|
    directory = File.dirname(f).gsub(@dir, '')
    entries = Hash[read_non_blank_lines(f).map { |e| e.chomp.split('=', 2) }.select { |k, _| valid_key?(k) }]
    Log.debug("found #{entries.length} entries in #{f.gsub(@dir, '')}: #{entries}")
    h[directory].merge!(entries)
    h
  end
end

Private Instance Methods

read_non_blank_lines(f) click to toggle source
# File lib/etcdist/reader.rb, line 40
def read_non_blank_lines(f)
  lines = IO.readlines(f)
  lines.delete_if { |line| line !~ /\S/ }
  lines
end
valid_key?(key) click to toggle source
# File lib/etcdist/reader.rb, line 46
def valid_key?(key)
  is_valid = !(key.include? '/')
  Log.warn("ignoring invalid key #{key}") unless is_valid
  is_valid
end