Skip to content

Commit d4a1c91

Browse files
authored
Merge pull request #3 from YakDriver/delete-extras
Add removal function
2 parents 30be952 + 7807644 commit d4a1c91

File tree

6 files changed

+71
-7
lines changed

6 files changed

+71
-7
lines changed

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.1.3
2+
current_version = 0.2.0
33
commit = False
44
tag = False
55
tag_name = {new_version}

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
![scratch relax tv](https://github.com/YakDriver/scratchrelaxtv/raw/master/assets/srt.gif "Extract HCL Vars")
22

3-
Terraform developer tool to extract variables and create `variables.tf` files.
3+
Terraform module development tool. Extract variables from `variables.tf` files
44

55

66
## simply
@@ -29,11 +29,31 @@ By default, it looks for `main.tf` and will keep variables in the resulting `var
2929

3030
*scratchrelaxtv* can also be used to generate a module usage stub. By default, it looks for `variables.tf` and will keep variables in the resulting `modstub.tf` in the order found in the `variables.tf`. If variables are included more than once, they will only be listed once in the resulting `modstub.tf`. If you do not `--force` overwriting, *scratchrelaxtv* will create new `modstub.tf` files with each run: `modstub.1.tf`, `modstub.2.tf` and so on.
3131

32+
### remove files
33+
34+
*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.
35+
36+
*scratchrelaxtv* removes files in the current directory _and subdirectories_.
37+
38+
| Filename | Deleted? |
39+
| -------- | ------ |
40+
| variables.tf | no |
41+
| modstub.tf | yes |
42+
| modstub.1.tf | yes |
43+
| variables.1.tf | yes |
44+
| xyz.abc | no |
45+
| variables.a.tf | no |
46+
| variables.43.tf | yes |
47+
| modstub | no |
48+
| modstub..tf | no |
49+
50+
### help
51+
3252
*scratchrelaxtv* includes help:
3353

3454
```console
3555
$ scratchrelaxtv --help
36-
usage: scratchrelaxtv [-h] [-i INPUT] [-o OUTPUT] [-f] [-m] [-n MODNAME]
56+
usage: scratchrelaxtv [-h] [-i INPUT] [-o OUTPUT] [-f] [-m] [-n MODNAME] [-r]
3757
[-a | -d]
3858

3959
optional arguments:
@@ -46,6 +66,7 @@ optional arguments:
4666
-m, --modstub create module usage stub
4767
-n MODNAME, --modname MODNAME
4868
name to use in module stub
69+
-r, --remove remove all modstub.tf and variables.x.tf files
4970
-a, --asc sort output variables in ascending order
5071
-d, --desc sort output variables in descending order
5172
```

scratchrelaxtv/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import re
1919

2020

21-
__version__ = "0.1.3"
21+
__version__ = "0.2.0"
2222
EXIT_OKAY = 0
2323
EXIT_NOT_OKAY = 1
2424

@@ -32,6 +32,14 @@ def remove_prefix(text, prefix):
3232
return text[text.startswith(prefix) and len(prefix):]
3333

3434

35+
def remove_files():
36+
"""Remove files from the os that look like scratchrelaxtv files."""
37+
pattern = r'^(variables\.\d+|modstub(\.\d+|))\.tf$'
38+
for root, _, files in os.walk(os.getcwd()):
39+
for file in filter(lambda x: re.match(pattern, x), files):
40+
os.remove(os.path.join(root, file))
41+
42+
3543
class BassExtractor():
3644
"""
3745
A class that extracts variables from Terraform HCL files to make creating

scratchrelaxtv/cli.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def parse_args(args):
3131
parser.add_argument("-m", "--modstub", default=False, action="store_true",
3232
help="create module usage stub")
3333
parser.add_argument("-n", "--modname", help="name to use in module stub")
34+
parser.add_argument("-r", "--remove", default=False, action="store_true",
35+
help="remove all modstub.tf and variables.x.tf files")
3436
group = parser.add_mutually_exclusive_group()
3537
group.add_argument("-a", "--asc", action="store_true",
3638
help="sort output variables in ascending order")
@@ -45,7 +47,9 @@ def main():
4547
args = parse_args(sys.argv[1:])
4648

4749
extractor = None
48-
if not args.modstub:
50+
if args.remove:
51+
scratchrelaxtv.remove_files()
52+
elif not args.modstub:
4953
extractor = scratchrelaxtv.VarExtractor(args)
5054
else:
5155
extractor = scratchrelaxtv.StubMaker(args)

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = scratchrelaxtv
33
description = Terraform developer tool to extract variables and create variables.tf files.
44
long_description = file: README.md, CHANGELOG.md
55
long_description_content_type = text/markdown
6-
version = 0.1.3
6+
version = 0.2.0
77
author = YakDriver
88
author_email = [email protected]
99
url = https://github.com/YakDriver/scratchrelaxtv

tests/test_scratchrelaxtv.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
import os
77
from contextlib import contextmanager
8-
from scratchrelaxtv import cli, StubMaker, VarExtractor, EXIT_OKAY
8+
from scratchrelaxtv import cli, StubMaker, VarExtractor, EXIT_OKAY, \
9+
remove_files
910

1011

1112
@contextmanager
@@ -89,3 +90,33 @@ def test_same_content_maker():
8990
second_list = file_handle.read().splitlines()
9091
assert first_list == second_list
9192
os.remove(filename)
93+
94+
95+
def test_removal():
96+
"""Test removing files."""
97+
with change_dir("tests"):
98+
tmpdir = "tmpdir"
99+
os.mkdir(tmpdir)
100+
101+
with open(os.path.join(tmpdir, "modstub.tf"), "w") as file_handle:
102+
file_handle.write("hello")
103+
104+
with open(os.path.join(tmpdir, "variables.5.tf"), "w") as file_handle:
105+
file_handle.write("hello")
106+
107+
with open(os.path.join(tmpdir, "modstub.2.tf"), "w") as file_handle:
108+
file_handle.write("hello")
109+
110+
with open(os.path.join(tmpdir, "dont_delete.tf"), "w") as file_handle:
111+
file_handle.write("hello")
112+
113+
with change_dir(tmpdir):
114+
remove_files()
115+
116+
assert not os.path.isfile(os.path.join(tmpdir, "modstub.tf"))
117+
assert not os.path.isfile(os.path.join(tmpdir, "variables.5.tf"))
118+
assert not os.path.isfile(os.path.join(tmpdir, "modstub.2.tf"))
119+
assert os.path.isfile(os.path.join(tmpdir, "dont_delete.tf"))
120+
121+
os.remove(os.path.join(tmpdir, "dont_delete.tf"))
122+
os.rmdir(tmpdir)

0 commit comments

Comments
 (0)