Skip to content

Commit 5ac9ac3

Browse files
Merge pull request #33 from amplify-education/drop_docopt
Drop docopt
2 parents 44a4427 + 4407bdd commit 5ac9ac3

File tree

5 files changed

+46
-37
lines changed

5 files changed

+46
-37
lines changed

bin/hcl2tojson

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,47 @@ Options:
1010
PATH The path to convert
1111
OUT_PATH The path to write files to
1212
"""
13+
import argparse
1314
import json
14-
import sys
1515
import os
16-
17-
from docopt import docopt
16+
import sys
1817

1918
from hcl2 import load
2019
from hcl2.parser import hcl2
2120
from hcl2.version import __version__
2221
from lark import UnexpectedToken
2322

2423
if __name__ == '__main__':
25-
arguments = docopt(__doc__, version=__version__)
26-
skip = arguments["-s"]
27-
in_path = arguments["PATH"]
28-
out_path = arguments["OUT_PATH"]
29-
if os.path.isfile(in_path):
30-
with open(in_path, 'r') as in_file:
31-
out_file = sys.stdout if out_path is None else open(out_path, 'w')
32-
print(in_path, file=sys.stderr, flush=True)
24+
parser = argparse.ArgumentParser(description='This script recursively converts hcl2 files to json')
25+
parser.add_argument('-s', dest='skip', action='store_true', help='Skip un-parsable files')
26+
parser.add_argument('PATH', help='The file or directory to convert')
27+
parser.add_argument(
28+
'OUT_PATH',
29+
nargs='?',
30+
help='The path where to write files to. Optional when parsing a single file. '
31+
'Output is printed to stdout if OUT_PATH is blank'
32+
)
33+
parser.add_argument('--version', action='version', version=__version__)
34+
35+
args = parser.parse_args()
36+
if os.path.isfile(args.PATH):
37+
with open(args.PATH, 'r') as in_file:
38+
out_file = sys.stdout if args.OUT_PATH is None else open(args.OUT_PATH, 'w')
39+
print(args.PATH, file=sys.stderr, flush=True)
3340
json.dump(hcl2.parse(in_file.read()), out_file)
34-
if out_path is None:
41+
if args.OUT_PATH is None:
3542
out_file.write('\n')
3643
out_file.close()
37-
elif os.path.isdir(in_path):
44+
elif os.path.isdir(args.PATH):
3845
processed_files = set()
39-
if out_path is None:
46+
if args.OUT_PATH is None:
4047
raise RuntimeError("Positional OUT_PATH parameter shouldn't be empty")
41-
if not os.path.exists(out_path):
42-
os.mkdir(out_path)
43-
for current_dir, dirs, files in os.walk(in_path):
44-
dir_prefix = os.path.commonpath([in_path, current_dir])
48+
if not os.path.exists(args.OUT_PATH):
49+
os.mkdir(args.OUT_PATH)
50+
for current_dir, dirs, files in os.walk(args.PATH):
51+
dir_prefix = os.path.commonpath([args.PATH, current_dir])
4552
relative_current_dir = os.path.relpath(current_dir, dir_prefix)
46-
current_out_path = os.path.normpath(os.path.join(out_path, relative_current_dir))
53+
current_out_path = os.path.normpath(os.path.join(args.OUT_PATH, relative_current_dir))
4754
if not os.path.exists(current_out_path):
4855
os.mkdir(current_out_path)
4956
for file_name in files:
@@ -63,10 +70,10 @@ if __name__ == '__main__':
6370
try:
6471
parsed_data = load(in_file)
6572
except UnexpectedToken:
66-
if skip:
73+
if args.skip:
6774
continue
6875
raise
6976
with open(out_file_path, 'w') as out_file:
7077
json.dump(parsed_data, out_file)
7178
else:
72-
raise RuntimeError('Invalid Path %s', in_path)
79+
raise RuntimeError('Invalid Path %s', args.PATH)

bin/terraform_test

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,24 @@ Options:
99
PATH The directory to check. Defaults to the TERRAFORM_CONFIG environment variable
1010
1111
"""
12+
import argparse
1213
import os
1314

14-
from docopt import docopt
15-
1615
from hcl2 import load
17-
from hcl2.parser import hcl2
1816
from hcl2.version import __version__
1917

2018
if __name__ == '__main__':
21-
arguments = docopt(__doc__, version=__version__)
22-
target_dir = arguments["PATH"] if arguments["PATH"] else os.environ['TERRAFORM_CONFIG']
23-
for current_dir, dirs, files in os.walk(target_dir):
19+
parser = argparse.ArgumentParser(description='This script recursively converts hcl2 files to json')
20+
parser.add_argument('PATH', nargs='?', default=None, help='The path to convert')
21+
parser.add_argument('--version', action='version', version=__version__)
22+
23+
args = parser.parse_args()
24+
25+
target_dir = args.PATH if args.PATH else os.environ['TERRAFORM_CONFIG']
26+
for curr_dir, dirs, files in os.walk(target_dir):
2427
for file_name in files:
25-
if ".terraform" not in current_dir and (file_name.endswith(".tf") or file_name.endswith('tfvars')):
26-
file_path = os.path.join(current_dir, file_name)
28+
if ".terraform" not in curr_dir and (file_name.endswith(".tf") or file_name.endswith('tfvars')):
29+
file_path = os.path.join(curr_dir, file_name)
2730

2831
with open(file_path, 'r') as file:
2932
print(file_path)

hcl2/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
"""Place of record for the package version"""
22

3-
__version__ = "0.2.6"
3+
__version__ = "0.3.0"
44
__git_hash__ = "GIT_HASH"

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

requirements.pip

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
# Place dependencies in this file, following the distutils format:
22
# http://docs.python.org/2/distutils/setupscript.html#relationships-between-distributions-and-packages
3-
docopt==0.6.2
43
lark-parser>=0.7.8,<0.8.0

0 commit comments

Comments
 (0)