Class: Krane::TemplateSets

Inherits:
Object
  • Object
show all
Includes:
DelayedExceptions
Defined in:
lib/krane/template_sets.rb

Constant Summary collapse

VALID_TEMPLATES =
%w(.yml.erb .yml .yaml .yaml.erb)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DelayedExceptions

#with_delayed_exceptions

Class Method Details

.from_dirs_and_files(paths:, logger:) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/krane/template_sets.rb', line 86

def from_dirs_and_files(paths:, logger:)
  resource_templates = {}
  dir_paths, file_paths = paths.partition { |path| File.directory?(path) }

  # Directory paths
  dir_paths.each do |template_dir|
    resource_templates[template_dir] = Dir.foreach(template_dir).select do |filename|
      filename.end_with?(*VALID_TEMPLATES) || filename == EjsonSecretProvisioner::EJSON_SECRETS_FILE
    end
  end

  # Filename paths
  file_paths.each do |filename|
    dir_name = File.dirname(filename)
    resource_templates[dir_name] ||= []
    resource_templates[dir_name] << File.basename(filename) unless resource_templates[dir_name].include?(filename)
  end

  template_sets = []
  resource_templates.each do |template_dir, files|
    template_sets << TemplateSet.new(template_dir: template_dir, file_whitelist: files, logger: logger)
  end
  TemplateSets.new(template_sets: template_sets)
end

Instance Method Details

#ejson_secrets_filesObject



132
133
134
# File 'lib/krane/template_sets.rb', line 132

def ejson_secrets_files
  @template_sets.map(&:ejson_secrets_file).compact
end

#validateObject



136
137
138
# File 'lib/krane/template_sets.rb', line 136

def validate
  @template_sets.flat_map(&:validate)
end

#with_resource_definitions(render_erb: false, current_sha: nil, bindings: nil, raw: false) ⇒ Object



125
126
127
128
129
130
# File 'lib/krane/template_sets.rb', line 125

def with_resource_definitions(render_erb: false, current_sha: nil, bindings: nil, raw: false)
  with_resource_definitions_and_filename(render_erb: render_erb,
    current_sha: current_sha, bindings: bindings, raw: raw) do |r_def, _|
    yield r_def
  end
end

#with_resource_definitions_and_filename(render_erb: false, current_sha: nil, bindings: nil, raw: false) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/krane/template_sets.rb', line 112

def with_resource_definitions_and_filename(render_erb: false, current_sha: nil, bindings: nil, raw: false)
  with_delayed_exceptions(@template_sets, Krane::InvalidTemplateError) do |template_set|
    template_set.with_resource_definitions_and_filename(
      render_erb: render_erb,
      current_sha: current_sha,
      bindings: bindings,
      raw: raw
    ) do |r_def, filename|
      yield r_def, filename
    end
  end
end