diff --git a/lib/terraspace/compiler/strategy/mod.rb b/lib/terraspace/compiler/strategy/mod.rb index a0fbf616..5c442b8f 100644 --- a/lib/terraspace/compiler/strategy/mod.rb +++ b/lib/terraspace/compiler/strategy/mod.rb @@ -1,3 +1,5 @@ +require "open3" + module Terraspace::Compiler::Strategy class Mod < AbstractBase def run @@ -9,9 +11,15 @@ def run def strategy_class(path) ext = File.extname(path).sub('.','') return Mod::Pass if ext.empty? # infinite loop without this - return Mod::Pass if Terraspace.pass_file?(path) + return Mod::Pass if Terraspace.pass_file?(path) or !text_file?(path) # Fallback to Mod::Tf for all other files. ERB useful for terraform.tfvars "Terraspace::Compiler::Strategy::Mod::#{ext.camelize}".constantize rescue Mod::Tf end + + # Thanks: https://stackoverflow.com/questions/2355866/ruby-how-to-determine-if-file-being-read-is-binary-or-text + def text_file?(filename) + file_type, status = Open3.capture2e("file", filename) + status.success? && file_type.include?("text") + end end end