Skip to content

Commit

Permalink
move file logic out of generate_markdown
Browse files Browse the repository at this point in the history
and drive-by simplifications
  • Loading branch information
eaudetcobello committed Jun 15, 2024
1 parent 9c6386b commit 353ccfc
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions k8s/scripts/cis-yaml-to-md.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
CONFIG_FILE = "config.yaml"


def get_variable_substitutions(data: str):
def get_variable_substitutions(data: str) -> dict[str, str]:
return {
"$DATA_DIR": data["master"]["etcd"]["confs"][0],
"$apiserverbin": data["master"]["apiserver"]["bins"][0],
Expand All @@ -40,7 +40,6 @@ def get_variable_substitutions(data: str):
"$etcdbin": data["master"]["etcd"]["bins"][0],
"$etcdconf": data["master"]["etcd"]["confs"][0],
"$kubeletbin": data["node"]["kubelet"]["bins"][0],
"$kubeletbin": data["node"]["kubelet"]["bins"][0],
"$kubeletcafile": data["node"]["kubelet"]["cafile"][0],
"$kubeletconf": data["node"]["kubelet"]["confs"][0],
"$kubeletkubeconfig": data["node"]["kubelet"]["kubeconfig"][0],
Expand Down Expand Up @@ -69,34 +68,31 @@ def to_yaml_filter(value):
)


def generate_markdown(input_dir: Path, output_dir: Path):
# All files in the input directory, but not the config.yaml file.
def generate_markdown(
input_dir: Path, output_dir: Path, substitutions: dict[str, str] = None
):
input_files = [
file
for file in input_dir.iterdir()
if file.is_file() and file.suffix == ".yaml" and file.name != "config.yaml"
]

substs = get_variable_substitutions(
yaml.safe_load((input_dir / "config.yaml").read_text())
)

for file in input_files:
control_data = yaml.safe_load(file.read_text())

markdown_content = make_template(control_data)
markdown_content = make_template(yaml.safe_load(file.read_text()))

for old, new in substs.items():
markdown_content = markdown_content.replace(old, new)
if substitutions:
for old, new in substitutions.items():
markdown_content = markdown_content.replace(old, new)

output_dir.mkdir(exist_ok=True)

output_file = output_dir / f"{file.stem}.md"
output_file.touch()
output_file.write_text(markdown_content)

LOG.info(f"Rendered {file} to {output_file}.")

LOG.w


def main():
parser = argparse.ArgumentParser(usage=USAGE, description=DESCRIPTION)
Expand All @@ -108,7 +104,14 @@ def main():
)
args = parser.parse_args()

generate_markdown(Path(args.input_dir), Path(args.output_dir))
input_dir = Path(args.input_dir).expanduser()
output_dir = Path(args.output_dir).expanduser()

substitutions = get_variable_substitutions(
yaml.safe_load((input_dir / "config.yaml").read_text())
)

generate_markdown(input_dir, output_dir, substitutions)


if __name__ == "__main__":
Expand Down

0 comments on commit 353ccfc

Please sign in to comment.