@@ -76,64 +76,43 @@ def find_templates_in_git_repo
76
76
template_dirs = { }
77
77
78
78
# checkout repo, for sending files to terraform-runner to parse for input/ouput vars.
79
- git_checkout_tempdir = checkout_git_repo
80
-
81
- # traverse through files in git- worktree
82
- git_repository . with_worktree do | worktree |
83
- worktree . ref = scm_branch
84
-
85
- # Find all dir's with .tf/.tf.json files
86
- worktree . blob_list
87
- . group_by { |file | File . dirname ( file ) }
88
- . select { |_dir , files | files . any? { |f | f . end_with? ( ".tf" , ".tf.json" ) } }
89
- . transform_values! { | files | files . map { | f | File . basename ( f ) } }
90
- . each do | relative_path , files |
91
- name = self . class . template_name_from_git_repo_url ( git_repository . url , relative_path )
92
-
93
- template_full_path = File . join ( git_checkout_tempdir , relative_path )
94
-
95
- input_vars , output_vars , terraform_version = parse_vars_in_template ( template_full_path )
96
-
97
- template_dirs [ name ] = {
98
- :relative_path => relative_path ,
99
- :files => files ,
100
- :input_vars => input_vars ,
101
- :output_vars => output_vars ,
102
- :terraform_version => terraform_version ,
103
- }
79
+ checkout_git_repository do | git_checkout_tempdir |
80
+ # traverse through files in git-worktree
81
+ git_repository . with_worktree do | worktree |
82
+ worktree . ref = scm_branch
83
+
84
+ # Find all dir's with .tf/.tf.json files
85
+ worktree . blob_list
86
+ . group_by { | file | File . dirname ( file ) }
87
+ . select { |_dir , files | files . any? { | f | f . end_with? ( ".tf" , ".tf.json" ) } }
88
+ . transform_values! { |files | files . map { |f | File . basename ( f ) } }
89
+ . each do | relative_path , files |
90
+ name = self . class . template_name_from_git_repo_url ( git_repository . url , relative_path )
91
+
92
+ template_full_path = File . join ( git_checkout_tempdir , relative_path )
93
+
94
+ input_vars , output_vars , terraform_version = parse_vars_in_template ( template_full_path )
95
+
96
+ template_dirs [ name ] = {
97
+ :relative_path => relative_path ,
98
+ :files => files ,
99
+ :input_vars => input_vars ,
100
+ :output_vars => output_vars ,
101
+ :terraform_version => terraform_version ,
102
+ }
103
+ end
104
104
end
105
105
end
106
106
107
107
template_dirs
108
108
rescue => error
109
109
_log . error ( "Failing scaning for terraform templates in the git repo: #{ error } " )
110
110
raise
111
- ensure
112
- cleanup_git_repo ( git_checkout_tempdir )
113
111
end
114
112
115
113
# Parse template and return input-vars, output-vars & terraform-version
116
114
def parse_vars_in_template ( template_path )
117
115
response = Terraform ::Runner . parse_template_variables ( template_path )
118
116
return response [ 'template_input_params' ] , response [ 'template_output_params' ] , response [ 'terraform_version' ]
119
117
end
120
-
121
- # checkout git repo to temp dir
122
- def checkout_git_repo
123
- git_checkout_tempdir = Dir . mktmpdir ( "embedded-terraform-runner-git" )
124
-
125
- _log . debug ( "Checking out git repository to #{ git_checkout_tempdir } ..." )
126
- checkout_git_repository ( git_checkout_tempdir )
127
- git_checkout_tempdir
128
- end
129
-
130
- # clean temp dir
131
- def cleanup_git_repo ( git_checkout_tempdir )
132
- return if git_checkout_tempdir . nil?
133
-
134
- _log . debug ( "Cleaning up git repository checked out at #{ git_checkout_tempdir } ..." )
135
- FileUtils . rm_rf ( git_checkout_tempdir )
136
- rescue Errno ::ENOENT
137
- nil
138
- end
139
118
end
0 commit comments