Skip to content

Commit 54dc58d

Browse files
committed
Add cli tool
1 parent 6dd1984 commit 54dc58d

File tree

3 files changed

+91
-53
lines changed

3 files changed

+91
-53
lines changed

hierarchical_yaml/config_generator.py

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,58 @@
1818
from .remote_state import S3TerraformRemoteStateRetriever
1919

2020

21+
class ConfigProcessor(object):
22+
23+
def process(self, cwd=None, path=None, filters=(), exclude_keys=(), enclosing_key=None, output_format=yaml, print_data=False,
24+
output_file=None, skip_interpolations=False, skip_interpolation_validation=False):
25+
26+
path = self.get_relative_path(path)
27+
28+
if skip_interpolations:
29+
skip_interpolation_validation = True
30+
31+
if cwd is None:
32+
cwd = os.getcwd()
33+
34+
generator = ConfigGenerator(cwd, path)
35+
generator.generate_hierarchy()
36+
generator.process_hierarchy()
37+
38+
if not skip_interpolations:
39+
generator.resolve_interpolations()
40+
generator.add_dynamic_data()
41+
generator.resolve_interpolations()
42+
43+
if len(filters) > 0:
44+
generator.filter_data(filters)
45+
46+
if len(exclude_keys) > 0:
47+
generator.exclude_keys(exclude_keys)
48+
49+
if not skip_interpolation_validation:
50+
generator.validate_interpolations()
51+
52+
data = generator.add_enclosing_key(enclosing_key) if enclosing_key else generator.generated_data
53+
54+
formatted_data = generator.output_data(data, output_format)
55+
56+
if print_data:
57+
print(formatted_data)
58+
59+
if output_file:
60+
with open(output_file, 'w') as f:
61+
f.write(formatted_data)
62+
63+
return data
64+
65+
@staticmethod
66+
def get_relative_path(self, path):
67+
cwd = os.path.join(os.getcwd(), '')
68+
if path.startswith(cwd):
69+
return path[len(cwd):]
70+
return path
71+
72+
2173
class ConfigGenerator(object):
2274
"""
2375
this class is used to create a config generator object which will be used to generate cluster definition files
@@ -160,55 +212,3 @@ def resolve_interpolations(self):
160212

161213
def validate_interpolations(self):
162214
self.interpolation_validator.check_all_interpolations_resolved(self.generated_data)
163-
164-
165-
class ConfigProcessor(object):
166-
167-
def process(self, cwd=None, path=None, filters=(), exclude_keys=(), enclosing_key=None, output_format=yaml, print_data=False,
168-
output_file=None, skip_interpolations=False, skip_interpolation_validation=False):
169-
170-
path = self.get_relative_path(path)
171-
172-
if skip_interpolations:
173-
skip_interpolation_validation = True
174-
175-
if cwd is None:
176-
cwd = os.getcwd()
177-
178-
generator = ConfigGenerator(cwd, path)
179-
generator.generate_hierarchy()
180-
generator.process_hierarchy()
181-
182-
if not skip_interpolations:
183-
generator.resolve_interpolations()
184-
generator.add_dynamic_data()
185-
generator.resolve_interpolations()
186-
187-
if len(filters) > 0:
188-
generator.filter_data(filters)
189-
190-
if len(exclude_keys) > 0:
191-
generator.exclude_keys(exclude_keys)
192-
193-
if not skip_interpolation_validation:
194-
generator.validate_interpolations()
195-
196-
data = generator.add_enclosing_key(enclosing_key) if enclosing_key else generator.generated_data
197-
198-
formatted_data = generator.output_data(data, output_format)
199-
200-
if print_data:
201-
print(formatted_data)
202-
203-
if output_file:
204-
with open(output_file, 'w') as f:
205-
f.write(formatted_data)
206-
207-
return data
208-
209-
@staticmethod
210-
def get_relative_path(self, path):
211-
cwd = os.path.join(os.getcwd(), '')
212-
if path.startswith(cwd):
213-
return path[len(cwd):]
214-
return path

hierarchical_yaml/main.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import argparse
2+
import os
3+
from .config_generator import ConfigProcessor
4+
5+
def run(args=None):
6+
""" App entry point """
7+
8+
parser = argparse.ArgumentParser()
9+
parser.add_argument('--output-file', dest='output_file', type=str,
10+
help='output file location')
11+
parser.add_argument('--format', dest='output_format', type=str, default="yaml",
12+
help='output file format')
13+
parser.add_argument('--filter', dest='filter', action='append',
14+
help='keep these keys from the generated data')
15+
parser.add_argument('--exclude', dest='exclude', action='append',
16+
help='exclude these keys from generated data')
17+
parser.add_argument('--skip-interpolation-validation', action='store_true',
18+
help='will not throw an error if interpolations can not be resolved')
19+
parser.add_argument('--skip-interpolation-resolving', action='store_true',
20+
help='do not perform any AWS calls to resolve interpolations')
21+
parser.add_argument('--enclosing-key', dest='enclosing_key', type=str,
22+
help='enclose the generated data under a common key')
23+
parser.add_argument('--cwd', dest='cwd', type=str, default="",
24+
help='the working directory')
25+
26+
opts = parser.parse_args(args)
27+
cwd = opts.cwd if opts.cwd else os.getcwd()
28+
filters = opts.filter if opts.filter else ()
29+
excluded_keys = opts.exclude if opts.exclude else ()
30+
31+
config_processor = ConfigProcessor()
32+
config_processor.process(cwd, opts.path, filters, excluded_keys, opts.enclosing_key, opts.output_format,
33+
print_data=True, opts.output_file, opts.skip_interpolation_resolving,
34+
opts.skip_interpolation_validation)

setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,9 @@
4242
'pyyaml',
4343
'backports.functools_lru_cache',
4444
'boto3'],
45-
entry_points={}
45+
entry_points={
46+
'console_scripts': [
47+
'hyaml = hierarchical_yaml.main:run'
48+
]
49+
}
4650
)

0 commit comments

Comments
 (0)