From 311df1fb0726ec6242da238e006c83f460c44204 Mon Sep 17 00:00:00 2001 From: Tung Nguyen Date: Tue, 23 Nov 2021 05:08:11 +0000 Subject: [PATCH] use pass strategy for binary files --- lib/terraspace/compiler/strategy/mod.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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