Skip to content

Commit

Permalink
Merge pull request #16 from YakDriver/tfvars
Browse files Browse the repository at this point in the history
Tfvars
  • Loading branch information
YakDriver authored Jul 3, 2019
2 parents a004172 + f5dfb70 commit 23e6739
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.4.0
current_version = 0.5.0
commit = False
tag = False
tag_name = {new_version}
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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]

Expand Down
28 changes: 24 additions & 4 deletions scratchrelaxtv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import re


__version__ = "0.4.0"
__version__ = "0.5.0"
EXIT_OKAY = 0
EXIT_NOT_OKAY = 1

Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion scratchrelaxtv/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [email protected]
url = https://github.com/YakDriver/scratchrelaxtv
Expand Down
Empty file removed test.tf
Empty file.
10 changes: 10 additions & 0 deletions tests/terraform.1.tfvars
Original file line number Diff line number Diff line change
@@ -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"
20 changes: 20 additions & 0 deletions tests/test_scratchrelaxtv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 23e6739

Please sign in to comment.