@@ -10,40 +10,47 @@ Options:
1010 PATH The path to convert
1111 OUT_PATH The path to write files to
1212"""
13+ import argparse
1314import json
14- import sys
1515import os
16-
17- from docopt import docopt
16+ import sys
1817
1918from hcl2 import load
2019from hcl2 .parser import hcl2
2120from hcl2 .version import __version__
2221from lark import UnexpectedToken
2322
2423if __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 )
0 commit comments