Skip to content

Commit

Permalink
by default, save/print all manifests; add option to save/print only p…
Browse files Browse the repository at this point in the history
…atched ones; enforce json utf-8 encoding
  • Loading branch information
tsladecek committed Mar 29, 2023
1 parent 9e7c1d2 commit c4b5bba
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
25 changes: 21 additions & 4 deletions src/ksux/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def main():
help='Patches directory. Script will read only files with yaml/yml/json extensions')
parser.add_argument('-e', '--output_extension', help='Extension of output files', choices=['yaml', 'json', 'yml'],
default='json')
parser.add_argument('--patched_only', help='If all manifests should be saved, even those which were not patched',
action='store_true')
parser.add_argument('--dry-run', help='Print manifests to stdout', action="store_true")
parser.add_argument('-q', '--quiet', help='Do not print debug, info and warning messages', action='store_true')
parser.add_argument('--version', help='Package version', action="store_true")
Expand All @@ -33,19 +35,34 @@ def main():
else:
logging.root.setLevel(logging.INFO)

manifests = patch_manifests(base_dir=args.base_dir, patches_dir=args.patches_dir)
final, all_manifests = patch_manifests(base_dir=args.base_dir, patches_dir=args.patches_dir)

# Create final list of manifests
if not args.patched_only:
versions = all_manifests.keys()
final = []

for v in versions:
version = all_manifests[v]
kinds = version.keys()
for k in kinds:
kind = version[k]
resource_names = kind.keys()

for r in resource_names:
final.append(kind[r])

if args.dry_run:
if args.output_extension in ['yaml', 'yml']:
print('---')
for i, m in enumerate(manifests):
for i, m in enumerate(final):
round_trip_dump(m, sys.stdout)
print('---')
else:
print(json.dumps(manifests))
print(json.dumps(final))

else:
save_manifests(patched_manifests=manifests, out_dir=args.out_dir, extension=args.output_extension)
save_manifests(patched_manifests=final, out_dir=args.out_dir, extension=args.output_extension)

if args.output_extension in ['yaml', 'yml']:
logging.warning(
Expand Down
8 changes: 4 additions & 4 deletions src/ksux/src/create_manifests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import os
from typing import Dict, List
from typing import Dict, List, Tuple

from .patch import read_patches, patch_manifest
from .read_file import read_yaml, read_json
Expand Down Expand Up @@ -58,12 +58,12 @@ def read_manifests(base_dir: str) -> Dict:
return manifests


def patch_manifests(base_dir: str, patches_dir: str) -> List[Dict]:
def patch_manifests(base_dir: str, patches_dir: str) -> Tuple[List[Dict], Dict]:
"""
Patch all manifests
:param base_dir: directory with manifest templates
:param patches_dir: dire
:return:
:return: list of patched manifests + all manifests
"""
manifests = read_manifests(base_dir=base_dir)
patches = read_patches(patches_dir=patches_dir)
Expand All @@ -72,4 +72,4 @@ def patch_manifests(base_dir: str, patches_dir: str) -> List[Dict]:
for patch in patches:
patched.append(patch_manifest(patch, manifests))

return patched
return patched, manifests
2 changes: 1 addition & 1 deletion src/ksux/src/save_manifests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def save_manifests(

with open(output_path, 'w') as f:
if extension == 'json':
json.dump(p, f, indent=2)
json.dump(p, f, indent=2, ensure_ascii=False)
elif extension == 'yaml' or extension == 'yml':
round_trip_dump(p, f)
else:
Expand Down

0 comments on commit c4b5bba

Please sign in to comment.