Skip to content

Commit

Permalink
CH-141 merge json files
Browse files Browse the repository at this point in the history
  • Loading branch information
filippomc committed Jul 30, 2024
1 parent f403e8c commit 419910d
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 1 deletion.
11 changes: 11 additions & 0 deletions application-templates/webapp/frontend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "__APP_NAME__",
"scripts": {
"dev": "vite",
"start": "DOMAIN=http://localhost:5000 vite",
"start:dev": "DOMAIN=https://test.ch.metacell.us vite",
"start:local": "DOMAIN=http://samples.ch vite",
"prebuild": "eslint .",
"build": "vite build"
}
}
2 changes: 1 addition & 1 deletion tools/deployment-cli-tools/ch_cli_tools/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ def image_tag(self, image_name, build_context_path=None, dependencies=()):
ignore = set(DEFAULT_IGNORE)
if os.path.exists(ignore_path):
with open(ignore_path) as f:
ignore = ignore.union({line.strip() for line in f})
ignore = ignore.union({line.strip() for line in f if line.strip() and not line.startswith('#')})
logging.info(f"Ignoring {ignore}")
tag = generate_tag_from_content(build_context_path, ignore)
logging.info(f"Content hash: {tag}")
Expand Down
32 changes: 32 additions & 0 deletions tools/deployment-cli-tools/ch_cli_tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ def get_template(yaml_path, base_default=False):
def file_is_yaml(fname):
return fname[-4:] == 'yaml' or fname[-3:] == 'yml'

def file_is_json(fname):
return fname[-4:] == 'json'

def replaceindir(root_src_dir, source, replace):
"""
Expand Down Expand Up @@ -270,6 +272,14 @@ def merge_configuration_directories(source, dest):
except Exception as e:
logging.warning(f"Overwriting file {fdest} with {fpath}")
shutil.copy2(fpath, fdest)
elif file_is_json(fpath):
try:
merge_json_files(fpath, fdest)
logging.info(
f"Merged/overridden file content of {fdest} with {fpath}")
except Exception as e:
logging.warning(f"Overwriting file {fdest} with {fpath}")
shutil.copy2(fpath, fdest)
else:
logging.warning(f"Overwriting file {fdest} with {fpath}")
shutil.copy2(fpath, fdest)
Expand All @@ -280,6 +290,28 @@ def merge_yaml_files(fname, fdest):
content_src = yaml.load(f)
merge_to_yaml_file(content_src, fdest)

def merge_json_files(fname, fdest):
with open(fname) as f:
content_src = json.load(f)
merge_to_json_file(content_src, fdest)

def merge_to_json_file(content_src, fdest):
if not content_src:
return
if not exists(fdest):
merged = content_src
else:
with open(fdest) as f:
content_dest = json.load(f)

merged = dict_merge(
content_dest, content_src) if content_dest else content_src

if not exists(dirname(fdest)):
os.makedirs(dirname(fdest))
with open(fdest, "w") as f:
json.dump(merged, f, indent=2)
return merged

def merge_to_yaml_file(content_src, fdest):
if not content_src:
Expand Down
1 change: 1 addition & 0 deletions tools/deployment-cli-tools/harness-application
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ if __name__ == "__main__":
shutil.move(os.path.join(app_path, args.name), os.path.join(app_path, 'frontend'))
generate_ts_client(openapi_file=os.path.join(app_path, 'api/openapi.yaml'))


if 'server' in templates:
with tempfile.TemporaryDirectory() as tmp_dirname:
copymergedir(os.path.join(CH_ROOT, APPLICATION_TEMPLATE_PATH, template_name), tmp_dirname)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"a": "a",
"b": {
"ba": "ba",
"bc": "bc"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"a": "a",
"b": {
"ba": "ba",
"bb": "bb"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"a": "a1",
"b": {
"ba": "ba1",
"bb": "bb"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"a": "a",
"b": {
"ba": "ba",
"bb": "bb"
}
}
11 changes: 11 additions & 0 deletions tools/deployment-cli-tools/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ def test_merge_configuration_directories():
assert a['b']['ba'] == 'ba1'
assert a['b']['bb'] == 'bb'
assert a['b']['bc'] == 'bc'

assert os.path.exists(os.path.join(res_path, "a.json"))
assert os.path.exists(os.path.join(res_path, "b.json"))
assert os.path.exists(os.path.join(res_path, "c.json"))

with open(os.path.join(res_path, "a.json")) as f:
a = json.load(f)
assert a['a'] == 'a1'
assert a['b']['ba'] == 'ba1'
assert a['b']['bb'] == 'bb'
assert a['b']['bc'] == 'bc'
finally:
if os.path.exists(res_path):
shutil.rmtree(res_path)
Expand Down

0 comments on commit 419910d

Please sign in to comment.