Last active
October 29, 2018 10:39
-
-
Save danihodovic/10a1bc58ccaed5485fea60e4ff0930e2 to your computer and use it in GitHub Desktop.
Terraform vars generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import re | |
import sys | |
import os | |
usage = f''' | |
Prints all terraform variables in a directory. Used to generate a variables.tf | |
file based on variables used in resource files (var.foo, var.bar). | |
Usage: | |
${sys.argv[0]} <directory> | |
''' | |
if len(sys.argv) != 2: | |
print(usage) | |
sys.exit(1) | |
directory = sys.argv[1] | |
full_path = os.path.join(os.getcwd(), directory) | |
files = os.listdir(full_path) | |
def create_tf_var(var_name): | |
return f''' | |
variable "{var_name}" {{ | |
type = "string" | |
}} | |
''''' | |
def main(): | |
already_printed = set() | |
for filename in files: | |
filepath = os.path.join(full_path, filename) | |
with open(filepath, 'r') as f: | |
for line in f: | |
m = re.search(r'var\.(\w+)', line) | |
if m: | |
var_name = m.groups()[0] | |
if var_name not in already_printed: | |
print(create_tf_var(var_name)) | |
already_printed.add(var_name) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice one.