Created
February 10, 2025 09:56
-
-
Save martimors/9a644996c56b308fad3f1ff8dc412157 to your computer and use it in GitHub Desktop.
Split the output of the `helm template` command into multiple yaml files using the source path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import yaml | |
| from pathlib import Path | |
| import re | |
| import argparse | |
| import os | |
| import sys | |
| class Args(argparse.Namespace): | |
| input_path: str | |
| output_path: str | |
| def parse_arguments() -> Args: | |
| parser = argparse.ArgumentParser( | |
| description="""Process a YAML file output from the helm template command and write output documents. | |
| Generate the file using helm, eg. `helm template superset/superset > k8s.yaml`. | |
| """ | |
| ) | |
| parser.add_argument( | |
| "input_path", | |
| help="Path to the YAML file output of the `helm template` command. Defaults to './k8s.yaml'.", | |
| default="./k8s.yaml", | |
| nargs="?", | |
| ) | |
| parser.add_argument( | |
| "output_path", | |
| help="Root path to write the output documents. Defaults to './deployments'.", | |
| default="./deployments", | |
| nargs="?", | |
| ) | |
| args = parser.parse_args() | |
| return Args(**vars(args)) | |
| def main(): | |
| args = parse_arguments() | |
| # Ensure input file exists | |
| if not os.path.isfile(args.input_path): | |
| print(f"Error: Input file '{args.input_path}' does not exist.", file=sys.stderr) | |
| sys.exit(1) | |
| # Ensure output path exists | |
| if not os.path.isdir(args.output_path): | |
| print(f"Error: Output path '{args.output_path}' does not exist.", file=sys.stderr) | |
| sys.exit(1) | |
| # Placeholder for YAML processing logic | |
| print(f"Processing YAML file: {args.input_path}") | |
| print(f"Writing output to: {args.output_path}") | |
| proceed = input("Continue? [y/N] ").strip().lower() | |
| if proceed != "y": | |
| print("Exiting.") | |
| sys.exit(0) | |
| in_path = args.input_path | |
| out_path = args.output_path | |
| path_pattern = r"# Source: ([\w./-]+)" | |
| with Path(in_path).open() as f: | |
| content = f.read() | |
| matches = re.findall(path_pattern, content) | |
| docs = yaml.safe_load_all(content) | |
| for path, doc in zip(matches, docs): | |
| path = out_path / Path(path) | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| with path.open("w") as out: | |
| out.write(yaml.safe_dump(doc)) | |
| print(f"Written {path}") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment