mirror of
https://github.com/nofusscomputing/kubernetes-manifest-tools.git
synced 2025-08-15 00:40:08 +00:00
50
includes/bin/yaml-format.py
Normal file
50
includes/bin/yaml-format.py
Normal file
@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
import os
|
||||
from ruamel.yaml import YAML
|
||||
|
||||
def debug_print(msg):
|
||||
if os.getenv('is_debug', '').lower() == 'true':
|
||||
print(f"[DEBUG] {msg}", file=sys.stdout)
|
||||
|
||||
def format_yaml_file(input_path, output_path):
|
||||
yaml = YAML()
|
||||
yaml.explicit_start = True # Add ---
|
||||
yaml.indent(mapping=2, sequence=4, offset=2)
|
||||
|
||||
debug_print(f"Reading file: {input_path}")
|
||||
with open(input_path, 'r') as f:
|
||||
data = yaml.load(f)
|
||||
debug_print(f"YAML type: {type(data)}")
|
||||
|
||||
with open(output_path, 'w') as f:
|
||||
yaml.dump(data, f)
|
||||
|
||||
print(f"[INFO] Wrote formatted: {output_path}", file=sys.stdout)
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 3:
|
||||
print(f"[ERROR] Usage: {sys.argv[0]} <input_dir> <output_dir>", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
input_dir, output_dir = sys.argv[1], sys.argv[2]
|
||||
|
||||
if not os.path.isdir(input_dir):
|
||||
print(f"[ERROR] Input '{input_dir}' is not a directory", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
debug_print(f"Created output dir: {output_dir}")
|
||||
|
||||
for filename in os.listdir(input_dir):
|
||||
if filename.endswith(('.yaml', '.yml')):
|
||||
input_path = os.path.join(input_dir, filename)
|
||||
output_path = os.path.join(output_dir, filename)
|
||||
|
||||
try:
|
||||
format_yaml_file(input_path, output_path)
|
||||
except Exception as e:
|
||||
print(f"[ERROR] Failed on {input_path}: {e}", file=sys.stderr)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
86
includes/entrypoint.sh
Normal file
86
includes/entrypoint.sh
Normal file
@ -0,0 +1,86 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -e
|
||||
|
||||
export TMP_OUTPUT_DIR="/tmp/yaml-files";
|
||||
|
||||
if [ -d "/workspace/github" ]; then
|
||||
|
||||
|
||||
export NFC_GIT_PROVIDER=github;
|
||||
|
||||
export NFC_WORKDIR="/github/workspace";
|
||||
|
||||
|
||||
if [ "${ACTIONS_STEP_DEBUG:-}" = "true" ] || [ "${RUNNER_DEBUG:-}" = "1" ]; then
|
||||
|
||||
export KUBECTL_SLICE_DEBUG="true"
|
||||
|
||||
fi;
|
||||
|
||||
|
||||
elif [ -n "${CI_PROJECT_DIR}" ]; then
|
||||
|
||||
|
||||
export NFC_GIT_PROVIDER=gitlab;
|
||||
|
||||
export NFC_WORKDIR="${CI_PROJECT_DIR}"
|
||||
|
||||
else
|
||||
|
||||
|
||||
export NFC_WORKDIR="/workdir"
|
||||
|
||||
|
||||
fi;
|
||||
|
||||
|
||||
cd $NFC_WORKDIR;
|
||||
|
||||
|
||||
if [ ! -n "${KUBECTL_SLICE_INPUT_FILE}" ]; then
|
||||
|
||||
echo "var KUBECTL_SLICE_INPUT_FILE must be set to the input file.";
|
||||
exit 1;
|
||||
|
||||
fi;
|
||||
|
||||
|
||||
if [ ! -n "${KUBECTL_SLICE_TEMPLATE}" ]; then
|
||||
|
||||
export KUBECTL_SLICE_TEMPLATE='{{ .kind }}-{{ .metadata.name | dottodash | replace ":" "-" }}.yaml';
|
||||
|
||||
fi;
|
||||
|
||||
|
||||
if [ ! -n "${KUBECTL_SLICE_OUTPUT_DIR}" ]; then
|
||||
|
||||
export NFC_OUTPUT_DIR="${PWD}";
|
||||
|
||||
else
|
||||
|
||||
export NFC_OUTPUT_DIR="${KUBECTL_SLICE_OUTPUT_DIR}";
|
||||
|
||||
fi;
|
||||
|
||||
if printf '%s\n' "${KUBECTL_SLICE_INPUT_FILE}" | grep -Eq '^https?://|^ftp://'; then
|
||||
|
||||
wget ${KUBECTL_SLICE_INPUT_FILE} -O /tmp/manifest.yaml;
|
||||
|
||||
export KUBECTL_SLICE_INPUT_FILE="/tmp/manifest.yaml";
|
||||
|
||||
fi
|
||||
|
||||
mkdir -p $TMP_OUTPUT_DIR;
|
||||
|
||||
kubectl-slice --output-dir $TMP_OUTPUT_DIR
|
||||
|
||||
|
||||
if [ "${NFC_FORMAT_YAML:-}" = "true" ]; then
|
||||
|
||||
echo "adding yaml headers '---' to output files";
|
||||
|
||||
# sed -i "1s/^/---\n/" ${KUBECTL_SLICE_OUTPUT_DIR}*.yaml;
|
||||
yaml-format $TMP_OUTPUT_DIR $NFC_OUTPUT_DIR
|
||||
|
||||
fi;
|
Reference in New Issue
Block a user