Class: Tflat::Terraform
- Inherits:
-
Object
- Object
- Tflat::Terraform
- Defined in:
- lib/tflat.rb
Instance Attribute Summary collapse
-
#all_files ⇒ Object
Returns the value of attribute all_files.
-
#args ⇒ Object
Returns the value of attribute args.
-
#destination ⇒ Object
Returns the value of attribute destination.
-
#directory ⇒ Object
Returns the value of attribute directory.
Instance Method Summary collapse
- #execute ⇒ Object
- #f(file) ⇒ Object
- #file(file) ⇒ Object
- #file_sha256(ff) ⇒ Object
- #flatten_directories ⇒ Object
-
#initialize(args:, directory: '.') ⇒ Terraform
constructor
A new instance of Terraform.
- #parse_erb ⇒ Object
- #read_variables ⇒ Object
- #render(file) ⇒ Object
- #run! ⇒ Object
- #setup ⇒ Object
Constructor Details
#initialize(args:, directory: '.') ⇒ Terraform
Returns a new instance of Terraform.
11 12 13 14 15 16 |
# File 'lib/tflat.rb', line 11 def initialize(args:, directory: '.') @args = args.join(' ') @directory = directory @all_files = Dir.glob("#{directory}/**/*").select{|e| File.file?(e) && !e.match(/^\.tflat\/.+$/)} @destination = "#{directory}/.tflat" end |
Instance Attribute Details
#all_files ⇒ Object
Returns the value of attribute all_files.
10 11 12 |
# File 'lib/tflat.rb', line 10 def all_files @all_files end |
#args ⇒ Object
Returns the value of attribute args.
10 11 12 |
# File 'lib/tflat.rb', line 10 def args @args end |
#destination ⇒ Object
Returns the value of attribute destination.
10 11 12 |
# File 'lib/tflat.rb', line 10 def destination @destination end |
#directory ⇒ Object
Returns the value of attribute directory.
10 11 12 |
# File 'lib/tflat.rb', line 10 def directory @directory end |
Instance Method Details
#execute ⇒ Object
92 93 94 |
# File 'lib/tflat.rb', line 92 def execute exec "cd .tflat && terraform #{args}" end |
#f(file) ⇒ Object
74 75 76 |
# File 'lib/tflat.rb', line 74 def f(file) file.sub(/^\.\//, "").gsub('/', '#') end |
#file(file) ⇒ Object
78 79 80 |
# File 'lib/tflat.rb', line 78 def file(file) render(file).inspect[1...-1] end |
#file_sha256(ff) ⇒ Object
82 83 84 85 |
# File 'lib/tflat.rb', line 82 def file_sha256(ff) f = file(ff) Digest::SHA256.hexdigest(f) end |
#flatten_directories ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/tflat.rb', line 46 def flatten_directories all_files.each do |entry| next if entry =~ /#/ new_name = entry.sub(/^#{directory}\//,'').gsub('/', '#') if new_name =~ /^#/ # Skip files/directories that start with a hash sign puts "- [tflat] Skipping: #{entry}" next end FileUtils.cp(entry, ".tflat/#{new_name}") end end |
#parse_erb ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/tflat.rb', line 58 def parse_erb Dir.glob(".tflat/*").each do |entry| next unless File.file?(entry) next if File.binary?(entry) puts "- [tflat] -> #{entry}" begin rendered = render(entry) rescue Exception => e puts "- [tflat] ERROR: Could not parse ERB on file #{entry}" puts e. exit 1 end File.write(entry, rendered) end end |
#read_variables ⇒ Object
41 42 43 44 |
# File 'lib/tflat.rb', line 41 def read_variables return unless File.file?('terraform.tfvars.json') @variables = JSON.parse(File.read 'terraform.tfvars.json') end |
#render(file) ⇒ Object
87 88 89 90 |
# File 'lib/tflat.rb', line 87 def render(file) template = File.read(file) ERB.new(template).result(binding) end |
#run! ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/tflat.rb', line 17 def run! if args.empty? puts `terraform` puts "\n\n- [tflat] Notice: You must run tflat with terraform arguments.\n\n" return end setup read_variables puts "- [tflat] Generating files" flatten_directories parse_erb puts " done!" puts "- [tflat] Running: terraform #{args}" execute end |
#setup ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/tflat.rb', line 33 def setup FileUtils.mkdir_p(destination) Dir.glob('.tflat/*').each do |entry| next unless File.file?(entry) FileUtils.rm_f entry end end |