diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 4083682..32c4498 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.4.0 +current_version = 0.5.0 commit = False tag = False tag_name = {new_version} diff --git a/README.md b/README.md index 8d04433..d9a8d30 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ pip install scratchrelaxtv # tip -Once installed, you can just type "relaxtv." +Once installed, you can run *scratchrelaxtv* by typing either `relaxtv` or `scratchrelaxtv`. # workflows @@ -62,7 +62,7 @@ resource "aws_s3_bucket" "this" { Run *scratchrelaxtv*: ```console -$ scratchrelaxtv +$ relaxtv 2019-04-26 08:02:54,011 - INFO - generating variables file 2019-04-26 08:02:54,011 - INFO - input file: main.tf 2019-04-26 08:02:54,011 - INFO - output file: variables.tf @@ -115,7 +115,7 @@ variable "bucket" { Run *scratchrelaxtv* to automatically add any missing variables: ```console -$ scratchrelaxtv -cf +$ relaxtv -cf 2019-04-26 08:21:27,289 - INFO - checking for missing variables 2019-04-26 08:21:27,289 - INFO - input file: main.tf 2019-04-26 08:21:27,289 - INFO - output file: variables.tf @@ -167,7 +167,7 @@ variable "region" { Run *scratchrelaxtv* with the module stub option: ```console -$ scratchrelaxtv -m +$ relaxtv -m 2019-04-26 08:09:27,147 - INFO - generating module usage stub 2019-04-26 08:09:27,147 - INFO - input file: variables.tf 2019-04-26 08:09:27,147 - INFO - output file: modstub.tf @@ -204,7 +204,7 @@ resource "aws_s3_bucket" "this" { Run *scratchrelaxtv* with the generate `.env` and sort-ascending options: ```console -$ scratchrelaxtv -ea +$ relaxtv -ea 2019-06-21 20:01:35,362 - INFO - generating .env file 2019-06-21 20:01:35,362 - INFO - input file: main.tf 2019-06-21 20:01:35,362 - INFO - output file: .env @@ -222,7 +222,7 @@ TF_VAR_region=replace ## example: remove files ```console -$ scratchrelaxtv -r +$ relaxtv -r ``` *scratchrelaxtv* can also tidy up your directories by removing its own extra generated files. Presumably it will only remove files you no longer need but *be careful*. This chart shows examples of what would be deleted or not. @@ -246,7 +246,7 @@ $ scratchrelaxtv -r *scratchrelaxtv* includes help: ```console -$ scratchrelaxtv --help +$ relaxtv --help usage: scratchrelaxtv [-h] [-i INPUT] [-o OUTPUT] [-f] [-m] [-n MODNAME] [-r] [-c] [-e] [-a | -d] diff --git a/scratchrelaxtv/__init__.py b/scratchrelaxtv/__init__.py index df9214b..ecb11b1 100644 --- a/scratchrelaxtv/__init__.py +++ b/scratchrelaxtv/__init__.py @@ -20,7 +20,7 @@ import re -__version__ = "0.4.0" +__version__ = "0.5.0" EXIT_OKAY = 0 EXIT_NOT_OKAY = 1 @@ -72,6 +72,8 @@ def log_arguments(self): logger.info("checking for missing variables") elif self.args.env: logger.info("generating .env file") + elif self.args.tfvars: + logger.info("generating .tfvars file") else: logger.info("generating variables file") @@ -249,14 +251,16 @@ def __init__(self, args): if not args.input: args.input = "main.tf" - if not args.output: + if not args.output and args.env: args.output = ".env" + if not args.output and args.tfvars: + args.output = "terraform.tfvars" + super().__init__(args) - def write_file(self): + def _write_env(self): """Output vars to .env file.""" - self._find_non_existing_filename() with open(self.args.output, "w", encoding='utf_8') as file_handle: file_handle.write('unset "${!TF_VAR_@}"\n') for tf_var in self.tf_vars: @@ -265,6 +269,22 @@ def write_file(self): tf_var, "=replace\n"])) + def _write_tfvars(self): + """Output vars to .tfvars file.""" + with open(self.args.output, "w", encoding='utf_8') as file_handle: + for tf_var in self.tf_vars: + file_handle.write("".join([ + tf_var, + ' = "replace"\n'])) + + def write_file(self): + """Output vars to file.""" + self._find_non_existing_filename() + if self.args.env: + self._write_env() + elif self.args.tfvars: + self._write_tfvars() + def extract(self): """Extract vars from .tf file.""" self.find_vars_in_file(self.main_phrase_find, self.main_token_find) diff --git a/scratchrelaxtv/cli.py b/scratchrelaxtv/cli.py index 549b8bd..8edec79 100644 --- a/scratchrelaxtv/cli.py +++ b/scratchrelaxtv/cli.py @@ -39,6 +39,8 @@ def parse_args(args): help="check that all vars are listed") task.add_argument("-e", "--env", default=False, action="store_true", help="generate .env with Terraform vars") + task.add_argument("-t", "--tfvars", default=False, action="store_true", + help="generate .tfvars with Terraform vars") sort_order = parser.add_mutually_exclusive_group() sort_order.add_argument("-a", "--asc", action="store_true", @@ -61,7 +63,7 @@ def main(): extractor = scratchrelaxtv.StubMaker(args) elif args.check: extractor = scratchrelaxtv.Checker(args) - elif args.env: + elif args.env or args.tfvars: extractor = scratchrelaxtv.EnvGenerator(args) else: extractor = scratchrelaxtv.VarExtractor(args) diff --git a/setup.cfg b/setup.cfg index b51dd33..b295658 100644 --- a/setup.cfg +++ b/setup.cfg @@ -3,7 +3,7 @@ name = scratchrelaxtv description = Terraform developer tool to extract variables and generate variables.tf files. long_description = file: README.md, CHANGELOG.md long_description_content_type = text/markdown -version = 0.4.0 +version = 0.5.0 author = YakDriver author_email = projects@plus3it.com url = https://github.com/YakDriver/scratchrelaxtv diff --git a/test.tf b/test.tf deleted file mode 100644 index e69de29..0000000 diff --git a/tests/terraform.1.tfvars b/tests/terraform.1.tfvars new file mode 100644 index 0000000..478e92c --- /dev/null +++ b/tests/terraform.1.tfvars @@ -0,0 +1,10 @@ +acl = "replace" +bucket = "replace" +create_2 = "replace" +create_keystore_bucket = "replace" +org_ids = "replace" +prefix = "replace" +problem_var = "replace" +region = "replace" +tags = "replace" +versioning = "replace" diff --git a/tests/test_scratchrelaxtv.py b/tests/test_scratchrelaxtv.py index 88db923..4ceae7c 100644 --- a/tests/test_scratchrelaxtv.py +++ b/tests/test_scratchrelaxtv.py @@ -160,3 +160,23 @@ def test_gen_env(): second_list = file_handle.read().splitlines() assert first_list == second_list os.remove(filename) + + +def test_gen_tfvars(): + """Test extracting variables.""" + with change_dir("tests"): + filename = "terraform.2.tfvars" + if os.path.isfile(filename): + os.remove(filename) + + args = cli.parse_args(["-fta", "-o", filename]) + + extractor = EnvGenerator(args) + + assert extractor.extract() == EXIT_OKAY + with open("terraform.1.tfvars", "r", encoding='utf_8') as file_handle: + first_list = file_handle.read().splitlines() + with open(filename, "r", encoding='utf_8') as file_handle: + second_list = file_handle.read().splitlines() + assert first_list == second_list + os.remove(filename)