Module: Xapixctl::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/xapixctl/util.rb

Defined Under Namespace

Classes: InvalidDocumentStructureError

Constant Summary collapse

DOCUMENT_STRUCTURE =
%w[version kind metadata definition].freeze

Instance Method Summary collapse

Instance Method Details

#load_files(filename, ignore_missing) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/xapixctl/util.rb', line 26

def load_files(filename, ignore_missing)
  if filename == '-'
    yield 'STDIN', $stdin.read
  else
    pn = filename.is_a?(Pathname) ? filename : Pathname.new(filename)
    if pn.directory?
      pn.glob(["**/*.yaml", "**/*.yml"]).sort.each { |dpn| yield dpn.to_s, dpn.read }
    elsif pn.exist?
      yield pn.to_s, pn.read
    elsif !ignore_missing
      warn "file not found: #{filename}"
      exit 1
    end
  end
end

#resources_from_file(filename, ignore_missing: false) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/xapixctl/util.rb', line 17

def resources_from_file(filename, ignore_missing: false)
  load_files(filename, ignore_missing) do |actual_file, yaml_string|
    yaml_string.split(/^---\s*\n/).map { |yml| Psych.safe_load(yml) }.compact.each do |doc|
      raise InvalidDocumentStructureError, actual_file unless (DOCUMENT_STRUCTURE - doc.keys.map(&:to_s)).empty?
      yield doc
    end
  end
end