Class: Consult::Template

Inherits:
Object
  • Object
show all
Includes:
TemplateFunctions, Utilities
Defined in:
lib/consult/template.rb

Constant Summary collapse

LOCATIONS =
%i[path paths consul_key consul_keys]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TemplateFunctions

#indent, #key, #query, #query_nodes, #secret, #secrets, #service, #timestamp, #with

Methods included from Utilities

#resolve

Constructor Details

#initialize(name, config) ⇒ Template

Returns a new instance of Template.



16
17
18
19
# File 'lib/consult/template.rb', line 16

def initialize(name, config)
  @name = name
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



14
15
16
# File 'lib/consult/template.rb', line 14

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/consult/template.rb', line 14

def name
  @name
end

Instance Method Details

#destObject



61
62
63
# File 'lib/consult/template.rb', line 61

def dest
  resolve @config.fetch(:dest)
end

#expired?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
# File 'lib/consult/template.rb', line 69

def expired?
  # Treat renders as expired if a TTL isn't set, or it has never been rendered before
  return true if !config.key?(:ttl) || !dest.exist?
  dest.mtime < (Time.now - @config[:ttl].to_i)
end

#ordered_locationsObject



79
80
81
# File 'lib/consult/template.rb', line 79

def ordered_locations
  @config.keys & LOCATIONS
end

#pathObject



49
50
51
# File 'lib/consult/template.rb', line 49

def path
  resolve @config[:path]
end

#pathsObject



53
54
55
# File 'lib/consult/template.rb', line 53

def paths
  @config.fetch(:paths, []).map { |path| resolve(path) }
end

#render(save: true) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/consult/template.rb', line 21

def render(save: true)
  if contents.empty? && @config[:skip_missing_template]
    return
  end

  # Attempt to render
  renderer = if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.0')
    ERB.new(contents, trim_mode: '-')
  else
    ERB.new(contents, nil, '-')
  end
  result = renderer.result(binding)

  puts "Consult: Rendering #{name}" + (save ? " to #{dest}" : "...") if verbose?

  if save
    FileUtils.mkdir_p(dest.dirname) unless dest.dirname.exist?
    File.open(dest, 'wb') { |f| f << result }
  end

  result
rescue StandardError => e
  STDERR.puts "Error rendering template: #{name}"
  STDERR.puts e
  STDERR.puts e.backtrace if verbose?
  nil
end

#should_render?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/consult/template.rb', line 65

def should_render?
  expired?
end

#varsObject



57
58
59
# File 'lib/consult/template.rb', line 57

def vars
  @config[:env_vars].to_h.deep_merge @config[:vars].to_h
end

#verbose?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/consult/template.rb', line 75

def verbose?
  @config[:verbose]
end