From 08004fa3662a09de29aa9a80cb6c60b7fdf852a0 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 4 Dec 2024 14:36:10 +0100 Subject: [PATCH] bin: Only show invalid values in validate This reduces the amount of output when the config is validated on load while running other commands, which tends to become repetitive and overly verbose. If you run `ck8s validate`, you are probably more interested in seeing these details. --- bin/ck8s | 2 +- bin/common.bash | 24 +++++++++++++----------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/bin/ck8s b/bin/ck8s index 05a434d51..ad810212a 100755 --- a/bin/ck8s +++ b/bin/ck8s @@ -123,7 +123,7 @@ install-requirements) validate) [[ "${2}" =~ ^(wc|sc)$ ]] || usage check_tools - config_load "$2" + config_load "$2" -v echo "Config validation successful" ;; providers) echo "${ck8s_cloud_providers[@]}" ;; diff --git a/bin/common.bash b/bin/common.bash index 02fa72742..7cb201df8 100644 --- a/bin/common.bash +++ b/bin/common.bash @@ -393,13 +393,15 @@ validate_config() { if ! yajsv -s "${schema_file}" "${merged_config}" >"${schema_validation_result}"; then log_warning "Failed schema validation:" sed -r 's/^.*_(..-config\.yaml): fail: (.*)/\1: \2/; / failed validation$/q' <"${schema_validation_result}" - grep -oP '(?<=fail: )[^:]+' "${schema_validation_result}" | sort -u | - while read -r jpath; do - if [[ $jpath != "(root)" ]]; then - echo -n ".$jpath = " - yq4 -oj ".$jpath" "${merged_config}" - fi - done + if [[ "${3:-}" == "-v" ]]; then + grep -oP '(?<=fail: )[^:]+' "${schema_validation_result}" | sort -u | + while read -r jpath; do + if [[ $jpath != "(root)" ]]; then + echo -n ".$jpath = " + yq4 -oj ".$jpath" "${merged_config}" + fi + done + fi maybe_exit="true" fi } @@ -431,10 +433,10 @@ validate_config() { check_conditionals "${config_to_validate}" "${template_file}" validate "${config_to_validate}" "${template_file}" - schema_validate "${config_to_validate}" "${config_template_path}/schemas/config.yaml" + schema_validate "${config_to_validate}" "${config_template_path}/schemas/config.yaml" "${2:-}" check_conditionals "${secrets[secrets_file]}" "${config_template_path}/secrets.yaml" validate "${secrets[secrets_file]}" "${config_template_path}/secrets.yaml" - schema_validate "${secrets[secrets_file]}" "${config_template_path}/schemas/secrets.yaml" + schema_validate "${secrets[secrets_file]}" "${config_template_path}/schemas/secrets.yaml" "${2:-}" if ${maybe_exit} && ! ${CK8S_AUTO_APPROVE}; then ask_abort @@ -473,13 +475,13 @@ validate_sops_config() { } # Load and validate all configuration options from the config path. -# Usage: config_load [sk] +# Usage: config_load [--skip-validation|-v] config_load() { load_config "$1" if [[ "--skip-validation" != "${2:-''}" ]]; then validate_version "$1" - validate_config "$1" + validate_config "$1" "${2:-''}" validate_sops_config fi }